mongoid-time_range 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,2 +1,12 @@
1
+ # 0.3.0 (January 05, 2013)
2
+ * TimeRange is now based on Hash.
3
+
4
+ # 0.2.0 (January 04, 2013)
5
+ * TimeRange does not extend Struct anymore.
6
+
7
+ # 0.1.0 (January 03, 2013)
8
+ * Added integration specs.
9
+ * Mongoid::TimeRange is now available as ::TimeRange too.
10
+
1
11
  # 0.0.1 (January 01, 2013)
2
12
  * First version.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # mongoid-time_range
2
2
 
3
- Mongoid::TimeRange defines a TimeRange type for your Mongoid apps, which is an object with `from` and `to` keys.
3
+ `Mongoid::TimeRange` defines a time range type for your Mongoid apps, which is stored internally as a hash with `from` and `to` keys.
4
4
 
5
5
  ## Installation
6
6
 
@@ -21,9 +21,6 @@ end
21
21
  ```
22
22
 
23
23
  ```ruby
24
- document = Document.create
25
- document.range # => { from: 2013-01-01 00:00:00 +0100, to: nil }
26
-
27
24
  year2013 = Document.create(range: { from: Time.now.at_beginning_of_year, to: Time.now.end_of_year })
28
25
  year2013.range # => { from: 2013-01-01 00:00:00 +0100, to: 2013-12-31 23:59:59 +0100 }
29
26
  ```
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  class TimeRange < Hash
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -8,24 +8,22 @@ module Mongoid
8
8
  merge!(from: from, to: to)
9
9
  end
10
10
 
11
+ [:from, :to].each do |key|
12
+ define_method key do
13
+ self[key]
14
+ end
15
+
16
+ define_method :"#{key}=" do |value|
17
+ self[key] = value
18
+ end
19
+ end
20
+
11
21
  alias_method :to_a, :values
12
22
 
13
23
  def mongoize
14
24
  self.class.mongoize(self)
15
25
  end
16
26
 
17
- def to_h
18
- self
19
- end
20
-
21
- def from
22
- self[:from]
23
- end
24
-
25
- def to
26
- self[:to]
27
- end
28
-
29
27
  class << self
30
28
  def mongoize(object)
31
29
  [:from, :to].associate { |key| Time.mongoize(object[key]) }
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.authors = 'Mario Uher'
8
8
  gem.email = 'uher.mario@gmail.com'
9
9
  gem.homepage = 'https://github.com/haihappen/mongoid-time_range'
10
- gem.summary = 'TimeRange type for Mongoid.'
10
+ gem.summary = 'Time range type for Mongoid.'
11
11
  gem.description = 'Mongoid::TimeRange defines a TimeRange type for your Mongoid apps.'
12
12
 
13
13
  gem.files = `git ls-files`.split("\n")
@@ -13,12 +13,22 @@ describe 'Mongoid::TimeRange integration' do
13
13
  end
14
14
 
15
15
 
16
- describe 'when default is set to ->{ TimeRange.new }' do
17
- subject { DocumentWithDefault.create }
16
+ describe 'when default is set to from: Time.now }' do
17
+ subject { DocumentWithImplicitDefault.create }
18
18
 
19
19
 
20
- it 'sets from to current time and to to nil' do
21
- subject.range.to_h.must_equal from: Time.now, to: nil
20
+ it 'sets from to current time and to to ni' do
21
+ subject.range.must_equal from: Time.now, to: nil
22
+ end
23
+ end
24
+
25
+
26
+ describe 'when default is set to ->{ Mongoid::TimeRange.new(from: Time.now) }' do
27
+ subject { DocumentWithExplicitDefault.create }
28
+
29
+
30
+ it 'sets from to current time and to to ni' do
31
+ subject.range.must_equal from: Time.now, to: nil
22
32
  end
23
33
  end
24
34
  end
@@ -29,7 +39,7 @@ describe 'Mongoid::TimeRange integration' do
29
39
 
30
40
 
31
41
  it 'sets from to given time and to to nil' do
32
- subject.range.to_h.must_equal from: Time.now.beginning_of_year, to: nil
42
+ subject.range.must_equal from: Time.now.beginning_of_year, to: nil
33
43
  end
34
44
  end
35
45
 
@@ -39,7 +49,7 @@ describe 'Mongoid::TimeRange integration' do
39
49
 
40
50
 
41
51
  it 'sets from and to to given time' do
42
- subject.range.to_h.must_equal from: Time.now.beginning_of_year, to: Time.now.end_of_year
52
+ subject.range.must_equal from: Time.now.beginning_of_year, to: Time.now.end_of_year
43
53
  end
44
54
  end
45
55
 
@@ -4,8 +4,14 @@ class Document
4
4
  field :range, type: Mongoid::TimeRange
5
5
  end
6
6
 
7
- class DocumentWithDefault
7
+ class DocumentWithImplicitDefault
8
8
  include Mongoid::Document
9
9
 
10
- field :range, type: Mongoid::TimeRange, default: ->{ TimeRange.new }
10
+ field :range, type: Mongoid::TimeRange, default: ->{ { from: Time.now } }
11
+ end
12
+
13
+ class DocumentWithExplicitDefault
14
+ include Mongoid::Document
15
+
16
+ field :range, type: Mongoid::TimeRange, default: -> { Mongoid::TimeRange.new(Time.now) }
11
17
  end
@@ -54,6 +54,30 @@ describe Mongoid::TimeRange do
54
54
  end
55
55
 
56
56
 
57
+ describe 'getters' do
58
+ it 'responds to from' do
59
+ subject.must_respond_to :from
60
+ end
61
+
62
+
63
+ it 'responds to to' do
64
+ subject.must_respond_to :to
65
+ end
66
+ end
67
+
68
+
69
+ describe 'setters' do
70
+ it 'responds to from=' do
71
+ subject.must_respond_to :from=
72
+ end
73
+
74
+
75
+ it 'responds to to=' do
76
+ subject.must_respond_to :to=
77
+ end
78
+ end
79
+
80
+
57
81
  describe :mongoize do
58
82
  let(:value) { subject.mongoize }
59
83
 
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: mongoid-time_range
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mario Uher
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-05 00:00:00.000000000 Z
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -90,5 +90,5 @@ rubyforge_project:
90
90
  rubygems_version: 1.8.23
91
91
  signing_key:
92
92
  specification_version: 3
93
- summary: TimeRange type for Mongoid.
93
+ summary: Time range type for Mongoid.
94
94
  test_files: []