time_ext 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/time_ext/core_ext/time.rb +1 -2
- data/lib/time_ext/iterations.rb +25 -3
- data/lib/time_ext/support.rb +27 -0
- data/lib/time_ext.rb +1 -2
- data/spec/time_iterations_spec.rb +29 -1
- metadata +5 -6
- data/lib/time_ext/backwards_compatibility.rb +0 -14
- data/lib/time_ext/method_chain.rb +0 -19
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/lib/time_ext/iterations.rb
CHANGED
@@ -4,16 +4,23 @@ module TimeExt
|
|
4
4
|
|
5
5
|
# Used by #each, #map_each and similar methods to iterate over ranges of time.
|
6
6
|
def iterate(unit, options = {}, &block)
|
7
|
-
options.reverse_merge!(:map_result => false, :beginning_of => false, :include_start => false)
|
7
|
+
options.reverse_merge!(:map_result => false, :beginning_of => false, :include_start => false, :include_end => true)
|
8
8
|
if block_given?
|
9
9
|
units = [:year, :month, :day, :hour, :min, :sec, :usec]
|
10
10
|
parent_unit = units[units.index(unit)-1]
|
11
|
-
@
|
12
|
-
|
11
|
+
if @of_the.nil?
|
12
|
+
time = self.clone
|
13
|
+
@until ||= (!parent_unit.nil?) ? self.send("#{parent_unit}s_since", 1) : self.send("#{unit}s_since", 1)
|
14
|
+
else
|
15
|
+
time = self.beginning_of(@of_the)
|
16
|
+
@until = self.next(@of_the).beginning_of(@of_the)
|
17
|
+
options.merge!(:beginning_of => true, :include_start => true, :include_end => false)
|
18
|
+
end
|
13
19
|
direction = (self < @until) ? :f : :b
|
14
20
|
succ_method = (direction == :f) ? "next_#{unit}" : "prev_#{unit}"
|
15
21
|
time = time.beginning_of(unit) if options[:beginning_of]
|
16
22
|
time = time.send(succ_method) if !options[:include_start]
|
23
|
+
@until = @until.prev(unit).end_of(unit) if !options[:include_end]
|
17
24
|
results = []
|
18
25
|
while (direction == :f && time <= @until) || (direction == :b && time >= @until)
|
19
26
|
options[:map_result] ? results << yield(time) : yield(time)
|
@@ -46,6 +53,14 @@ module TimeExt
|
|
46
53
|
end
|
47
54
|
end
|
48
55
|
|
56
|
+
# Let's you iterate over every unit specified in the #each or #map call for the specified unit.
|
57
|
+
def of_the(unit, &block)
|
58
|
+
@of_the = unit
|
59
|
+
return call_chain(block) if block_given?
|
60
|
+
self
|
61
|
+
end
|
62
|
+
alias :of :of_the
|
63
|
+
|
49
64
|
# Executes passed block for each "unit" of time specified, with a new time object for each interval passed to the block.
|
50
65
|
def each(unit, options = {}, &block)
|
51
66
|
iterate(unit, options.merge(:map_result => false), &block)
|
@@ -76,6 +91,13 @@ module TimeExt
|
|
76
91
|
class_eval { alias :"#{method}_minute" :"#{method}_min" } if unit == :min
|
77
92
|
class_eval { alias :"#{method}_second" :"#{method}_sec" } if unit == :sec
|
78
93
|
end
|
94
|
+
[:of_the, :of].each do |method|
|
95
|
+
define_method "#{method}_#{unit}" do |*args, &block|
|
96
|
+
send(method, unit, *args, &block)
|
97
|
+
end
|
98
|
+
class_eval { alias :"#{method}_minute" :"#{method}_min" } if unit == :min
|
99
|
+
class_eval { alias :"#{method}_second" :"#{method}_sec" } if unit == :sec
|
100
|
+
end
|
79
101
|
end
|
80
102
|
|
81
103
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module TimeExt
|
2
|
+
# Provides helper methods used by TimeExt::Calculations for backwards compatibility with ActiveSupport, and method chaining helpers for TimeExt::Iterations.
|
3
|
+
module Support
|
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
|
+
def add_to_chain(method, *args, &block)
|
14
|
+
@method_chain ||= []
|
15
|
+
@method_chain << [method.to_sym, args, block]
|
16
|
+
end
|
17
|
+
|
18
|
+
def call_chain(custom_block = nil, &block)
|
19
|
+
method, args, iblock = @method_chain.pop
|
20
|
+
return nil if method.nil?
|
21
|
+
iblock = custom_block if !custom_block.nil?
|
22
|
+
method, args, iblock = yield(method, args, iblock) if block_given?
|
23
|
+
self.send(method, *args, &iblock)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/time_ext.rb
CHANGED
@@ -68,4 +68,32 @@ describe "Time Iterations" do
|
|
68
68
|
(@now + 6.hours).from(@now).map_each(:hour) { |time| time }.should == match
|
69
69
|
end
|
70
70
|
|
71
|
-
|
71
|
+
it "should iterate over time objects with #map_each and #of_the via method chaining" do
|
72
|
+
match = (0..23).map { |i| ((@now - (@now.hour).hours) + i.hours).beginning_of_hour }
|
73
|
+
@now.map_each_hour.of_the(:day) { |time| time }.should == match
|
74
|
+
@now.map_each_hour.of_the_day { |time| time }.should == match
|
75
|
+
match = @now.beginning_of_month.map_beginning_of_each_hour(:include_start => true, :include_end => false).until(@now.next_month.beginning_of_month) { |time| time }
|
76
|
+
@now.map_each_hour.of_the(:month) { |time| time }.should == match
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should iterate and respect the include_start and include_end options" do
|
80
|
+
match = (1..6).map { |i| @now + i.hours }
|
81
|
+
@now.map_each_hour.until(@now + 6.hours) { |time| time }.should == match
|
82
|
+
match = (1..6).map { |i| @now + i.hours }
|
83
|
+
@now.map_each_hour(:include_end => true).until(@now + 6.hours) { |time| time }.should == match
|
84
|
+
@now.map_each_hour(:include_start => false).until(@now + 6.hours) { |time| time }.should == match
|
85
|
+
match = (0..6).map { |i| @now + i.hours }
|
86
|
+
@now.map_each_hour(:include_start => true).until(@now + 6.hours) { |time| time }.should == match
|
87
|
+
match = (0..5).map { |i| @now + i.hours }
|
88
|
+
@now.map_each_hour(:include_start => true, :include_end => false).until(@now + 6.hours) { |time| time }.should == match
|
89
|
+
match = (0..6).map { |i| @now + i.hours }
|
90
|
+
@now.map_each_hour(:include_start => true, :include_end => true).until(@now + 6.hours) { |time| time }.should == match
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should iterate and respect the beginning_of option" do
|
94
|
+
match = (1..6).map { |i| @now.beginning_of_hour + i.hours }
|
95
|
+
@now.map_beginning_of_each_hour.until(@now.beginning_of_hour + 6.hours) { |time| time }.should == match
|
96
|
+
@now.map_each_hour(:beginning_of => true).until(@now.beginning_of_hour + 6.hours) { |time| time }.should == match
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
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:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 3
|
10
|
+
version: 0.2.3
|
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-08-
|
18
|
+
date: 2010-08-05 00:00:00 +03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -82,11 +82,10 @@ files:
|
|
82
82
|
- VERSION
|
83
83
|
- lib/time/ext.rb
|
84
84
|
- lib/time_ext.rb
|
85
|
-
- lib/time_ext/backwards_compatibility.rb
|
86
85
|
- lib/time_ext/calculations.rb
|
87
86
|
- lib/time_ext/core_ext/time.rb
|
88
87
|
- lib/time_ext/iterations.rb
|
89
|
-
- lib/time_ext/
|
88
|
+
- lib/time_ext/support.rb
|
90
89
|
- spec/spec.opts
|
91
90
|
- spec/spec_helper.rb
|
92
91
|
- spec/time_calculations_spec.rb
|
@@ -1,14 +0,0 @@
|
|
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
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module TimeExt
|
2
|
-
# Allows iterators' #until and #from methods to chain back to the parent iteration method.
|
3
|
-
module MethodChain
|
4
|
-
|
5
|
-
def add_to_chain(method, *args, &block)
|
6
|
-
@method_chain ||= []
|
7
|
-
@method_chain << [method.to_sym, args, block]
|
8
|
-
end
|
9
|
-
|
10
|
-
def call_chain(custom_block = nil, &block)
|
11
|
-
method, args, iblock = @method_chain.pop
|
12
|
-
return nil if method.nil?
|
13
|
-
iblock = custom_block if !custom_block.nil?
|
14
|
-
method, args, iblock = yield(method, args, iblock) if block_given?
|
15
|
-
self.send(method, *args, &iblock)
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|