timerage 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 035b3a7c7620c538f07fb2fa9f9298e5a79979b5
4
- data.tar.gz: 249150193ae3f75b8164c2dcdeb1bc13cc89b6db
3
+ metadata.gz: 5ceefa4559c94982a16c483de5def25fecf657c0
4
+ data.tar.gz: 90f35a70852bc552f75e78c4af721bdcb7297838
5
5
  SHA512:
6
- metadata.gz: 47841e8cd22637790b469a331317ccfc3d030f8b1aac7637245ff7d3a905419395fa385d409a40c94395fad4e12b8d99cc0cfa09977438efc483a97b58b1fd64
7
- data.tar.gz: 4fe79cb39bb708bd6264b49ba3ed46d84464f0a7b1ddd27792e6388f31df1d43981b7c93154c4433d2ed61cc098566996afa100a9c0e7c1d02308a058d911417
6
+ metadata.gz: 02599aeffd1a812a3b0404b0f101946bdbd6fa6815dec2aa2611ed1b02f2958a0468b0adb1d03333233e7d7922aa9cd9a4958e4746636f80f0a4295b3289bc7e
7
+ data.tar.gz: cb0c22a9074efbce775c0b50688121981727028ca1af332ec8c2b70980fe3ad515d5733251e1a1d3924d9b2be0faa1295f3905e18dca63e522a3f2de3f72ca7d
@@ -0,0 +1,51 @@
1
+ require "delegate"
2
+
3
+ module Timerage
4
+ # A range of time. The exposes the Range like interface.
5
+ class TimeInterval < DelegateClass(Range)
6
+ def initialize(*args)
7
+ rng = if rangeish?(args.first)
8
+ args.first
9
+ else
10
+ Range.new(*args)
11
+ end
12
+
13
+ super rng
14
+ end
15
+
16
+ alias_method :to_time, :begin
17
+
18
+ def step(n, &blk)
19
+ if block_given?
20
+ time_enumerator(n).each(&blk)
21
+ else
22
+ time_enumerator(n)
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def rangeish?(an_obj)
29
+ an_obj.respond_to?(:begin) &&
30
+ an_obj.respond_to?(:end)
31
+ end
32
+
33
+ def time_enumerator(step)
34
+ not_done = if exclude_end?
35
+ ->(nxt) { nxt < self.end }
36
+ else
37
+ ->(nxt) { nxt <= self.end }
38
+ end
39
+
40
+ Enumerator.new do |y|
41
+ nxt = self.begin
42
+
43
+ while not_done.call(nxt) do
44
+ y << nxt
45
+
46
+ nxt += step
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module Timerage
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/timerage.rb CHANGED
@@ -1,29 +1,19 @@
1
1
  require "timerage/version"
2
2
 
3
3
  module Timerage
4
+ autoload :TimeInterval, "timerage/time_interval"
5
+
4
6
  refine Range do
5
7
  def step(n, &blk)
6
8
  if self.begin.kind_of?(Time) || self.begin.kind_of?(Date)
7
- if block_given?
8
- time_enumerator(n).each(&blk)
9
- else
10
- time_enumerator(n)
11
- end
9
+ Timerage::TimeInterval.new(self).step(n, &blk)
12
10
  else
13
11
  super
14
12
  end
15
13
  end
16
14
 
17
- def time_enumerator(step)
18
- Enumerator.new do |y|
19
- nxt = self.begin
20
-
21
- while nxt <= self.end do
22
- y << nxt
23
-
24
- nxt += step
25
- end
26
- end
15
+ def to_time_interval
16
+ Timerage::TimeInterval.new(self)
27
17
  end
28
18
  end
29
19
  end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+
