mongoid-time_range 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source :rubygems
2
2
 
3
3
  gemspec
4
+
5
+ gem 'minitest'
6
+ gem 'rake'
7
+ gem 'timecop'
data/README.md CHANGED
@@ -10,6 +10,12 @@ 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
+
13
19
  ## Usage
14
20
 
15
21
  ```ruby
@@ -22,10 +28,10 @@ end
22
28
 
23
29
  ```ruby
24
30
  document = Document.create
25
- document.range # =>
31
+ document.range # => { from: 2013-01-01 00:00:00 +0100, to: nil }
26
32
 
27
33
  year2013 = Document.create(range: { from: Time.now.at_beginning_of_year, to: Time.now.end_of_year })
28
- year2013.range # =>
34
+ year2013.range # => { from: 2013-01-01 00:00:00 +0100, to: 2013-12-31 23:59:59 +0100 }
29
35
  ```
30
36
 
31
37
  ## Contributing
@@ -1,4 +1,4 @@
1
- module Enumerable # Remove me as soon as it is integrated into ActiveSupport.
1
+ module Enumerable # Remove this as soon as it is integrated into ActiveSupport.
2
2
  # Associates keys with values and returns a Hash.
3
3
  #
4
4
  # If you have an enumerable of keys and want to associate them with values,
@@ -1,33 +1,43 @@
1
1
  require 'enumerable/associate'
2
- require 'mongoid'
3
2
 
4
- class Mongoid::TimeRange < Struct.new(:from, :to)
5
- def initialize(*)
6
- super
7
- self.from ||= Time.now
8
- end
3
+ module Mongoid
4
+ class TimeRange < Struct.new(:from, :to)
5
+ def initialize(*)
6
+ super
7
+ self.from ||= Time.now
8
+ end
9
9
 
10
- def mongoize
11
- Mongoid::TimeRange.mongoize(self)
12
- end
10
+ def mongoize
11
+ self.class.mongoize(self)
12
+ end
13
13
 
14
- def ==(other)
15
- self.from == other.from && self.to == other.to
16
- end
14
+ def ==(other)
15
+ self.from == other.from && self.to == other.to
16
+ end
17
17
 
18
- def to_a
19
- [from, to]
20
- end
18
+ def to_h
19
+ { from: from, to: to }
20
+ end
21
21
 
22
- class << self
23
- def mongoize(object)
24
- %w[from to].associate { |key| object[key.to_sym].mongoize }
22
+ def to_a
23
+ [from, to]
25
24
  end
26
25
 
27
- def demongoize(hash)
28
- times = hash.values.map { |time| Time.demongoize(time) }
29
-
30
- Mongoid::TimeRange.new(*times)
26
+ def inspect
27
+ to_h.inspect
28
+ end
29
+
30
+ class << self
31
+ def mongoize(object)
32
+ [:from, :to].associate { |key| Time.mongoize(object[key]) }
33
+ end
34
+
35
+ def demongoize(hash)
36
+ hash ||= {}
37
+ hash = [:from, :to].associate { |key| Time.demongoize(hash[key]) }
38
+
39
+ new(*hash.values)
40
+ end
31
41
  end
32
42
  end
33
- end
43
+ end
@@ -0,0 +1,3 @@
1
+ require 'mongoid/time_range'
2
+
3
+ TimeRange = Mongoid::TimeRange
@@ -1,3 +1,7 @@
1
- class Mongoid::TimeRange
2
- VERSION = '0.0.1'
1
+ require 'mongoid/time_range'
2
+
3
+ module Mongoid
4
+ class TimeRange
5
+ VERSION = '0.1.0'
6
+ end
3
7
  end
@@ -1,9 +1,9 @@
1
1
  $: << File.expand_path('../lib', __FILE__)
2
- # require 'mongoid/time_range/version'
2
+ require 'mongoid/time_range/version'
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = 'mongoid-time_range'
6
- gem.version = '0.0.1'
6
+ gem.version = Mongoid::TimeRange::VERSION
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'
@@ -14,8 +14,4 @@ Gem::Specification.new do |gem|
14
14
  gem.require_path = 'lib'
15
15
 
16
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
17
  end
@@ -0,0 +1,10 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Mongoid::TimeRange do
4
+ before { require 'mongoid/time_range/global' }
5
+
6
+
7
+ it 'is available as ::TimeRange too' do
8
+ ::TimeRange.must_be_same_as Mongoid::TimeRange
9
+ end
10
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe 'Mongoid::TimeRange integration' do
4
+ before { Timecop.freeze }
5
+
6
+
7
+ describe 'create document without any attributes' do
8
+ subject { Document.create }
9
+
10
+
11
+ it 'sets from to current time and to to nil' do
12
+ subject.range.from.to_i.must_equal Time.now.to_i
13
+ subject.range.to.must_be_nil
14
+ end
15
+ end
16
+
17
+
18
+ describe 'create document only with from attribute' do
19
+ subject { Document.create(range: { from: Time.now.beginning_of_year }) }
20
+
21
+
22
+ it 'sets from to given time and to to nil' do
23
+ subject.range.from.to_i.must_equal Time.now.beginning_of_year.to_i
24
+ subject.range.to.must_be_nil
25
+ end
26
+ end
27
+
28
+
29
+ describe 'create document with both attributes' do
30
+ subject { Document.create(range: { from: Time.now.beginning_of_year, to: Time.now.end_of_year }) }
31
+
32
+
33
+ it 'sets from and to to given time' do
34
+ subject.range.from.to_i.must_equal Time.now.beginning_of_year.to_i
35
+ subject.range.to.to_i.must_equal Time.now.end_of_year.to_i
36
+ end
37
+ end
38
+
39
+
40
+ after { Timecop.return }
41
+ end
@@ -6,6 +6,7 @@ require 'minitest/spec'
6
6
 
7
7
  require 'mongoid/time_range'
8
8
 
9
+ require 'mongoid'
9
10
  require 'timecop'
10
11
 
11
12
  # Load support *.rb files in ./support
@@ -1,9 +1,5 @@
1
1
  class Document
2
2
  include Mongoid::Document
3
3
 
4
- end
5
-
6
- class Localized
7
- include Mongoid::Document
8
-
4
+ field :range, type: Mongoid::TimeRange
9
5
  end
@@ -3,6 +3,6 @@ test:
3
3
  default:
4
4
  database: mongoid_time_range_test
5
5
  hosts:
6
- - localhost: 27017
6
+ - localhost:27017
7
7
  options:
8
8
  consistency: :strong
@@ -65,7 +65,7 @@ describe Mongoid::TimeRange do
65
65
 
66
66
  describe 'having both from and to' do
67
67
  it 'returns database friendly value' do
68
- value.must_equal 'from' => from.mongoize, 'to' => to.mongoize
68
+ value.must_equal from: from.mongoize, to: to.mongoize
69
69
  end
70
70
  end
71
71
 
@@ -75,7 +75,7 @@ describe Mongoid::TimeRange do
75
75
 
76
76
 
77
77
  it 'returns database friendly value' do
78
- value.must_equal 'from' => from.mongoize, 'to' => nil
78
+ value.must_equal from: from.mongoize, to: nil
79
79
  end
80
80
  end
81
81
  end
@@ -88,7 +88,7 @@ describe Mongoid::TimeRange do
88
88
 
89
89
  describe 'having both from and to' do
90
90
  it 'returns database friendly value' do
91
- value.must_equal 'from' => from.mongoize, 'to' => to.mongoize
91
+ value.must_equal from: from.mongoize, to: to.mongoize
92
92
  end
93
93
  end
94
94
 
@@ -98,7 +98,7 @@ describe Mongoid::TimeRange do
98
98
 
99
99
 
100
100
  it 'returns database friendly value' do
101
- value.must_equal 'from' => from.mongoize, 'to' => nil
101
+ value.must_equal from: from.mongoize, to: nil
102
102
  end
103
103
  end
104
104
  end
@@ -110,7 +110,7 @@ describe Mongoid::TimeRange do
110
110
 
111
111
  describe 'having both from and to' do
112
112
  it 'returns database friendly value' do
113
- value.must_equal 'from' => from.mongoize, 'to' => to.mongoize
113
+ value.must_equal from: from.mongoize, to: to.mongoize
114
114
  end
115
115
  end
116
116
 
@@ -120,7 +120,7 @@ describe Mongoid::TimeRange do
120
120
 
121
121
 
122
122
  it 'returns database friendly value' do
123
- value.must_equal 'from' => from.mongoize, 'to' => nil
123
+ value.must_equal from: from.mongoize, to: nil
124
124
  end
125
125
  end
126
126
  end
@@ -128,7 +128,7 @@ describe Mongoid::TimeRange do
128
128
 
129
129
 
130
130
  describe 'self.demongoize' do
131
- let(:value) { Mongoid::TimeRange.demongoize('from' => from, 'to' => to) }
131
+ let(:value) { Mongoid::TimeRange.demongoize(from: from, to: to) }
132
132
 
133
133
 
134
134
  describe 'having both from and to' do
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.0.1
5
+ version: 0.1.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-01 00:00:00.000000000 Z
12
+ date: 2013-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,54 +27,6 @@ dependencies:
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
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
30
  description: Mongoid::TimeRange defines a TimeRange type for your Mongoid apps.
79
31
  email: uher.mario@gmail.com
80
32
  executables: []
@@ -88,8 +40,11 @@ files:
88
40
  - Rakefile
89
41
  - lib/enumerable/associate.rb
90
42
  - lib/mongoid/time_range.rb
43
+ - lib/mongoid/time_range/global.rb
91
44
  - lib/mongoid/time_range/version.rb
92
45
  - mongoid-time_range.gemspec
46
+ - spec/global_spec.rb
47
+ - spec/integration_spec.rb
93
48
  - spec/spec_helper.rb
94
49
  - spec/support/connection.rb
95
50
  - spec/support/document.rb