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.
- data/lib/mongoid/time_range/version.rb +2 -2
- data/lib/mongoid/time_range.rb +14 -15
- data/spec/integration_spec.rb +11 -1
- data/spec/support/document.rb +6 -0
- data/spec/time_range_spec.rb +6 -8
- metadata +2 -2
data/lib/mongoid/time_range.rb
CHANGED
@@ -3,29 +3,27 @@ require 'enumerable/associate'
|
|
3
3
|
require 'mongoid'
|
4
4
|
|
5
5
|
module Mongoid
|
6
|
-
class TimeRange
|
7
|
-
|
8
|
-
|
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
|
20
|
-
self
|
17
|
+
def to_h
|
18
|
+
self
|
21
19
|
end
|
22
20
|
|
23
|
-
def
|
24
|
-
|
21
|
+
def from
|
22
|
+
self[:from]
|
25
23
|
end
|
26
24
|
|
27
|
-
def
|
28
|
-
[
|
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
|
-
|
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(
|
40
|
+
new.merge(hash)
|
42
41
|
end
|
43
42
|
end
|
44
43
|
end
|
data/spec/integration_spec.rb
CHANGED
@@ -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.
|
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
|
|
data/spec/support/document.rb
CHANGED
data/spec/time_range_spec.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
3
|
describe Mongoid::TimeRange do
|
4
|
-
|
5
|
-
let(:
|
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.
|
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-
|
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
|