3
+ require 'timerage'
@@ -0,0 +1,58 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Timerage::TimeInterval do
4
+ describe "creation" do
5
+ specify { expect(described_class.new(now-1..now)).to be_kind_of described_class }
6
+ specify { expect(described_class.new(now-1, now)).to be_kind_of described_class }
7
+ specify { expect(described_class.new(now-1, now, true)).to be_kind_of described_class }
8
+ specify { expect(described_class.new(now-1, now, false )).to be_kind_of described_class }
9
+ end
10
+
11
+ subject(:interval) { described_class.new(now-duration, now) }
12
+
13
+ it { is_expected.to behave_like_a Range }
14
+ specify { expect(interval.begin).to eq now - duration }
15
+ specify { expect(interval.end).to eq now }
16
+ specify { expect(interval.to_time).to behave_like_a Time }
17
+ specify { expect(interval.to_time).to (be >= interval.begin)
18
+ .and (be <= interval.end) }
19
+ specify { expect{|b| interval.step(1200, &b) }
20
+ .to yield_control.at_least(:once) }
21
+
22
+ context "interval include end" do
23
+ specify { expect(interval.exclude_end?).to be false }
24
+ specify { expect(interval.cover? now).to be true }
25
+ specify { expect(interval.cover? now - duration).to be true }
26
+ specify { expect(interval.cover? now - (duration/2)).to be true }
27
+ specify { expect(interval.cover? now + 1).to be false }
28
+ specify { expect(interval.cover? now - (duration + 1)).to be false }
29
+
30
+ specify { expect{|b| interval.step(1200, &b) }
31
+ .to yield_successive_args now-duration, now-(duration-1200), now-(duration-2400), now }
32
+
33
+ end
34
+
35
+ context "interval exclude end" do
36
+ subject(:interval) { described_class.new(now-duration...now) }
37
+
38
+ specify { expect(interval.exclude_end?).to be true }
39
+ specify { expect(interval.cover? now).to be false }
40
+ specify { expect(interval.cover? now - duration).to be true }
41
+ specify { expect(interval.cover? now - (duration/2)).to be true }
42
+ specify { expect(interval.cover? now + 1).to be false }
43
+ specify { expect(interval.cover? now - (duration + 1)).to be false }
44
+
45
+ specify { expect{|b| interval.step(1200, &b) }
46
+ .to yield_successive_args now-duration, now-(duration-1200), now-(duration-2400) }
47
+
48
+ end
49
+
50
+ let(:now) { Time.now }
51
+ let(:duration) { 3600 }
52
+
53
+ matcher :behave_like_a do |expected|
54
+ match do |actual|
55
+ Set[*expected.instance_methods].subset? Set[*actual.methods]
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe Timerage do
4
+ begin
5
+ using Timerage
6
+
7
+ subject(:range) { Range.new(now-duration, now) }
8
+
9
+ specify { expect(range.to_time_interval).to be_kind_of Timerage::TimeInterval }
10
+ specify { expect{|b| range.step(1200, &b) }.to yield_control.at_least(:once) }
11
+
12
+ context "interval include end" do
13
+ specify { expect{|b| range.step(1200, &b) }
14
+ .to yield_successive_args now-duration, now-(duration-1200), now-(duration-2400), now }
15
+
16
+ end
17
+
18
+ context "interval exclude end" do
19
+ subject (:range) { Range.new(now-duration, now, true) }
20
+
21
+ specify { expect{|b| range.step(1200, &b) }
22
+ .to yield_successive_args now-duration, now-(duration-1200), now-(duration-2400) }
23
+ end
24
+
25
+ let(:now) { Time.now }
26
+ let(:duration) { 3600 }
27
+ end
28
+ end
data/timerage.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", ["> 3.0.0.a", "< 4"]
23
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timerage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Williams
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-07 00:00:00.000000000 Z
12
+ date: 2014-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -39,6 +39,26 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">"
47
+ - !ruby/object:Gem::Version
48
+ version: 3.0.0.a
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '4'
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">"
57
+ - !ruby/object:Gem::Version
58
+ version: 3.0.0.a
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '4'
42
62
  description: Simple refinement to Range to allow Time or Date as arguments
43
63
  email:
44
64
  - cschneider@comverge.com
@@ -52,7 +72,11 @@ files:
52
72
  - README.md
53
73
  - Rakefile
54
74
  - lib/timerage.rb
75
+ - lib/timerage/time_interval.rb
55
76
  - lib/timerage/version.rb
77
+ - spec/spec_helper.rb
78
+ - spec/timerage/time_interval_spec.rb
79
+ - spec/timerage_spec.rb
56
80
  - timerage.gemspec
57
81
  homepage: ''
58
82
  licenses:
@@ -78,4 +102,7 @@ rubygems_version: 2.2.2
78
102
  signing_key:
79
103
  specification_version: 4
80
104
  summary: Simple refinement to Range to allow Time or Date as arguments
81
- test_files: []
105
+ test_files:
106
+ - spec/spec_helper.rb
107
+ - spec/timerage/time_interval_spec.rb
108
+ - spec/timerage_spec.rb