mongoid-time_range 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -6
- data/lib/mongoid/time_range.rb +10 -8
- data/lib/mongoid/time_range/version.rb +1 -3
- data/mongoid-time_range.gemspec +1 -0
- data/spec/integration_spec.rb +3 -6
- data/spec/support/time.rb +6 -0
- metadata +19 -2
data/README.md
CHANGED
@@ -10,12 +10,6 @@ In your Gemfile:
|
|
10
10
|
gem 'mongoid-time_range'
|
11
11
|
```
|
12
12
|
|
13
|
-
If you want to use ::TimeRange instead of Mongoid::TimeRange, your Gemfile must include a additional require statement:
|
14
|
-
|
15
|
-
```ruby
|
16
|
-
gem 'mongoid-time_range', require: 'mongoid/time_range/global'
|
17
|
-
```
|
18
|
-
|
19
13
|
## Usage
|
20
14
|
|
21
15
|
```ruby
|
@@ -34,6 +28,22 @@ year2013 = Document.create(range: { from: Time.now.at_beginning_of_year, to: Tim
|
|
34
28
|
year2013.range # => { from: 2013-01-01 00:00:00 +0100, to: 2013-12-31 23:59:59 +0100 }
|
35
29
|
```
|
36
30
|
|
31
|
+
If you want to use `TimeRange` directly instead of `Mongoid::TimeRange`:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Document
|
35
|
+
include Mongoid::Document
|
36
|
+
|
37
|
+
field :range, type: TimeRange
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
… your Gemfile must include a additional require statement:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
gem 'mongoid-time_range', require: 'mongoid/time_range/global'
|
45
|
+
```
|
46
|
+
|
37
47
|
## Contributing
|
38
48
|
|
39
49
|
1. Fork it
|
data/lib/mongoid/time_range.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
+
require 'active_support/all'
|
1
2
|
require 'enumerable/associate'
|
3
|
+
require 'mongoid'
|
2
4
|
|
3
5
|
module Mongoid
|
4
|
-
class TimeRange
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
8
13
|
end
|
9
14
|
|
10
15
|
def mongoize
|
@@ -23,10 +28,6 @@ module Mongoid
|
|
23
28
|
[from, to]
|
24
29
|
end
|
25
30
|
|
26
|
-
def inspect
|
27
|
-
to_h.inspect
|
28
|
-
end
|
29
|
-
|
30
31
|
class << self
|
31
32
|
def mongoize(object)
|
32
33
|
[:from, :to].associate { |key| Time.mongoize(object[key]) }
|
@@ -34,6 +35,7 @@ module Mongoid
|
|
34
35
|
|
35
36
|
def demongoize(hash)
|
36
37
|
hash ||= {}
|
38
|
+
hash = hash.symbolize_keys
|
37
39
|
hash = [:from, :to].associate { |key| Time.demongoize(hash[key]) }
|
38
40
|
|
39
41
|
new(*hash.values)
|
data/mongoid-time_range.gemspec
CHANGED
data/spec/integration_spec.rb
CHANGED
@@ -9,8 +9,7 @@ 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.
|
13
|
-
subject.range.to.must_be_nil
|
12
|
+
subject.range.to_h.must_equal from: Time.now, to: nil
|
14
13
|
end
|
15
14
|
end
|
16
15
|
|
@@ -20,8 +19,7 @@ describe 'Mongoid::TimeRange integration' do
|
|
20
19
|
|
21
20
|
|
22
21
|
it 'sets from to given time and to to nil' do
|
23
|
-
subject.range.
|
24
|
-
subject.range.to.must_be_nil
|
22
|
+
subject.range.to_h.must_equal from: Time.now.beginning_of_year, to: nil
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
@@ -31,8 +29,7 @@ describe 'Mongoid::TimeRange integration' do
|
|
31
29
|
|
32
30
|
|
33
31
|
it 'sets from and to to given time' do
|
34
|
-
subject.range.
|
35
|
-
subject.range.to.to_i.must_equal Time.now.end_of_year.to_i
|
32
|
+
subject.range.to_h.must_equal from: Time.now.beginning_of_year, to: Time.now.end_of_year
|
36
33
|
end
|
37
34
|
end
|
38
35
|
|
metadata
CHANGED
@@ -2,15 +2,31 @@
|
|
2
2
|
name: mongoid-time_range
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.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-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
none: false
|
21
|
+
name: activesupport
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
none: false
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
version_requirements: !ruby/object:Gem::Requirement
|
16
32
|
requirements:
|
@@ -49,6 +65,7 @@ files:
|
|
49
65
|
- spec/support/connection.rb
|
50
66
|
- spec/support/document.rb
|
51
67
|
- spec/support/mongoid.yml
|
68
|
+
- spec/support/time.rb
|
52
69
|
- spec/time_range_spec.rb
|
53
70
|
homepage: https://github.com/haihappen/mongoid-time_range
|
54
71
|
licenses: []
|