Module: TimeExt + + + +
+ +-
+
+
+
+
+
+
+
+
+
- Defined in: +
- lib/time_ext.rb,
+ lib/time_ext/method_chain.rb +
+
+
Defined Under Namespace
++ + + Modules: Base, MethodChain + + + + +
+ + + + + +From 820057f842b7bf2b03ba8638db5867238b189ea9 Mon Sep 17 00:00:00 2001
From: Jim Myhrberg
+Executes passed block for each “unit” of time specified, with a
+new time object set to the beginning of “unit” for each
+interval passed to the block.
+
+ lib/time_ext/core_ext/time/calculations.rb
+
@@ -93,6 +103,32 @@
+Executes passed block for each “unit” of time specified, with a +new time object for each interval passed to the block. +
++Returns a new Time representing the end of the unit specified (defaults to +second). +
++Used by #each, #map_each and similar methods to iterate over ranges of +time. +
++Executes passed block for each “unit” of time specified, +returning an array with the return values from passed block. +
++Executes passed block for each “unit” of time specified, +returning an array with the return values from the passed block. +
++Returns a new Time representing the next unit specified (defaults to +second). +
++Returns a new Time representing the previoius unit specified (defaults to +second). +
++Used togeter with #each to specify end of interation. +
++
+ + - (Object) beginning_of_each(unit, options = {}, &block) + + + +
+Executes passed block for each “unit” of time specified, with a +new time object set to the beginning of “unit” for each +interval passed to the block. +
+ + +
+ + + +34 +35 +36+ |
+
+ # File 'lib/time_ext/core_ext/time/iterations.rb', line 34 + +def beginning_of_each(unit, = {}, &block) + iterate(unit, .merge(:map_result => false, :beginning_of => true), &block) +end ++ |
+
- (Object) beginning_of_hour @@ -937,12 +1226,12 @@ Returns a new Time representing the start of the hour (XX:00:00).
-181 -182 -183+201 +202 +203
# File 'lib/time_ext/time.rb', line 181 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 201 def beginning_of_hour change(:min => 0, :sec => 0, :usec => 0) @@ -981,12 +1270,12 @@ Returns a new Time representing the start of the minute (XX:XX:00).-167 -168 -169+187 +188 +189
# File 'lib/time_ext/time.rb', line 167 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 187 def beginning_of_minute change(:sec => 0, :usec => 0) @@ -1026,12 +1315,12 @@ Returns a new Time representing the start of the second, XX:XX:XX.000000-153 -154 -155+173 +174 +175
# File 'lib/time_ext/time.rb', line 153 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 173 def beginning_of_second change(:usec => 0) @@ -1076,7 +1365,7 @@ Returns a new Time representing the start of the next unit specified 23
# File 'lib/time_ext/time.rb', line 21 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 21 def ceil(unit = :sec) self.send("next_#{unit}").send("beginning_of_#{unit}") @@ -1116,7 +1405,7 @@ Helper method for backwards compatibility with ActiveSupport. 11
# File 'lib/time_ext/time.rb', line 9 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 9 def common_year_days_in_month defined?(COMMON_YEAR_DAYS_IN_MONTH) ? COMMON_YEAR_DAYS_IN_MONTH : [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -1151,12 +1440,12 @@ Returns a new Time representing the time a number of specified days ago.-123 -124 -125+133 +134 +135
# File 'lib/time_ext/time.rb', line 123 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 133 def days_ago(days) ago(days.days) @@ -1196,7 +1485,7 @@ Helper method for backwards compatibility with ActiveSupport. 6
# File 'lib/time_ext/time.rb', line 4 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 4 def days_into_week defined?(DAYS_INTO_WEEK) ? DAYS_INTO_WEEK : { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 } @@ -1232,12 +1521,12 @@ future.-128 -129 -130+138 +139 +140
# File 'lib/time_ext/time.rb', line 128 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 138 def days_since(days) since(days.days) @@ -1246,6 +1535,88 @@ future.
+ + - (Object) each(unit, options = {}, &block) + + + +
+Executes passed block for each “unit” of time specified, with a +new time object for each interval passed to the block. +
+ + +
+ + + +29 +30 +31+ |
+
+ # File 'lib/time_ext/core_ext/time/iterations.rb', line 29 + +def each(unit, = {}, &block) + iterate(unit, .merge(:map_result => false), &block) +end ++ |
+
+ + - (Object) end_of(unit = :sec) + + + +
+Returns a new Time representing the end of the unit specified (defaults to +second). +
+ + +
+ + + +168 +169 +170+ |
+
+ # File 'lib/time_ext/core_ext/time/calculations.rb', line 168 + +def end_of(unit = :sec) + send("end_of_#{unit}") +end ++ |
+
-187 -188 -189+207 +208 +209
# File 'lib/time_ext/time.rb', line 187 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 207 def end_of_hour change(:min => 59, :sec => 59, :usec => 999999.999) @@ -1318,12 +1689,12 @@ Returns a new Time representing the end of the hour, XX:XX:59.999999-175 -176 -177+195 +196 +197
# File 'lib/time_ext/time.rb', line 175 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 195 def end_of_minute change(:sec => 59, :usec => 999999.999) @@ -1363,12 +1734,12 @@ Returns a new Time representing the end of the hour, XX:XX:XX.999999-161 -162 -163+181 +182 +183
# File 'lib/time_ext/time.rb', line 161 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 181 def end_of_second change(:usec => 999999.999) @@ -1413,7 +1784,7 @@ default). 16
# File 'lib/time_ext/time.rb', line 14 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 14 def floor(unit = :sec) self.send("beginning_of_#{unit}") @@ -1448,12 +1819,12 @@ Returns a new Time representing the time a number of specified hours ago.-113 -114 -115+123 +124 +125
# File 'lib/time_ext/time.rb', line 113 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 123 def hours_ago(hours) ago(hours.hours) @@ -1489,12 +1860,12 @@ future.-118 -119 -120+128 +129 +130
# File 'lib/time_ext/time.rb', line 118 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 128 def hours_since(hours) since(hours.hours) @@ -1503,6 +1874,169 @@ future.
+ + - (Object) iterate(unit, options = {}, &block) + + + +
+Used by #each, #map_each and similar methods to iterate over ranges of +time. +
+ + +
+ + + +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26+ |
+
+ # File 'lib/time_ext/core_ext/time/iterations.rb', line 5 + +def iterate(unit, = {}, &block) + .reverse_merge!(:map_result => false, :beginning_of => false, :include_start => false) + if block_given? + units = [:year, :month, :day, :hour, :min, :sec, :usec] + parent_unit = units[units.index(unit)-1] + @until ||= (!parent_unit.nil?) ? self.send("#{parent_unit}s_since", 1) : self.send("#{unit}s_since", 1) + time = self.clone + direction = (self < @until) ? :f : :b + succ_method = (direction == :f) ? "next_#{unit}" : "prev_#{unit}" + time = time.beginning_of(unit) if [:beginning_of] + time = time.send(succ_method) if ![:include_start] + results = [] + while (direction == :f && time <= @until) || (direction == :b && time >= @until) + [:map_result] ? results << yield(time) : yield(time) + time = time.send(succ_method) + end + [:map_result] ? results : self + else + add_to_chain(:iterate, unit, ) + self + end +end ++ |
+
+ + - (Object) map_beginning_of_each(unit, options = {}, &block) + + + +
+Executes passed block for each “unit” of time specified, +returning an array with the return values from passed block. Additionally +the time object passed into the block is set to the beginning of specified +“unit”. +
+ + +
+ + + +44 +45 +46+ |
+
+ # File 'lib/time_ext/core_ext/time/iterations.rb', line 44 + +def map_beginning_of_each(unit, = {}, &block) + iterate(unit, .merge(:map_result => true, :beginning_of => true), &block) +end ++ |
+
+ + - (Object) map_each(unit, options = {}, &block) + + + +
+Executes passed block for each “unit” of time specified, +returning an array with the return values from the passed block. +
+ + +
+ + + +39 +40 +41+ |
+
+ # File 'lib/time_ext/core_ext/time/iterations.rb', line 39 + +def map_each(unit, = {}, &block) + iterate(unit, .merge(:map_result => true), &block) +end ++ |
+
-101 -102 -103+111 +112 +113
# File 'lib/time_ext/time.rb', line 101 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 111 def minutes_ago(minutes) ago(minutes.minutes) @@ -1578,12 +2112,12 @@ the future.-107 -108 -109+117 +118 +119
# File 'lib/time_ext/time.rb', line 107 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 117 def minutes_since(minutes) since(minutes.minutes) @@ -1592,6 +2126,47 @@ the future.
+ + - (Object) next(unit = :sec) + + + +
+Returns a new Time representing the next unit specified (defaults to +second). +
+ + +
+ + + +41 +42 +43+ |
+
+ # File 'lib/time_ext/core_ext/time/calculations.rb', line 41 + +def next(unit = :sec) + send("next_#{unit}") +end ++ |
+
-75 -76 -77+85 +86 +87
# File 'lib/time_ext/time.rb', line 75 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 85 def next_day days_since(1) @@ -1658,12 +2233,12 @@ Short-hand for hours_since(1).-65 -66 -67+75 +76 +77
# File 'lib/time_ext/time.rb', line 65 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 75 def next_hour hours_since(1) @@ -1702,12 +2277,12 @@ Short-hand for minutes_since(1).-54 -55 -56+64 +65 +66
# File 'lib/time_ext/time.rb', line 54 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 64 def next_minute minutes_since(1) @@ -1742,12 +2317,12 @@ Short-hand for quarters_since(1).beginning_of_quarter.-85 -86 -87+95 +96 +97
# File 'lib/time_ext/time.rb', line 85 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 95 def next_quarter quarters_since(1).beginning_of_quarter @@ -1786,12 +2361,12 @@ Short-hand for seconds_since(1).-42 -43 -44+52 +53 +54
# File 'lib/time_ext/time.rb', line 42 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 52 def next_second since(1) @@ -1800,6 +2375,47 @@ Short-hand for seconds_since(1).
+ + - (Object) prev(unit = :sec) + + + +
+Returns a new Time representing the previoius unit specified (defaults to +second). +
+ + +
+ + + +36 +37 +38+ |
+
+ # File 'lib/time_ext/core_ext/time/calculations.rb', line 36 + +def prev(unit = :sec) + send("prev_#{unit}") +end ++ |
+
-70 -71 -72+80 +81 +82
# File 'lib/time_ext/time.rb', line 70 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 80 def prev_day days_ago(1) @@ -1866,12 +2482,12 @@ Short-hand for hours_ago(1).-60 -61 -62+70 +71 +72
# File 'lib/time_ext/time.rb', line 60 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 70 def prev_hour hours_ago(1) @@ -1910,12 +2526,12 @@ Short-hand for minutes_ago(1).-48 -49 -50+58 +59 +60
# File 'lib/time_ext/time.rb', line 48 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 58 def prev_minute minutes_ago(1) @@ -1950,12 +2566,12 @@ Short-hand for quarters_ago(1).beginning_of_quarter.-90 -91 -92+100 +101 +102
# File 'lib/time_ext/time.rb', line 90 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 100 def prev_quarter quarters_ago(1).beginning_of_quarter @@ -1994,12 +2610,12 @@ Short-hand for seconds_ago(1).-36 -37 -38+46 +47 +48
# File 'lib/time_ext/time.rb', line 36 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 46 def prev_second ago(1) @@ -2035,12 +2651,12 @@ week (default is Monday).-80 -81 -82+90 +91 +92
# File 'lib/time_ext/time.rb', line 80 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 90 def prev_week(day = :monday) weeks_ago(1).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) @@ -2076,12 +2692,12 @@ months) ago.-143 -144 -145+153 +154 +155
# File 'lib/time_ext/time.rb', line 143 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 153 def quarters_ago(quarters) ago((quarters * 3).months) @@ -2117,12 +2733,12 @@ months) in the future.-148 -149 -150+158 +159 +160
# File 'lib/time_ext/time.rb', line 148 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 158 def quarters_since(quarters) since((quarters * 3).months) @@ -2169,7 +2785,7 @@ specified (second by default) depending which is closest 32
# File 'lib/time_ext/time.rb', line 28 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 28 def round(unit = :sec) next_unit = self.ceil(unit) @@ -2180,6 +2796,64 @@ specified (second by default) depending which is closest
+ + - (Object) until(time, &block) + + + + Also known as: + till + + +
+Used togeter with #each to specify end of interation. +
+ + +
+ + + +49 +50 +51 +52 +53 +54 +55 +56 +57 +58+ |
+
+ # File 'lib/time_ext/core_ext/time/iterations.rb', line 49 + +def until(time, &block) + time = time.to_time if time.is_a?(::Date) + @until = time + if block_given? + call_chain(block) + else + add_to_chain(:until, time) + self + end +end ++ |
+
-133 -134 -135+143 +144 +145
# File 'lib/time_ext/time.rb', line 133 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 143 def weeks_ago(weeks) ago(weeks.weeks) @@ -2247,12 +2921,12 @@ future.-138 -139 -140+148 +149 +150
# File 'lib/time_ext/time.rb', line 138 +# File 'lib/time_ext/core_ext/time/calculations.rb', line 148 def weeks_since(weeks) since(weeks.weeks) @@ -2268,7 +2942,7 @@ future. diff --git a/doc/TimeExt.html b/doc/TimeExt.html new file mode 100644 index 0000000..d874c64 --- /dev/null +++ b/doc/TimeExt.html @@ -0,0 +1,93 @@ + + + + +Module: TimeExt + + + + + + + + + + + ++ + + + + ++ + + ++ + + + + \ No newline at end of file diff --git a/doc/TimeExt/Base.html b/doc/TimeExt/Base.html new file mode 100644 index 0000000..0d6e786 --- /dev/null +++ b/doc/TimeExt/Base.html @@ -0,0 +1,94 @@ + + + + +Module: TimeExt + + + +
+ ++ + + + + + + + +
+ + +- Defined in:
+- lib/time_ext.rb,
+ +
+ lib/time_ext/method_chain.rb +Defined Under Namespace
++ + + Modules: Base, MethodChain + + + + +
+ + + + + +Module: TimeExt::Base + + + + + + + + + + + ++ + + + + ++ + + ++ + + + + \ No newline at end of file diff --git a/doc/TimeExt/MethodChain.html b/doc/TimeExt/MethodChain.html new file mode 100644 index 0000000..98d5ce6 --- /dev/null +++ b/doc/TimeExt/MethodChain.html @@ -0,0 +1,212 @@ + + + + +Module: TimeExt::Base + + + +
+ ++ + + + + + + + +
+ + + +- Defined in:
+- lib/time_ext.rb
+ +Constant Summary
+ ++ +
+ + + + + + +- TIME_EXT_UNITS = + +
+- + +
[:year, :month, :day, :hour, :min, :sec, :usec] +Module: TimeExt::MethodChain + + + + + + + + + + + ++ + + + + ++ + + ++ + + + + \ No newline at end of file diff --git a/doc/_index.html b/doc/_index.html index fa97280..9701ea5 100644 --- a/doc/_index.html +++ b/doc/_index.html @@ -67,6 +67,36 @@Module: TimeExt::MethodChain + + + +
+ ++ + + + + + + +
+ + + + + + +- Included in:
+- Time
+ + + +- Defined in:
+- lib/time_ext/method_chain.rb
+ ++ Instance Method Summary + (collapse) +
+ ++ +
+ + + + +- + + + - (Object) add_to_chain(method, *args, &block) + + + + + + + + + + + + + + +
+ + +- + + + - (Object) call_chain(custom_block = nil, &block) + + + + + + + + + + + + + + +
+ + +++ +Instance Method Details
+ + +++ ++ + - (Object) add_to_chain(method, *args, &block) + + + +
+
++ ++ ++ + +4 +5 +6 +7++ +# File 'lib/time_ext/method_chain.rb', line 4 + +def add_to_chain(method, *args, &block) + @method_chain ||= [] + @method_chain << [method.to_sym, args, block] +end ++++ ++ + - (Object) call_chain(custom_block = nil, &block) + + + +
+
++ ++ ++ + +9 +10 +11 +12 +13 +14 +15++ +# File 'lib/time_ext/method_chain.rb', line 9 + +def call_chain(custom_block = nil, &block) + method, args, iblock = @method_chain.pop + return nil if method.nil? + iblock = custom_block if !custom_block.nil? + method, args, iblock = yield(method, args, iblock) if block_given? + self.send(method, *args, &iblock) +end +++ +
+ + +- B
++ +
+- + Base + + (TimeExt) + +
+ ++
+ +- M
++ +
+- + MethodChain + + (TimeExt) + +
+ +@@ -85,7 +120,7 @@ diff --git a/doc/class_list.html b/doc/class_list.html index 26ba488..e0b5213 100644 --- a/doc/class_list.html +++ b/doc/class_list.html @@ -27,7 +27,7 @@
- T
@@ -76,6 +106,11 @@ +
- + TimeExt + +
+diff --git a/doc/file.README.html b/doc/file.README.html index a32c38e..f361b9e 100644 --- a/doc/file.README.html +++ b/doc/file.README.html @@ -96,7 +96,7 @@
- Top Level Namespace
-- Time < ObjectTop Level Namespace
+- Time < ObjectTop Level Namespace
- TimeExtTop Level Namespace
- BaseTimeExt
- MethodChainTimeExt
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/doc/index.html b/doc/index.html index a32c38e..f361b9e 100644 --- a/doc/index.html +++ b/doc/index.html @@ -96,7 +96,7 @@THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/doc/method_list.html b/doc/method_list.html index 03f383e..85605b8 100644 --- a/doc/method_list.html +++ b/doc/method_list.html @@ -28,6 +28,22 @@+
- + #add_to_chain + + TimeExt::MethodChain + +
+ + +- + #beginning_of_each + + Time + +
+ +- #beginning_of_hour @@ -53,6 +69,14 @@
- + #call_chain + + TimeExt::MethodChain + +
+ + +- #ceil Time @@ -60,7 +84,7 @@
-- +
- #common_year_days_in_month Time @@ -68,7 +92,7 @@
-- +
- #days_ago Time @@ -76,7 +100,7 @@
-- +
- #days_into_week Time @@ -84,7 +108,7 @@
-- +
- #days_since Time @@ -92,7 +116,23 @@
+- + #each + + Time + +
+ +- + #end_of + + Time + +
+ + +- #end_of_hour Time @@ -100,7 +140,7 @@
-- +
- #end_of_minute Time @@ -108,7 +148,7 @@
-- +
- #end_of_second Time @@ -116,7 +156,7 @@
-- +
- #floor Time @@ -124,7 +164,7 @@
-- +
- #hours_ago Time @@ -132,7 +172,7 @@
-- +
- #hours_since Time @@ -140,6 +180,30 @@
+- + #iterate + + Time + +
+ + +- + #map_beginning_of_each + + Time + +
+ + +- + #map_each + + Time + +
+ +- #minutes_ago @@ -157,6 +221,14 @@
- + #next + + Time + +
+ + +- #next_day Time @@ -164,7 +236,7 @@
-- +
- #next_hour Time @@ -172,7 +244,7 @@
-- +
- #next_minute Time @@ -180,7 +252,7 @@
-- +
- #next_quarter Time @@ -188,7 +260,7 @@
-- +
- #next_second Time @@ -196,6 +268,14 @@
+- + #prev + + Time + +
+ +- #prev_day @@ -269,7 +349,7 @@
- - #weeks_ago + #until Time @@ -277,6 +357,14 @@
- + #weeks_ago + + Time + +
+ + +- #weeks_since Time diff --git a/doc/top-level-namespace.html b/doc/top-level-namespace.html index f18b1ea..292b54b 100644 --- a/doc/top-level-namespace.html +++ b/doc/top-level-namespace.html @@ -65,6 +65,8 @@