time_ext 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,7 +14,7 @@ This gem extends the abilities of Ruby's built-in Time class by building on top
14
14
 
15
15
  require "time/ext" # or "time_ext"
16
16
  Time.utc(2010, 6, 19).round(:month) #=> Thu Jul 01 00:00:00 UTC 2010
17
- #=> Beginning of this week, or next week depending on which date is closest
17
+ #=> Beginning of this month, or beginning of next month depending on which is closest
18
18
  Time.now.each_hour.until(6.hours.from_now) { |t| puts t.to_s }
19
19
  #=> Prints the time at one hour interals from now till 6 hours from now
20
20
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -0,0 +1,14 @@
1
+ module TimeExt
2
+ # Provides helper methods used by TimeExt::Calculations for backwards compatibility with ActiveSupport.
3
+ module BackwardsCompatibility
4
+
5
+ def days_into_week
6
+ defined?(DAYS_INTO_WEEK) ? DAYS_INTO_WEEK : { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
7
+ end
8
+
9
+ def common_year_days_in_month
10
+ defined?(COMMON_YEAR_DAYS_IN_MONTH) ? COMMON_YEAR_DAYS_IN_MONTH : [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,193 @@
1
+ module TimeExt
2
+ # Adds an even greater extent of calculation methods on top of those already provided by ActiveSupport.
3
+ module Calculations
4
+
5
+ # Returns a new Time representing the start of the unit specified (second by default).
6
+ def floor(unit = :sec)
7
+ self.send("beginning_of_#{unit}")
8
+ end
9
+ alias :beginning_of :floor
10
+ alias :at_beginning_of :floor
11
+
12
+ # Returns a new Time representing the start of the next unit specified (second by default).
13
+ def ceil(unit = :sec)
14
+ self.send("next_#{unit}").send("beginning_of_#{unit}")
15
+ end
16
+ alias :beginning_of_next :ceil
17
+ alias :at_beginning_of_next :ceil
18
+
19
+ # Returns a new Time representing the start of the current or next unit specified (second by default) depending which is closest
20
+ def round(unit = :sec)
21
+ next_unit = self.ceil(unit)
22
+ this_unit = self.floor(unit)
23
+ (self - this_unit) < (next_unit - self) ? this_unit : next_unit
24
+ end
25
+ alias :beginning_of_closest :round
26
+
27
+ # Returns a new Time representing the previoius unit specified (defaults to second).
28
+ def prev(unit = :sec)
29
+ send("prev_#{unit}")
30
+ end
31
+
32
+ # Returns a new Time representing the next unit specified (defaults to second).
33
+ def next(unit = :sec)
34
+ send("next_#{unit}")
35
+ end
36
+
37
+ # Short-hand for seconds_ago(1).
38
+ def prev_second
39
+ seconds_ago(1)
40
+ end
41
+ alias :prev_sec :prev_second
42
+
43
+ # Short-hand for seconds_since(1).
44
+ def next_second
45
+ seconds_since(1)
46
+ end
47
+ alias :next_sec :next_second
48
+
49
+ # Short-hand for minutes_ago(1).
50
+ def prev_minute
51
+ minutes_ago(1)
52
+ end
53
+ alias :prev_min :prev_minute
54
+
55
+ # Short-hand for minutes_since(1).
56
+ def next_minute
57
+ minutes_since(1)
58
+ end
59
+ alias :next_min :next_minute
60
+
61
+ # Short-hand for hours_ago(1).
62
+ def prev_hour
63
+ hours_ago(1)
64
+ end
65
+
66
+ # Short-hand for hours_since(1).
67
+ def next_hour
68
+ hours_since(1)
69
+ end
70
+
71
+ # Short-hand for days_ago(1).
72
+ def prev_day
73
+ days_ago(1)
74
+ end
75
+
76
+ # Short-hand for days_since(1).
77
+ def next_day
78
+ days_since(1)
79
+ end
80
+
81
+ # Returns a new Time representing the start of the given day in the previous week (default is Monday).
82
+ def prev_week(day = :monday)
83
+ weeks_ago(1).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
84
+ end
85
+
86
+ # Short-hand for quarters_since(1).beginning_of_quarter.
87
+ def next_quarter
88
+ quarters_since(1).beginning_of_quarter
89
+ end
90
+
91
+ # Short-hand for quarters_ago(1).beginning_of_quarter.
92
+ def prev_quarter
93
+ quarters_ago(1).beginning_of_quarter
94
+ end
95
+
96
+ # Returns a new Time representing the time a number of specified minutes ago.
97
+ def minutes_ago(minutes)
98
+ ago(minutes.minutes)
99
+ end
100
+ alias :mins_ago :minutes_ago
101
+
102
+ # Returns a new Time representing the time a number of specified minutes in the future.
103
+ def minutes_since(minutes)
104
+ since(minutes.minutes)
105
+ end
106
+ alias :mins_since :minutes_since
107
+
108
+ # Returns a new Time representing the time a number of specified hours ago.
109
+ def hours_ago(hours)
110
+ ago(hours.hours)
111
+ end
112
+
113
+ # Returns a new Time representing the time a number of specified hours in the future.
114
+ def hours_since(hours)
115
+ since(hours.hours)
116
+ end
117
+
118
+ # Returns a new Time representing the time a number of specified days ago.
119
+ def days_ago(days)
120
+ ago(days.days)
121
+ end
122
+
123
+ # Returns a new Time representing the time a number of specified days in the future.
124
+ def days_since(days)
125
+ since(days.days)
126
+ end
127
+
128
+ # Returns a new Time representing the time a number of specified weeks ago.
129
+ def weeks_ago(weeks)
130
+ ago(weeks.weeks)
131
+ end
132
+
133
+ # Returns a new Time representing the time a number of specified weeks in the future.
134
+ def weeks_since(weeks)
135
+ since(weeks.weeks)
136
+ end
137
+
138
+ # Returns a new Time representing the time a number of specified quarters (3 months) ago.
139
+ def quarters_ago(quarters)
140
+ ago((quarters * 3).months)
141
+ end
142
+
143
+ # Returns a new Time representing the time a number of specified quarters (3 months) in the future.
144
+ def quarters_since(quarters)
145
+ since((quarters * 3).months)
146
+ end
147
+
148
+ # Returns a new Time representing the end of the unit specified (defaults to second).
149
+ def end_of(unit = :sec)
150
+ send("end_of_#{unit}")
151
+ end
152
+
153
+ # Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9).
154
+ def beginning_of_second
155
+ change(:usec => 0)
156
+ end
157
+ alias :beginning_of_sec :beginning_of_second
158
+ alias :at_beginning_of_sec :beginning_of_second
159
+ alias :at_beginning_of_second :beginning_of_second
160
+
161
+ # Returns a new Time representing the end of the hour, XX:XX:XX.999999 (.999999999 in ruby1.9).
162
+ def end_of_second
163
+ change(:usec => 999999.999)
164
+ end
165
+ alias :end_of_sec :end_of_second
166
+
167
+ # Returns a new Time representing the start of the minute (XX:XX:00).
168
+ def beginning_of_minute
169
+ change(:sec => 0, :usec => 0)
170
+ end
171
+ alias :beginning_of_min :beginning_of_minute
172
+ alias :at_beginning_of_min :beginning_of_minute
173
+ alias :at_beginning_of_minute :beginning_of_minute
174
+
175
+ # Returns a new Time representing the end of the hour, XX:XX:59.999999 (.999999999 in ruby1.9).
176
+ def end_of_minute
177
+ change(:sec => 59, :usec => 999999.999)
178
+ end
179
+ alias :end_of_min :end_of_minute
180
+
181
+ # Returns a new Time representing the start of the hour (XX:00:00).
182
+ def beginning_of_hour
183
+ change(:min => 0, :sec => 0, :usec => 0)
184
+ end
185
+ alias :at_beginning_of_hour :beginning_of_hour
186
+
187
+ # Returns a new Time representing the end of the hour, XX:59:59.999999 (.999999999 in ruby1.9).
188
+ def end_of_hour
189
+ change(:min => 59, :sec => 59, :usec => 999999.999)
190
+ end
191
+
192
+ end
193
+ end
@@ -0,0 +1,12 @@
1
+ class Time
2
+ include TimeExt::MethodChain
3
+ include TimeExt::BackwardsCompatibility
4
+ include TimeExt::Calculations
5
+ include TimeExt::Iterations
6
+
7
+ # Aliases to keep available method names to a standard pattern.
8
+ alias :secs_ago :ago
9
+ alias :seconds_ago :ago
10
+ alias :secs_since :since
11
+ alias :seconds_since :since
12
+ end
@@ -0,0 +1,82 @@
1
+ module TimeExt
2
+ # Allows you to iterate over Time objects with #each and other methods almost as if it was an Array or Hash.
3
+ module Iterations
4
+
5
+ # Used by #each, #map_each and similar methods to iterate over ranges of time.
6
+ def iterate(unit, options = {}, &block)
7
+ options.reverse_merge!(:map_result => false, :beginning_of => false, :include_start => false)
8
+ if block_given?
9
+ units = [:year, :month, :day, :hour, :min, :sec, :usec]
10
+ parent_unit = units[units.index(unit)-1]
11
+ @until ||= (!parent_unit.nil?) ? self.send("#{parent_unit}s_since", 1) : self.send("#{unit}s_since", 1)
12
+ time = self.clone
13
+ direction = (self < @until) ? :f : :b
14
+ succ_method = (direction == :f) ? "next_#{unit}" : "prev_#{unit}"
15
+ time = time.beginning_of(unit) if options[:beginning_of]
16
+ time = time.send(succ_method) if !options[:include_start]
17
+ results = []
18
+ while (direction == :f && time <= @until) || (direction == :b && time >= @until)
19
+ options[:map_result] ? results << yield(time) : yield(time)
20
+ time = time.send(succ_method)
21
+ end
22
+ options[:map_result] ? results : self
23
+ else
24
+ add_to_chain(:iterate, unit, options)
25
+ self
26
+ end
27
+ end
28
+
29
+ # Used togeter with #each and other iteration methods to specify end of interation.
30
+ def until(time, &block)
31
+ time = time.to_time if time.is_a?(::Date)
32
+ @until = time
33
+ return call_chain(block) if block_given?
34
+ self
35
+ end
36
+ alias :till :until
37
+
38
+ # Used together with #each and other interation methods to specify start of iteration, and end will be current object.
39
+ def from(time, &block)
40
+ time = time.to_time if time.is_a?(::Date)
41
+ method, args = @method_chain.pop if block_given?
42
+ if !method.nil?
43
+ time.until(self).send(method, *args, &block)
44
+ else
45
+ time.until(self)
46
+ end
47
+ end
48
+
49
+ # Executes passed block for each "unit" of time specified, with a new time object for each interval passed to the block.
50
+ def each(unit, options = {}, &block)
51
+ iterate(unit, options.merge(:map_result => false), &block)
52
+ end
53
+
54
+ # 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.
55
+ def beginning_of_each(unit, options = {}, &block)
56
+ iterate(unit, options.merge(:map_result => false, :beginning_of => true), &block)
57
+ end
58
+
59
+ # Executes passed block for each "unit" of time specified, returning an array with the return values from the passed block.
60
+ def map_each(unit, options = {}, &block)
61
+ iterate(unit, options.merge(:map_result => true), &block)
62
+ end
63
+ alias :map :map_each
64
+
65
+ # 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".
66
+ def map_beginning_of_each(unit, options = {}, &block)
67
+ iterate(unit, options.merge(:map_result => true, :beginning_of => true), &block)
68
+ end
69
+
70
+ # Dynamically define convenience methods, like #each_hour instead of #each(:hour).
71
+ [:year, :month, :day, :hour, :min, :sec].each do |unit|
72
+ [:each, :beginning_of_each, :map_each, :map_beginning_of_each, :map].each do |method|
73
+ define_method "#{method}_#{unit}" do |*args, &block|
74
+ send(method, unit, *args, &block)
75
+ end
76
+ class_eval { alias :"#{method}_minute" :"#{method}_min" } if unit == :min
77
+ class_eval { alias :"#{method}_second" :"#{method}_sec" } if unit == :sec
78
+ end
79
+ end
80
+
81
+ end
82
+ end
@@ -1,4 +1,5 @@
1
1
  module TimeExt
2
+ # Allows iterators' #until and #from methods to chain back to the parent iteration method.
2
3
  module MethodChain
3
4
 
4
5
  def add_to_chain(method, *args, &block)
data/lib/time_ext.rb CHANGED
@@ -1,13 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'active_support'
3
- require 'time_ext/method_chain'
4
- require 'time_ext/core_ext/time/calculations'
5
- require 'time_ext/core_ext/time/iterations'
6
3
 
7
- module TimeExt
8
-
9
- module Base
10
- TIME_EXT_UNITS = [:year, :month, :day, :hour, :min, :sec, :usec]
11
- end
12
-
13
- end
4
+ require 'time_ext/backwards_compatibility'
5
+ require 'time_ext/calculations'
6
+ require 'time_ext/iterations'
7
+ require 'time_ext/method_chain'
8
+ require 'time_ext/core_ext/time'
@@ -52,6 +52,8 @@ describe "Time Iterations" do
52
52
  match = (1..6).map { |i| @now + i.hours }
53
53
  @now.map_each_hour.until(@now + 6.hours) { |time| time }.should == match
54
54
  @now.until(@now + 6.hours).map_each(:hour) { |time| time }.should == match
55
+ # check so the #map alias for #map_each works
56
+ @now.map_hour.until(@now + 6.hours) { |time| time }.should == match
55
57
  end
56
58
 
57
59
  it "should iterate over time objects backwards with #until set in the past" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_ext
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jim Myhrberg
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-29 00:00:00 +03:00
18
+ date: 2010-08-03 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -82,8 +82,10 @@ files:
82
82
  - VERSION
83
83
  - lib/time/ext.rb
84
84
  - lib/time_ext.rb
85
- - lib/time_ext/core_ext/time/calculations.rb
86
- - lib/time_ext/core_ext/time/iterations.rb
85
+ - lib/time_ext/backwards_compatibility.rb
86
+ - lib/time_ext/calculations.rb
87
+ - lib/time_ext/core_ext/time.rb
88
+ - lib/time_ext/iterations.rb
87
89
  - lib/time_ext/method_chain.rb
88
90
  - spec/spec.opts
89
91
  - spec/spec_helper.rb
@@ -1,206 +0,0 @@
1
- class Time
2
-
3
- # Helper method for backwards compatibility with ActiveSupport.
4
- def days_into_week
5
- defined?(DAYS_INTO_WEEK) ? DAYS_INTO_WEEK : { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
6
- end
7
-
8
- # Helper method for backwards compatibility with ActiveSupport.
9
- def common_year_days_in_month
10
- defined?(COMMON_YEAR_DAYS_IN_MONTH) ? COMMON_YEAR_DAYS_IN_MONTH : [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
11
- end
12
-
13
- # Returns a new Time representing the start of the unit specified (second by default).
14
- def floor(unit = :sec)
15
- self.send("beginning_of_#{unit}")
16
- end
17
- alias :beginning_of :floor
18
- alias :at_beginning_of :floor
19
-
20
- # Returns a new Time representing the start of the next unit specified (second by default).
21
- def ceil(unit = :sec)
22
- self.send("next_#{unit}").send("beginning_of_#{unit}")
23
- end
24
- alias :beginning_of_next :ceil
25
- alias :at_beginning_of_next :ceil
26
-
27
- # Returns a new Time representing the start of the current or next unit specified (second by default) depending which is closest
28
- def round(unit = :sec)
29
- next_unit = self.ceil(unit)
30
- this_unit = self.floor(unit)
31
- (self - this_unit) < (next_unit - self) ? this_unit : next_unit
32
- end
33
- alias :beginning_of_closest :round
34
-
35
- # Returns a new Time representing the previoius unit specified (defaults to second).
36
- def prev(unit = :sec)
37
- send("prev_#{unit}")
38
- end
39
-
40
- # Returns a new Time representing the next unit specified (defaults to second).
41
- def next(unit = :sec)
42
- send("next_#{unit}")
43
- end
44
-
45
- # Short-hand for seconds_ago(1).
46
- def prev_second
47
- seconds_ago(1)
48
- end
49
- alias :prev_sec :prev_second
50
-
51
- # Short-hand for seconds_since(1).
52
- def next_second
53
- seconds_since(1)
54
- end
55
- alias :next_sec :next_second
56
-
57
- # Short-hand for minutes_ago(1).
58
- def prev_minute
59
- minutes_ago(1)
60
- end
61
- alias :prev_min :prev_minute
62
-
63
- # Short-hand for minutes_since(1).
64
- def next_minute
65
- minutes_since(1)
66
- end
67
- alias :next_min :next_minute
68
-
69
- # Short-hand for hours_ago(1).
70
- def prev_hour
71
- hours_ago(1)
72
- end
73
-
74
- # Short-hand for hours_since(1).
75
- def next_hour
76
- hours_since(1)
77
- end
78
-
79
- # Short-hand for days_ago(1).
80
- def prev_day
81
- days_ago(1)
82
- end
83
-
84
- # Short-hand for days_since(1).
85
- def next_day
86
- days_since(1)
87
- end
88
-
89
- # Returns a new Time representing the start of the given day in the previous week (default is Monday).
90
- def prev_week(day = :monday)
91
- weeks_ago(1).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
92
- end
93
-
94
- # Short-hand for quarters_since(1).beginning_of_quarter.
95
- def next_quarter
96
- quarters_since(1).beginning_of_quarter
97
- end
98
-
99
- # Short-hand for quarters_ago(1).beginning_of_quarter.
100
- def prev_quarter
101
- quarters_ago(1).beginning_of_quarter
102
- end
103
-
104
- # Aliases to keep available method names to a standard pattern.
105
- alias :secs_ago :ago
106
- alias :seconds_ago :ago
107
- alias :secs_since :since
108
- alias :seconds_since :since
109
-
110
- # Returns a new Time representing the time a number of specified minutes ago.
111
- def minutes_ago(minutes)
112
- ago(minutes.minutes)
113
- end
114
- alias :mins_ago :minutes_ago
115
-
116
- # Returns a new Time representing the time a number of specified minutes in the future.
117
- def minutes_since(minutes)
118
- since(minutes.minutes)
119
- end
120
- alias :mins_since :minutes_since
121
-
122
- # Returns a new Time representing the time a number of specified hours ago.
123
- def hours_ago(hours)
124
- ago(hours.hours)
125
- end
126
-
127
- # Returns a new Time representing the time a number of specified hours in the future.
128
- def hours_since(hours)
129
- since(hours.hours)
130
- end
131
-
132
- # Returns a new Time representing the time a number of specified days ago.
133
- def days_ago(days)
134
- ago(days.days)
135
- end
136
-
137
- # Returns a new Time representing the time a number of specified days in the future.
138
- def days_since(days)
139
- since(days.days)
140
- end
141
-
142
- # Returns a new Time representing the time a number of specified weeks ago.
143
- def weeks_ago(weeks)
144
- ago(weeks.weeks)
145
- end
146
-
147
- # Returns a new Time representing the time a number of specified weeks in the future.
148
- def weeks_since(weeks)
149
- since(weeks.weeks)
150
- end
151
-
152
- # Returns a new Time representing the time a number of specified quarters (3 months) ago.
153
- def quarters_ago(quarters)
154
- ago((quarters * 3).months)
155
- end
156
-
157
- # Returns a new Time representing the time a number of specified quarters (3 months) in the future.
158
- def quarters_since(quarters)
159
- since((quarters * 3).months)
160
- end
161
-
162
- # Returns a new Time representing the end of the unit specified (defaults to second).
163
- def end_of(unit = :sec)
164
- send("end_of_#{unit}")
165
- end
166
-
167
- # Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9).
168
- def beginning_of_second
169
- change(:usec => 0)
170
- end
171
- alias :beginning_of_sec :beginning_of_second
172
- alias :at_beginning_of_sec :beginning_of_second
173
- alias :at_beginning_of_second :beginning_of_second
174
-
175
- # Returns a new Time representing the end of the hour, XX:XX:XX.999999 (.999999999 in ruby1.9).
176
- def end_of_second
177
- change(:usec => 999999.999)
178
- end
179
- alias :end_of_sec :end_of_second
180
-
181
- # Returns a new Time representing the start of the minute (XX:XX:00).
182
- def beginning_of_minute
183
- change(:sec => 0, :usec => 0)
184
- end
185
- alias :beginning_of_min :beginning_of_minute
186
- alias :at_beginning_of_min :beginning_of_minute
187
- alias :at_beginning_of_minute :beginning_of_minute
188
-
189
- # Returns a new Time representing the end of the hour, XX:XX:59.999999 (.999999999 in ruby1.9).
190
- def end_of_minute
191
- change(:sec => 59, :usec => 999999.999)
192
- end
193
- alias :end_of_min :end_of_minute
194
-
195
- # Returns a new Time representing the start of the hour (XX:00:00).
196
- def beginning_of_hour
197
- change(:min => 0, :sec => 0, :usec => 0)
198
- end
199
- alias :at_beginning_of_hour :beginning_of_hour
200
-
201
- # Returns a new Time representing the end of the hour, XX:59:59.999999 (.999999999 in ruby1.9).
202
- def end_of_hour
203
- change(:min => 59, :sec => 59, :usec => 999999.999)
204
- end
205
-
206
- end
@@ -1,79 +0,0 @@
1
- class Time
2
- include TimeExt::MethodChain
3
-
4
- # Used by #each, #map_each and similar methods to iterate over ranges of time.
5
- def iterate(unit, options = {}, &block)
6
- options.reverse_merge!(:map_result => false, :beginning_of => false, :include_start => false)
7
- if block_given?
8
- units = [:year, :month, :day, :hour, :min, :sec, :usec]
9
- parent_unit = units[units.index(unit)-1]
10
- @until ||= (!parent_unit.nil?) ? self.send("#{parent_unit}s_since", 1) : self.send("#{unit}s_since", 1)
11
- time = self.clone
12
- direction = (self < @until) ? :f : :b
13
- succ_method = (direction == :f) ? "next_#{unit}" : "prev_#{unit}"
14
- time = time.beginning_of(unit) if options[:beginning_of]
15
- time = time.send(succ_method) if !options[:include_start]
16
- results = []
17
- while (direction == :f && time <= @until) || (direction == :b && time >= @until)
18
- options[:map_result] ? results << yield(time) : yield(time)
19
- time = time.send(succ_method)
20
- end
21
- options[:map_result] ? results : self
22
- else
23
- add_to_chain(:iterate, unit, options)
24
- self
25
- end
26
- end
27
-
28
- # Used togeter with #each and other iteration methods to specify end of interation.
29
- def until(time, &block)
30
- time = time.to_time if time.is_a?(::Date)
31
- @until = time
32
- return call_chain(block) if block_given?
33
- self
34
- end
35
- alias :till :until
36
-
37
- # Used together with #each and other interation methods to specify start of iteration, and end will be current object.
38
- def from(time, &block)
39
- time = time.to_time if time.is_a?(::Date)
40
- method, args = @method_chain.pop if block_given?
41
- if !method.nil?
42
- time.until(self).send(method, *args, &block)
43
- else
44
- time.until(self)
45
- end
46
- end
47
-
48
- # Executes passed block for each "unit" of time specified, with a new time object for each interval passed to the block.
49
- def each(unit, options = {}, &block)
50
- iterate(unit, options.merge(:map_result => false), &block)
51
- end
52
-
53
- # 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.
54
- def beginning_of_each(unit, options = {}, &block)
55
- iterate(unit, options.merge(:map_result => false, :beginning_of => true), &block)
56
- end
57
-
58
- # Executes passed block for each "unit" of time specified, returning an array with the return values from the passed block.
59
- def map_each(unit, options = {}, &block)
60
- iterate(unit, options.merge(:map_result => true), &block)
61
- end
62
-
63
- # 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".
64
- def map_beginning_of_each(unit, options = {}, &block)
65
- iterate(unit, options.merge(:map_result => true, :beginning_of => true), &block)
66
- end
67
-
68
- # Dynamically define convenience methods, like #each_hour instead of #each(:hour).
69
- [:year, :month, :day, :hour, :min, :sec].each do |unit|
70
- [:each, :beginning_of_each, :map_each, :map_beginning_of_each].each do |method|
71
- define_method "#{method}_#{unit}" do |*args, &block|
72
- send(method, unit, *args, &block)
73
- end
74
- class_eval { alias :"#{method}_minute" :"#{method}_min" } if unit == :min
75
- class_eval { alias :"#{method}_second" :"#{method}_sec" } if unit == :sec
76
- end
77
- end
78
-
79
- end