mongoid-time_range 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c386f40e7b4f23e18633700bee7176944128244b
4
- data.tar.gz: 07d68441727132bffd803dce7344cb5eb36a1ef5
3
+ metadata.gz: 42533adde53219b0683bdccbd450ad52770135a6
4
+ data.tar.gz: da95eff5f8eee018f51e91d4af22312cd55235cb
5
5
  SHA512:
6
- metadata.gz: 3b990abf33843cc8c179c14814891ec1a56611a0118bef5ddb2167ad247acb0ff0d927f67d7af5314c49ecece2f0a9d9338c0541e39a279443695a31884a9722
7
- data.tar.gz: a7e22009d8c4f1d29128490fee3149a1c88555604b82b6cad7461ab5fdf864a7eb800c6876a25cbebf8a05b75e63865249178d2f68dd3ed5e74beb02fc9b4ede
6
+ metadata.gz: 1034c7f4d369c874b2f8cdb7f44f23541ada721fd771e6eca15613cf2074c1cab7a68005fa2bd682618e428a2fd4f07d2b77ab3fadd3b77e31898e1011a0d519
7
+ data.tar.gz: e7dbe735671ea3cc672f9a30e9d825092cf87d1bd9805283067c1873e7c615e1faff9d7268ac86ef5f0d7604065e784ee72f24929e114c31f76aa52bb9b1aec0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 0.5.0 (Febrary 4, 2014)
2
+ * Simplifies tests a lot.
3
+ * Removes (direct) active_support dependency.
4
+
5
+ # 0.4.1 (February 4, 2014)
6
+ * Locks on mongoid ~> 3.0.
7
+
1
8
  # 0.4.0 (January 06, 2013)
2
9
  * Adds setters.
3
10
 
data/Gemfile CHANGED
@@ -1,7 +1,6 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
5
  gem 'minitest'
6
6
  gem 'rake'
7
- gem 'timecop'
data/README.md CHANGED
@@ -53,7 +53,7 @@ gem 'mongoid-time_range', require: 'mongoid/time_range/global'
53
53
 
54
54
  (The MIT license)
55
55
 
56
- Copyright (c) 2013 Mario Uher
56
+ Copyright (c) 2013-2014 Mario Uher
57
57
 
58
58
  Permission is hereby granted, free of charge, to any person obtaining
59
59
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rake/testtask'
2
2
 
3
3
  Rake::TestTask.new do |t|
4
- t.test_files = FileList['spec/**/*_spec.rb']
4
+ t.pattern = 'test/*_test.rb'
5
5
  end
6
6
 
7
7
  task default: :test
@@ -1,3 +1,3 @@
1
1
  require 'mongoid/time_range'
2
2
 
3
- TimeRange = Mongoid::TimeRange
3
+ ::TimeRange = Mongoid::TimeRange
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  class TimeRange < Hash
3
- VERSION = '0.4.1'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -1,18 +1,17 @@
1
1
  require 'active_support/all'
2
- require 'enumerable/associate'
3
2
  require 'mongoid'
4
3
 
5
4
  module Mongoid
6
5
  class TimeRange < Hash
7
6
  def initialize(from = Time.now, to = nil)
8
- merge!(from: from, to: to)
7
+ merge! from: from, to: to
9
8
  end
10
9
 
11
10
  [:from, :to].each do |key|
12
11
  define_method key do
13
12
  self[key]
14
13
  end
15
-
14
+
16
15
  define_method :"#{key}=" do |value|
17
16
  self[key] = value
18
17
  end
@@ -26,17 +25,15 @@ module Mongoid
26
25
 
27
26
  class << self
28
27
  def mongoize(object)
29
- [:from, :to].associate { |key| Time.mongoize(object[key]) }
28
+ { from: Time.mongoize(object[:from]), to: Time.mongoize(object[:to]) }
30
29
  end
31
30
 
32
31
  def demongoize(hash)
33
- return nil if hash.nil?
34
-
32
+ return if hash.nil?
35
33
  hash = hash.symbolize_keys
36
- hash = [:from, :to].associate { |key| Time.demongoize(hash[key]) }
37
-
38
- new.merge(hash)
34
+
35
+ new(Time.demongoize(hash[:from]), Time.demongoize(hash[:to]))
39
36
  end
40
37
  end
41
38
  end
42
- end
39
+ end
@@ -13,6 +13,5 @@ Gem::Specification.new do |gem|
13
13
  gem.files = `git ls-files`.split("\n")
14
14
  gem.require_path = 'lib'
15
15
 
16
- gem.add_dependency 'activesupport'
17
16
  gem.add_dependency 'mongoid', '~> 3.0'
18
17
  end
