timespan 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1 +1 @@
1
- --color
1
+ --color --format nested
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.5.4
2
+
3
+ - Added subclasses LongDurationRange and ShortDurationRange for long and short durations.
data/README.md CHANGED
@@ -5,7 +5,7 @@ Use Timespans in Ruby :)
5
5
  Will calculate time diff between two dates, then allow you to get the time difference in some time unit as a number.
6
6
 
7
7
  ```ruby
8
- t = Timespan.new(:start => Date.today, :duration => 3.days.ago)
8
+ t = Timespan.new(:start => Date.today, :duration => 3.days)
9
9
  t.to_days # => 3
10
10
  t.to_weeks # => 0
11
11
  t.to_secs # => 259200
@@ -324,6 +324,18 @@ dr.between?(4.days) # => true
324
324
 
325
325
  You can also use Range#intersect from *sugar-high* gem to test intersection of time ranges ;)
326
326
 
327
+ The duration range by default supports the following units: [seconds, minutes, hours, days, weeks, months, years]
328
+
329
+ You can subclass the DurationRange to supply your own list of time units to fit your particular scenario.
330
+
331
+ This gem comes with two subclasses:
332
+
333
+ `ShortDurationRange` and `LongDurationRange`
334
+
335
+ They both override the method `#allowed_units` to provide a particular list:
336
+
337
+ `ShortDurationRange` supports time ranges less than a day, whereas `LongDurationRange` only supports time ranges of a day or more.
338
+
327
339
  ## Client side helpers
328
340
 
329
341
  This gem now includes som javascript assets to assist in performing date and timespan (duration) calculations on the client side also:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.5.4
@@ -23,10 +23,25 @@ class DurationRange < DelegateDecorator
23
23
  def initialize range, unit = :minutes
24
24
  range = (0..60) if range.min == nil || range.max == nil
25
25
  super(range, except: %w{to_s to_str})
26
- @unit = unit.to_s.pluralize.to_sym
26
+ unit = unit.to_s.pluralize.to_sym
27
+
28
+ unless allowed_unit? unit
29
+ raise ArgumentError, "Unit #{unit} not valid, only: #{allowed_units} are valid"
30
+ end
31
+
32
+ @unit = unit
33
+
27
34
  @range = range
28
35
  end
29
36
 
37
+ def allowed_unit? unit
38
+ allowed_units.include? unit.to_sym
39
+ end
40
+
41
+ def allowed_units
42
+ [:seconds, :minutes, :hours, :days, :weeks, :months, :years]
43
+ end
44
+
30
45
  def to_str
31
46
  to_s
32
47
  end
@@ -122,10 +137,22 @@ class DurationRange < DelegateDecorator
122
137
  end
123
138
  end
124
139
  end
125
-
126
140
  end
127
141
  end
128
142
 
143
+ class LongDurationRange < DurationRange
144
+ def allowed_units
145
+ [:days, :weeks, :months, :years]
146
+ end
147
+ end
148
+
149
+ class ShortDurationRange < DurationRange
150
+ def allowed_units
151
+ [:seconds, :minutes, :hours]
152
+ end
153
+ end
154
+
155
+
129
156
  class Range
130
157
  [:seconds, :minutes, :hours, :days, :weeks, :months, :years].each do |unit|
131
158
  define_method unit do |type = :duration|
@@ -76,7 +76,7 @@ class Timespan
76
76
  end
77
77
 
78
78
  def custom_specify(name, operator, value, options = {})
79
- puts "custom_specify: #{name}"
79
+ # puts "custom_specify: #{name}"
80
80
  timespan = value.__evolve_to_timespan__
81
81
  case operator
82
82
  when '$gte', '$gt', '$lt', '$lte', '$eq', '$between', '$btw'
@@ -97,7 +97,7 @@ class Timespan
97
97
  query['from']['$gte'] = Serializer.serialize_time(timespan.min)
98
98
  query['to']['$lte'] = Serializer.serialize_time(timespan.max)
99
99
  end
100
- puts "query: #{query}"
100
+ # puts "query: #{query}"
101
101
  query
102
102
  end
103
103
  end
@@ -22,6 +22,12 @@ describe DurationRange do
22
22
 
23
23
  let(:range) { (1..5) }
24
24
 
25
+ context 'invalid range unit' do
26
+ it 'should not allow invalid time unit' do
27
+ expect { DurationRange.new range, :yuppies }.to raise_error
28
+ end
29
+ end
30
+
25
31
  context 'day range' do
26
32
  let (:timerange) { range.days }
27
33
 
@@ -57,4 +63,40 @@ describe DurationRange do
57
63
  its(:max) { should == 5.years }
58
64
  its(:unit) { should == :years }
59
65
  end
66
+ end
67
+
68
+ describe LongDurationRange do
69
+ subject { timerange }
70
+
71
+ let(:range) { (1..5) }
72
+
73
+ context 'invalid range unit' do
74
+ it 'should not allow invalid time unit' do
75
+ expect { LongDurationRange.new range, :hours }.to raise_error
76
+ end
77
+ end
78
+
79
+ context 'valid range unit' do
80
+ it 'should allow valid long time unit' do
81
+ expect { LongDurationRange.new range, :day }.to_not raise_error
82
+ end
83
+ end
84
+ end
85
+
86
+ describe ShortDurationRange do
87
+ subject { timerange }
88
+
89
+ let(:range) { (1..5) }
90
+
91
+ context 'invalid range unit' do
92
+ it 'should not allow invalid time unit' do
93
+ expect { ShortDurationRange.new range, :day }.to raise_error
94
+ end
95
+ end
96
+
97
+ context 'valid range unit' do
98
+ it 'should allow valid long time unit' do
99
+ expect { ShortDurationRange.new range, :hour }.to_not raise_error
100
+ end
101
+ end
60
102
  end
data/timespan.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.5.3"
8
+ s.version = "0.5.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2012-12-12"
12
+ s.date = "2013-01-25"
13
13
  s.description = "Makes it easy to calculate time distance in different units"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".rspec",
22
+ "CHANGELOG.md",
22
23
  "Gemfile",
23
24
  "Gemfile.lock",
24
25
  "LICENSE.txt",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timespan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-12 00:00:00.000000000 Z
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
@@ -245,6 +245,7 @@ extra_rdoc_files:
245
245
  files:
246
246
  - .document
247
247
  - .rspec
248
+ - CHANGELOG.md
248
249
  - Gemfile
249
250
  - Gemfile.lock
250
251
  - LICENSE.txt
@@ -308,7 +309,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
308
309
  version: '0'
309
310
  segments:
310
311
  - 0
311
- hash: -1354784355607378735
312
+ hash: 297766673639236930
312
313
  required_rubygems_version: !ruby/object:Gem::Requirement
313
314
  none: false
314
315
  requirements: