mongoid-time_range 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +25 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +3 -0
- data/README.md +62 -0
- data/Rakefile +7 -0
- data/lib/enumerable/associate.rb +29 -0
- data/lib/mongoid/time_range.rb +33 -0
- data/lib/mongoid/time_range/version.rb +3 -0
- data/mongoid-time_range.gemspec +21 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/connection.rb +1 -0
- data/spec/support/document.rb +9 -0
- data/spec/support/mongoid.yml +8 -0
- data/spec/time_range_spec.rb +160 -0
- metadata +122 -0
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Because this is a gem, ignore Gemfile.lock:
|
2
|
+
|
3
|
+
Gemfile.lock
|
4
|
+
|
5
|
+
# And because this is Ruby, ignore the following
|
6
|
+
# (source: https://github.com/github/gitignore/blob/master/Ruby.gitignore):
|
7
|
+
|
8
|
+
*.gem
|
9
|
+
*.rbc
|
10
|
+
.bundle
|
11
|
+
.config
|
12
|
+
coverage
|
13
|
+
InstalledFiles
|
14
|
+
lib/bundler/man
|
15
|
+
pkg
|
16
|
+
rdoc
|
17
|
+
spec/reports
|
18
|
+
test/tmp
|
19
|
+
test/version_tmp
|
20
|
+
tmp
|
21
|
+
|
22
|
+
# YARD artifacts
|
23
|
+
.yardoc
|
24
|
+
_yardoc
|
25
|
+
doc/
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# mongoid-time_range
|
2
|
+
|
3
|
+
Mongoid::TimeRange defines a TimeRange type for your Mongoid apps, which is an object with `from` and `to` keys.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
In your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mongoid-time_range'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class Document
|
17
|
+
include Mongoid::Document
|
18
|
+
|
19
|
+
field :range, type: Mongoid::TimeRange
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
document = Document.create
|
25
|
+
document.range # =>
|
26
|
+
|
27
|
+
year2013 = Document.create(range: { from: Time.now.at_beginning_of_year, to: Time.now.end_of_year })
|
28
|
+
year2013.range # =>
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
38
|
+
|
39
|
+
## Copyright
|
40
|
+
|
41
|
+
(The MIT license)
|
42
|
+
|
43
|
+
Copyright (c) 2013 Mario Uher
|
44
|
+
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
46
|
+
a copy of this software and associated documentation files (the
|
47
|
+
"Software"), to deal in the Software without restriction, including
|
48
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
49
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
50
|
+
permit persons to whom the Software is furnished to do so, subject to
|
51
|
+
the following conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
58
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
59
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
60
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
61
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
62
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Enumerable # Remove me as soon as it is integrated into ActiveSupport.
|
2
|
+
# Associates keys with values and returns a Hash.
|
3
|
+
#
|
4
|
+
# If you have an enumerable of keys and want to associate them with values,
|
5
|
+
# pass a block that returns a value for the key:
|
6
|
+
#
|
7
|
+
# [1, 2, 3].associate { |i| i ** 2 }
|
8
|
+
# # => { 1 => 1, 2 => 4, 3 => 9 }
|
9
|
+
#
|
10
|
+
# %w( tender love ).associate &:capitalize
|
11
|
+
# # => {"tender"=>"Tender", "love"=>"Love"}
|
12
|
+
#
|
13
|
+
# If you have an enumerable key/value pairs and want to associate them,
|
14
|
+
# omit the block and you'll get a hash in return:
|
15
|
+
#
|
16
|
+
# [[1, 2], [3, 4]].associate
|
17
|
+
# # => { 1 => 2, 3 => 4 }
|
18
|
+
def associate(mapping = {})
|
19
|
+
if block_given?
|
20
|
+
each_with_object(mapping) do |key, object|
|
21
|
+
object[key] = yield(key)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
each_with_object(mapping) do |(key, value), object|
|
25
|
+
object[key] = value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'enumerable/associate'
|
2
|
+
require 'mongoid'
|
3
|
+
|
4
|
+
class Mongoid::TimeRange < Struct.new(:from, :to)
|
5
|
+
def initialize(*)
|
6
|
+
super
|
7
|
+
self.from ||= Time.now
|
8
|
+
end
|
9
|
+
|
10
|
+
def mongoize
|
11
|
+
Mongoid::TimeRange.mongoize(self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def ==(other)
|
15
|
+
self.from == other.from && self.to == other.to
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_a
|
19
|
+
[from, to]
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def mongoize(object)
|
24
|
+
%w[from to].associate { |key| object[key.to_sym].mongoize }
|
25
|
+
end
|
26
|
+
|
27
|
+
def demongoize(hash)
|
28
|
+
times = hash.values.map { |time| Time.demongoize(time) }
|
29
|
+
|
30
|
+
Mongoid::TimeRange.new(*times)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$: << File.expand_path('../lib', __FILE__)
|
2
|
+
# require 'mongoid/time_range/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'mongoid-time_range'
|
6
|
+
gem.version = '0.0.1'
|
7
|
+
gem.authors = 'Mario Uher'
|
8
|
+
gem.email = 'uher.mario@gmail.com'
|
9
|
+
gem.homepage = 'https://github.com/haihappen/mongoid-time_range'
|
10
|
+
gem.summary = 'TimeRange type for Mongoid.'
|
11
|
+
gem.description = 'Mongoid::TimeRange defines a TimeRange type for your Mongoid apps.'
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split("\n")
|
14
|
+
gem.require_path = 'lib'
|
15
|
+
|
16
|
+
gem.add_dependency 'mongoid'
|
17
|
+
|
18
|
+
gem.add_development_dependency 'minitest'
|
19
|
+
gem.add_development_dependency 'rake'
|
20
|
+
gem.add_development_dependency 'timecop'
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$: << File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'minitest/pride'
|
5
|
+
require 'minitest/spec'
|
6
|
+
|
7
|
+
require 'mongoid/time_range'
|
8
|
+
|
9
|
+
require 'timecop'
|
10
|
+
|
11
|
+
# Load support *.rb files in ./support
|
12
|
+
Dir[File.expand_path('../support/*.rb', __FILE__)].each { |file| require_relative file }
|
@@ -0,0 +1 @@
|
|
1
|
+
Mongoid.load!(File.expand_path('../mongoid.yml', __FILE__), 'test')
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::TimeRange do
|
4
|
+
let(:from) { Time.now }
|
5
|
+
let(:to) { from + 1.day }
|
6
|
+
subject { Mongoid::TimeRange.new(from, to) }
|
7
|
+
|
8
|
+
|
9
|
+
describe :initialize do
|
10
|
+
before { Timecop.freeze }
|
11
|
+
|
12
|
+
|
13
|
+
describe 'without any arguments' do
|
14
|
+
subject { Mongoid::TimeRange.new }
|
15
|
+
|
16
|
+
|
17
|
+
it 'sets from to Time.now' do
|
18
|
+
subject.from.must_equal Time.now
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
it 'sets to to nil' do
|
23
|
+
subject.to.must_be_nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
describe 'with one argument' do
|
29
|
+
subject { Mongoid::TimeRange.new(from) }
|
30
|
+
|
31
|
+
|
32
|
+
it 'sets from to given value' do
|
33
|
+
subject.from.must_equal from
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
it 'sets to to nil' do
|
38
|
+
subject.to.must_be_nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
describe 'with two arguments' do
|
44
|
+
subject { Mongoid::TimeRange.new(from, to) }
|
45
|
+
|
46
|
+
|
47
|
+
it 'sets from to given value' do
|
48
|
+
subject.from.must_equal from
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
it 'sets to to given value' do
|
53
|
+
subject.to.must_equal to
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
after { Timecop.return }
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
describe :mongoize do
|
63
|
+
let(:value) { subject.mongoize }
|
64
|
+
|
65
|
+
|
66
|
+
describe 'having both from and to' do
|
67
|
+
it 'returns database friendly value' do
|
68
|
+
value.must_equal 'from' => from.mongoize, 'to' => to.mongoize
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
describe 'having only from value' do
|
74
|
+
let(:to) { nil }
|
75
|
+
|
76
|
+
|
77
|
+
it 'returns database friendly value' do
|
78
|
+
value.must_equal 'from' => from.mongoize, 'to' => nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
describe 'self.mongoize' do
|
85
|
+
describe 'giving a time range object' do
|
86
|
+
let(:value) { Mongoid::TimeRange.mongoize(subject) }
|
87
|
+
|
88
|
+
|
89
|
+
describe 'having both from and to' do
|
90
|
+
it 'returns database friendly value' do
|
91
|
+
value.must_equal 'from' => from.mongoize, 'to' => to.mongoize
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
describe 'having only from value' do
|
97
|
+
let(:to) { nil }
|
98
|
+
|
99
|
+
|
100
|
+
it 'returns database friendly value' do
|
101
|
+
value.must_equal 'from' => from.mongoize, 'to' => nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
describe 'giving a hash' do
|
108
|
+
let(:value) { Mongoid::TimeRange.mongoize(from: from, to: to) }
|
109
|
+
|
110
|
+
|
111
|
+
describe 'having both from and to' do
|
112
|
+
it 'returns database friendly value' do
|
113
|
+
value.must_equal 'from' => from.mongoize, 'to' => to.mongoize
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
describe 'having only from value' do
|
119
|
+
let(:to) { nil }
|
120
|
+
|
121
|
+
|
122
|
+
it 'returns database friendly value' do
|
123
|
+
value.must_equal 'from' => from.mongoize, 'to' => nil
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
describe 'self.demongoize' do
|
131
|
+
let(:value) { Mongoid::TimeRange.demongoize('from' => from, 'to' => to) }
|
132
|
+
|
133
|
+
|
134
|
+
describe 'having both from and to' do
|
135
|
+
it 'returns a TimeRange object' do
|
136
|
+
value.must_equal Mongoid::TimeRange.new(from, to)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
describe 'having only from value' do
|
142
|
+
let(:to) { nil }
|
143
|
+
|
144
|
+
|
145
|
+
it 'returns database friendly value' do
|
146
|
+
value.must_equal Mongoid::TimeRange.new(from)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
describe :to_a do
|
153
|
+
let(:value) { subject.to_a }
|
154
|
+
|
155
|
+
|
156
|
+
it 'returns an array' do
|
157
|
+
value.must_equal [from, to]
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-time_range
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mario Uher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-01 00:00:00.000000000 Z
|
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: mongoid
|
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
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
none: false
|
37
|
+
name: minitest
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
none: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
none: false
|
53
|
+
name: rake
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
none: false
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
none: false
|
69
|
+
name: timecop
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
none: false
|
78
|
+
description: Mongoid::TimeRange defines a TimeRange type for your Mongoid apps.
|
79
|
+
email: uher.mario@gmail.com
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files: []
|
83
|
+
files:
|
84
|
+
- .gitignore
|
85
|
+
- CHANGELOG.md
|
86
|
+
- Gemfile
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- lib/enumerable/associate.rb
|
90
|
+
- lib/mongoid/time_range.rb
|
91
|
+
- lib/mongoid/time_range/version.rb
|
92
|
+
- mongoid-time_range.gemspec
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/support/connection.rb
|
95
|
+
- spec/support/document.rb
|
96
|
+
- spec/support/mongoid.yml
|
97
|
+
- spec/time_range_spec.rb
|
98
|
+
homepage: https://github.com/haihappen/mongoid-time_range
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
none: false
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
none: false
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.23
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: TimeRange type for Mongoid.
|
122
|
+
test_files: []
|