@@ -0,0 +1,43 @@
1
+ require_relative 'test_helper'
2
+
3
+ class Document
4
+ include Mongoid::Document
5
+
6
+ field :range, type: Mongoid::TimeRange
7
+ end
8
+
9
+ class IntegrationTest < Minitest::Test
10
+ def test_it_initializes_with_nil
11
+ assert_nil Document.create.range
12
+ end
13
+
14
+
15
+ def test_it_initializes_with_default_hash
16
+ assert(Class.new(Document) do
17
+ field :range, type: Mongoid::TimeRange, default: ->{ { from: Time.now } }
18
+ end.create.range)
19
+ end
20
+
21
+
22
+ def test_it_initializes_with_default_time_range
23
+ assert(Class.new(Document) do
24
+ field :range, type: Mongoid::TimeRange, default: ->{ Mongoid::TimeRange.new(Time.now) }
25
+ end.create.range)
26
+ end
27
+
28
+
29
+ def test_it_initializes_with_given_from
30
+ document = Document.create(range: { from: Time.now })
31
+
32
+ assert document.range.from
33
+ assert_nil document.range.to
34
+ end
35
+
36
+
37
+ def test_it_initializes_with_given_from_and_to
38
+ document = Document.create(range: { from: Time.now, to: Time.now })
39
+
40
+ assert document.range.from
41
+ assert document.range.to
42
+ end
43
+ end
File without changes
@@ -0,0 +1,8 @@
1
+ require_relative 'test_helper'
2
+ require 'mongoid/time_range/global'
3
+
4
+ class RequireTest < Minitest::Test
5
+ def test_time_range_is_globally_available
6
+ assert_equal ::TimeRange, Mongoid::TimeRange
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ $: << File.expand_path('../../lib', __FILE__)
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+
5
+ require 'mongoid/time_range'
6
+
7
+ Mongoid.load!(File.expand_path('../mongoid.yml', __FILE__), 'test')
@@ -0,0 +1,90 @@
1
+ require_relative 'test_helper'
2
+
3
+ class TimeRangeTest < Minitest::Test
4
+ def freeze_time(&block)
5
+ Time.stub :now, Time.at(Time.now) do
6
+ yield
7
+ end
8
+ end
9
+
10
+
11
+ def test_it_initializes_without_any_argument
12
+ freeze_time do
13
+ time_range = Mongoid::TimeRange.new
14
+ assert_equal Time.now, time_range.from
15
+ assert_nil time_range.to
16
+
17
+ time_range = Mongoid::TimeRange.new(Time.now)
18
+ assert_equal Time.now, time_range.from
19
+ assert_nil time_range.to
20
+
21
+ time_range = Mongoid::TimeRange.new(Time.now, Time.now.tomorrow)
22
+ assert_equal Time.now, time_range.from
23
+ assert_equal Time.now.tomorrow, time_range.to
24
+ end
25
+ end
26
+
27
+
28
+ def test_it_has_getters_and_getters
29
+ time_range = Mongoid::TimeRange.new
30
+
31
+ assert time_range.respond_to?(:from)
32
+ assert time_range.respond_to?(:from=)
33
+
34
+ assert time_range.respond_to?(:to)
35
+ assert time_range.respond_to?(:to=)
36
+ end
37
+
38
+
39
+ def test_mongoize_returns_database_friendly_hash
40
+ freeze_time do
41
+ time_range = Mongoid::TimeRange.new(Time.now)
42
+ assert_equal({ from: Time.now.mongoize, to: nil }, time_range.mongoize)
43
+
44
+ time_range = Mongoid::TimeRange.new(Time.now, Time.now.tomorrow)
45
+ assert_equal({ from: Time.now.mongoize, to: Time.now.tomorrow.mongoize }, time_range.mongoize)
46
+ end
47
+ end
48
+
49
+
50
+ def test_class_mongoize_returns_database_friendly_hash_when_giving_a_time_range
51
+ freeze_time do
52
+ time_range = Mongoid::TimeRange.new(Time.now)
53
+ assert_equal({ from: Time.now.mongoize, to: nil }, Mongoid::TimeRange.mongoize(time_range))
54
+
55
+ time_range = Mongoid::TimeRange.new(Time.now, Time.now.tomorrow)
56
+ assert_equal({ from: Time.now.mongoize, to: Time.now.tomorrow.mongoize }, Mongoid::TimeRange.mongoize(time_range))
57
+ end
58
+ end
59
+
60
+
61
+ def test_class_mongoize_returns_database_friendly_hash_when_giving_a_hash
62
+ freeze_time do
63
+ hash = Mongoid::TimeRange.mongoize(from: Time.now, to: nil)
64
+ assert_equal({ from: Time.now.mongoize, to: nil }, Mongoid::TimeRange.mongoize(hash))
65
+
66
+ hash = Mongoid::TimeRange.mongoize(from: Time.now, to: Time.now.tomorrow)
67
+ assert_equal({ from: Time.now.mongoize, to: Time.now.tomorrow.mongoize }, Mongoid::TimeRange.mongoize(hash))
68
+ end
69
+ end
70
+
71
+
72
+ def test_class_demongoize_returns_time_range
73
+ freeze_time do
74
+ time_range = Mongoid::TimeRange.demongoize(from: Time.now, to: nil)
75
+ assert_equal Mongoid::TimeRange.new(Time.now, nil), time_range
76
+
77
+ time_range = Mongoid::TimeRange.demongoize(from: Time.now, to: Time.now.tomorrow)
78
+ assert_equal Mongoid::TimeRange.new(Time.now, Time.now.tomorrow), time_range
79
+ end
80
+ end
81
+
82
+
83
+ def test_it_returns_array
84
+ freeze_time do
85
+ time_range = Mongoid::TimeRange.demongoize(from: Time.now, to: nil)
86
+
87
+ assert_equal [Time.now, nil], time_range.to_a
88
+ end
89
+ end
90
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-time_range
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Uher
@@ -10,20 +10,6 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activesupport
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: mongoid
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -49,19 +35,15 @@ files:
49
35
  - Gemfile
50
36
  - README.md
51
37
  - Rakefile
52
- - lib/enumerable/associate.rb
53
38
  - lib/mongoid/time_range.rb
54
39
  - lib/mongoid/time_range/global.rb
55
40
  - lib/mongoid/time_range/version.rb
56
41
  - mongoid-time_range.gemspec
57
- - spec/global_spec.rb
58
- - spec/integration_spec.rb
59
- - spec/spec_helper.rb
60
- - spec/support/connection.rb
61
- - spec/support/document.rb
62
- - spec/support/mongoid.yml
63
- - spec/support/time.rb
64
- - spec/time_range_spec.rb
42
+ - test/integration_test.rb
43
+ - test/mongoid.yml
44
+ - test/require_test.rb
45
+ - test/test_helper.rb
46
+ - test/time_range_test.rb
65
47
  homepage: https://github.com/haihappen/mongoid-time_range
66
48
  licenses: []
67
49
  metadata: {}
@@ -1,29 +0,0 @@
1
- module Enumerable # Remove this 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
data/spec/global_spec.rb DELETED
@@ -1,10 +0,0 @@
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
@@ -1,58 +0,0 @@
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.must_be_nil
13
- end
14
-
15
-
16
- describe 'when default is set to from: Time.now }' do
17
- subject { DocumentWithImplicitDefault.create }
18
-
19
-
20
- it 'sets from to current time and to to ni' do
21
- subject.range.must_equal from: Time.now, to: nil
22
- end
23
- end
24
-
25
-
26
- describe 'when default is set to ->{ Mongoid::TimeRange.new(from: Time.now) }' do
27
- subject { DocumentWithExplicitDefault.create }
28
-
29
-
30
- it 'sets from to current time and to to ni' do
31
- subject.range.must_equal from: Time.now, to: nil
32
- end
33
- end
34
- end
35
-
36
-
37
- describe 'create document only with from attribute' do
38
- subject { Document.create(range: { from: Time.now.beginning_of_year }) }
39
-
40
-
41
- it 'sets from to given time and to to nil' do
42
- subject.range.must_equal from: Time.now.beginning_of_year, to: nil
43
- end
44
- end
45
-
46
-
47
- describe 'create document with both attributes' do
48
- subject { Document.create(range: { from: Time.now.beginning_of_year, to: Time.now.end_of_year }) }
49
-
50
-
51
- it 'sets from and to to given time' do
52
- subject.range.must_equal from: Time.now.beginning_of_year, to: Time.now.end_of_year
53
- end
54
- end
55
-
56
-
57
- after { Timecop.return }
58
- end
data/spec/spec_helper.rb DELETED
@@ -1,13 +0,0 @@
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 'mongoid'
10
- require 'timecop'
11
-
12
- # Load support *.rb files in ./support
13
- Dir[File.expand_path('../support/*.rb', __FILE__)].each { |file| require_relative file }
@@ -1 +0,0 @@
1
- Mongoid.load!(File.expand_path('../mongoid.yml', __FILE__), 'test')
@@ -1,17 +0,0 @@
1
- class Document
2
- include Mongoid::Document
3
-
4
- field :range, type: Mongoid::TimeRange
5
- end
6
-
7
- class DocumentWithImplicitDefault
8
- include Mongoid::Document
9
-
10
- field :range, type: Mongoid::TimeRange, default: ->{ { from: Time.now } }
11
- end
12
-
13
- class DocumentWithExplicitDefault
14
- include Mongoid::Document
15
-
16
- field :range, type: Mongoid::TimeRange, default: -> { Mongoid::TimeRange.new(Time.now) }
17
- end
data/spec/support/time.rb DELETED
@@ -1,6 +0,0 @@
1
- class Time
2
- def ==(other)
3
- to_i == other.to_i &&
4
- usec - (usec % 1000) == other.usec - (other.usec % 1000)
5
- end
6
- end
@@ -1,182 +0,0 @@
1
- require_relative 'spec_helper'
2
-
3
- describe Mongoid::TimeRange do
4
- before { Timecop.freeze }
5
- let(:from) { Time.now.beginning_of_year }
6
- let(:to) { Time.now.end_of_year }
7
- subject { Mongoid::TimeRange.new(from, to) }
8
-
9
-
10
- describe :initialize do
11
- describe 'without any arguments' do
12
- subject { Mongoid::TimeRange.new }
13
-
14
-
15
- it 'sets from to Time.now' do
16
- subject.from.must_equal Time.now
17
- end
18
-
19
-
20
- it 'sets to to nil' do
21
- subject.to.must_be_nil
22
- end
23
- end
24
-
25
-
26
- describe 'with one argument' do
27
- subject { Mongoid::TimeRange.new(from) }
28
-
29
-
30
- it 'sets from to given value' do
31
- subject.from.must_equal from
32
- end
33
-
34
-
35
- it 'sets to to nil' do
36
- subject.to.must_be_nil
37
- end
38
- end
39
-
40
-
41
- describe 'with two arguments' do
42
- subject { Mongoid::TimeRange.new(from, to) }
43
-
44
-
45
- it 'sets from to given value' do
46
- subject.from.must_equal from
47
- end
48
-
49
-
50
- it 'sets to to given value' do
51
- subject.to.must_equal to
52
- end
53
- end
54
- end
55
-
56
-
57
- describe 'getters' do
58
- it 'responds to from' do
59
- subject.must_respond_to :from
60
- end
61
-
62
-
63
- it 'responds to to' do
64
- subject.must_respond_to :to
65
- end
66
- end
67
-
68
-
69
- describe 'setters' do
70
- it 'responds to from=' do
71
- subject.must_respond_to :from=
72
- end
73
-
74
-
75
- it 'responds to to=' do
76
- subject.must_respond_to :to=
77
- end
78
- end
79
-
80
-
81
- describe :mongoize do
82
- let(:value) { subject.mongoize }
83
-
84
-
85
- describe 'having both from and to' do
86
- it 'returns database friendly value' do
87
- value.must_equal from: from.mongoize, to: to.mongoize
88
- end
89
- end
90
-
91
-
92
- describe 'having only from value' do
93
- let(:to) { nil }
94
-
95
-
96
- it 'returns database friendly value' do
97
- value.must_equal from: from.mongoize, to: nil
98
- end
99
- end
100
- end
101
-
102
-
103
- describe 'self.mongoize' do
104
- describe 'giving a time range object' do
105
- let(:value) { Mongoid::TimeRange.mongoize(subject) }
106
-
107
-
108
- describe 'having both from and to' do
109
- it 'returns database friendly value' do
110
- value.must_equal from: from.mongoize, to: to.mongoize
111
- end
112
- end
113
-
114
-
115
- describe 'having only from value' do
116
- let(:to) { nil }
117
-
118
-
119
- it 'returns database friendly value' do
120
- value.must_equal from: from.mongoize, to: nil
121
- end
122
- end
123
- end
124
-
125
-
126
- describe 'giving a hash' do
127
- let(:value) { Mongoid::TimeRange.mongoize(from: from, to: to) }
128
-
129
-
130
- describe 'having both from and to' do
131
- it 'returns database friendly value' do
132
- value.must_equal from: from.mongoize, to: to.mongoize
133
- end
134
- end
135
-
136
-
137
- describe 'having only from value' do
138
- let(:to) { nil }
139
-
140
-
141
- it 'returns database friendly value' do
142
- value.must_equal from: from.mongoize, to: nil
143
- end
144
- end
145
- end
146
- end
147
-
148
-
149
- describe 'self.demongoize' do
150
- let(:value) { Mongoid::TimeRange.demongoize(from: from, to: to) }
151
-
152
-
153
- describe 'having both from and to' do
154
- it 'returns a TimeRange object' do
155
- value.must_equal Mongoid::TimeRange.new(from, to)
156
- end
157
- end
158
-
159
-
160
- describe 'having only from value' do
161
- let(:to) { nil }
162
-
163
-
164
- it 'returns database friendly value' do
165
- value.must_equal Mongoid::TimeRange.new(from)
166
- end
167
- end
168
- end
169
-
170
-
171
- describe :to_a do
172
- let(:value) { subject.to_a }
173
-
174
-
175
- it 'returns an array' do
176
- value.must_equal [from, to]
177
- end
178
- end
179
-
180
-
181
- after { Timecop.return }
182
- end