mongoid-time_range 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
- class TimeRange
3
- VERSION = '0.2.0'
2
+ class TimeRange < Hash
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -3,29 +3,27 @@ require 'enumerable/associate'
3
3
  require 'mongoid'
4
4
 
5
5
  module Mongoid
6
- class TimeRange
7
- attr_accessor :from, :to
8
- delegate :inspect, :[], to: :to_h
9
-
10
- def initialize(from = nil, to = nil)
11
- self.from = from || Time.now
12
- self.to = to
6
+ class TimeRange < Hash
7
+ def initialize(from = Time.now, to = nil)
8
+ merge!(from: from, to: to)
13
9
  end
14
10
 
11
+ alias_method :to_a, :values
12
+
15
13
  def mongoize
16
14
  self.class.mongoize(self)
17
15
  end
18
16
 
19
- def ==(other)
20
- self.from == other.from && self.to == other.to
17
+ def to_h
18
+ self
21
19
  end
22
20
 
23
- def to_h
24
- { from: from, to: to }
21
+ def from
22
+ self[:from]
25
23
  end
26
24
 
27
- def to_a
28
- [from, to]
25
+ def to
26
+ self[:to]
29
27
  end
30
28
 
31
29
  class << self
@@ -34,11 +32,12 @@ module Mongoid
34
32
  end
35
33
 
36
34
  def demongoize(hash)
37
- hash ||= {}
35
+ return nil if hash.nil?
36
+
38
37
  hash = hash.symbolize_keys
39
38
  hash = [:from, :to].associate { |key| Time.demongoize(hash[key]) }
40
39
 
41
- new(*hash.values)
40
+ new.merge(hash)
42
41
  end
43
42
  end
44
43
  end
@@ -9,7 +9,17 @@ describe 'Mongoid::TimeRange integration' do
9
9
 
10
10
 
11
11
  it 'sets from to current time and to to nil' do
12
- subject.range.to_h.must_equal from: Time.now, to: nil
12
+ subject.range.must_be_nil
13
+ end
14
+
15
+
16
+ describe 'when default is set to ->{ TimeRange.new }' do
17
+ subject { DocumentWithDefault.create }
18
+
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
22
+ end
13
23
  end
14
24
  end
15
25
 
@@ -3,3 +3,9 @@ class Document
3
3
 
4
4
  field :range, type: Mongoid::TimeRange
5
5
  end
6
+
7
+ class DocumentWithDefault
8
+ include Mongoid::Document
9
+
10
+ field :range, type: Mongoid::TimeRange, default: ->{ TimeRange.new }
11
+ end
@@ -1,15 +1,13 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe Mongoid::TimeRange do
4
- let(:from) { Time.now }
5
- let(:to) { from + 1.day }
4
+ before { Timecop.freeze }
5
+ let(:from) { Time.now.beginning_of_year }
6
+ let(:to) { Time.now.end_of_year }
6
7
  subject { Mongoid::TimeRange.new(from, to) }
7
8
 
8
9
 
9
10
  describe :initialize do
10
- before { Timecop.freeze }
11
-
12
-
13
11
  describe 'without any arguments' do
14
12
  subject { Mongoid::TimeRange.new }
15
13
 
@@ -53,9 +51,6 @@ describe Mongoid::TimeRange do
53
51
  subject.to.must_equal to
54
52
  end
55
53
  end
56
-
57
-
58
- after { Timecop.return }
59
54
  end
60
55
 
61
56
 
@@ -157,4 +152,7 @@ describe Mongoid::TimeRange do
157
152
  value.must_equal [from, to]
158
153
  end
159
154
  end
155
+
156
+
157
+ after { Timecop.return }
160
158
  end
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.2.0
5
+ version: 0.3.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-04 00:00:00.000000000 Z
12
+ date: 2013-01-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement