timespan 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,8 +8,10 @@ gem 'xduration', '~> 2.2'
8
8
  group :test, :development do
9
9
  gem "rspec", ">= 2.8.0"
10
10
  gem 'rails', '~> 3.2'
11
- gem 'mongoid', '~> 2.4'
12
- gem 'bson', '~> 1.6'
11
+ gem 'mongoid', '>= 2.4'
12
+ # gem 'bson', '>= 1.6'
13
+
14
+ # gem 'mongoid', '~> 3.0.0.rc', :git => 'git://github.com/mongoid/mongoid.git'
13
15
  # gem 'i18n'
14
16
  end
15
17
 
data/Gemfile.lock CHANGED
@@ -53,11 +53,11 @@ GEM
53
53
  mime-types (1.18)
54
54
  mongo (1.6.2)
55
55
  bson (~> 1.6.2)
56
- mongoid (2.4.9)
56
+ mongoid (2.4.10)
57
57
  activemodel (~> 3.1)
58
58
  mongo (~> 1.3)
59
59
  tzinfo (~> 0.3.22)
60
- multi_json (1.3.4)
60
+ multi_json (1.3.6)
61
61
  numerizer (0.1.1)
62
62
  polyglot (0.3.3)
63
63
  rack (1.4.1)
@@ -116,12 +116,11 @@ PLATFORMS
116
116
  ruby
117
117
 
118
118
  DEPENDENCIES
119
- bson (~> 1.6)
120
119
  bundler (>= 1.0.0)
121
120
  chronic
122
121
  chronic_duration
123
122
  jeweler (>= 1.8.3)
124
- mongoid (~> 2.4)
123
+ mongoid (>= 2.4)
125
124
  rails (~> 3.2)
126
125
  rdoc (>= 3.12)
127
126
  rspec (>= 2.8.0)
data/README.md CHANGED
@@ -107,6 +107,8 @@ da:
107
107
 
108
108
  ## Timespan for Mongoid
109
109
 
110
+ Tested and works with Mongoid 2.4 and 3.0.0.rc
111
+
110
112
  Custom Timespan datatype
111
113
 
112
114
  ```ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
@@ -8,21 +8,9 @@ class Object
8
8
  end
9
9
  end
10
10
 
11
- Mongoid::Fields.option :between do |model, field, options|
12
- name = field.name.to_sym
13
- model.class_eval do
14
- meta_def :"#{name}_between" do |from, to|
15
- self.where(:"#{name}.#{TimeSpan.start_field}".gte => from.to_i, :"#{name}.#{TimeSpan.end_field}".lte => to.to_i)
16
- end
17
- end
18
- end
19
-
20
- # Mongoid serialization support for Timespan type.
21
11
  module Mongoid
22
12
  module Fields
23
13
  class Timespan
24
- include Mongoid::Fields::Serializable
25
-
26
14
  class << self
27
15
  attr_writer :start_field, :end_field
28
16
 
@@ -35,58 +23,53 @@ module Mongoid
35
23
  end
36
24
  end
37
25
 
38
- def self.instantiate(name, options = {})
39
- super
40
- end
41
-
42
- # Deserialize a Timespan given the hash stored by Mongodb
43
- #
44
- # @param [Hash] Timespan as hash
45
- # @return [Timespan] deserialized Timespan
46
- def deserialize(hash)
47
- return if !hash
48
- ::Timespan.new :from => from(hash), :to => to(hash)
49
- end
26
+ # protected
50
27
 
51
- # Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to
52
- # a BSON serializable type.
53
- #
54
- # @param [Timespan, Hash, Integer, String] value
55
- # @return [Hash] Timespan in seconds
56
- def serialize(value)
57
- return if value.blank?
58
- timespan = case value
59
- when ::Timespan
60
- value
61
- else
62
- ::Timespan.new(value)
28
+ module Methods
29
+ def from hash
30
+ from_value = hash['from'] || hash[:from]
31
+ raise ArgumentError, ":from is nil, #{hash.inspect}" if from_value.nil?
32
+ deserialize_time from_value
63
33
  end
64
- {:from => serialize_time(timespan.start_time), :to => serialize_time(timespan.end_time.to_i), :duration => timespan.duration.total }
65
- end
66
34
 
67
- protected
35
+ def to hash
36
+ to_value = hash['to'] || hash[:to]
37
+ raise ArgumentError, ":to is nil, #{hash.inspect}" if to_value.nil?
38
+ deserialize_time to_value
39
+ end
68
40
 
69
- def from hash
70
- from_value = hash['from'] || hash[:from]
71
- raise ArgumentError, ":from is nil, #{hash.inspect}" if from_value.nil?
72
- deserialize_time from_value
73
- end
41
+ def serialize_time time
42
+ raise ArgumentError, "Can't serialize time from nil" if time.nil?
43
+ time.to_i
44
+ end
74
45
 
75
- def to hash
76
- to_value = hash['to'] || hash[:to]
77
- raise ArgumentError, ":to is nil, #{hash.inspect}" if to_value.nil?
78
- deserialize_time to_value
46
+ def deserialize_time millisecs
47
+ raise ArgumentError, "Can't deserialize time from nil" if millisecs.nil?
48
+ Time.at millisecs
49
+ end
79
50
  end
80
51
 
81
- def serialize_time time
82
- time.to_i
83
- end
52
+ include Methods
53
+ extend Methods
54
+ end
55
+ end
56
+ end
84
57
 
85
- def deserialize_time millisecs
86
- Time.at millisecs
58
+ if defined?(Mongoid::Fields) && Mongoid::Fields.respond_to?(:option)
59
+ Mongoid::Fields.option :between do |model, field, options|
60
+ name = field.name.to_sym
61
+ model.class_eval do
62
+ meta_def :"#{name}_between" do |from, to|
63
+ self.where(:"#{name}.#{TimeSpan.start_field}".gte => from.to_i, :"#{name}.#{TimeSpan.end_field}".lte => to.to_i)
87
64
  end
88
65
  end
89
66
  end
90
67
  end
91
68
 
69
+ module Mongoid
70
+ MAJOR_VERSION = Mongoid::VERSION > '3' ? 3 : 2
71
+ end
72
+
73
+ require "timespan/mongoid/mongoid_#{Mongoid::MAJOR_VERSION}x"
74
+
92
75
  TimeSpan = Mongoid::Fields::Timespan
@@ -0,0 +1,37 @@
1
+ # Mongoid serialization support for Timespan type.
2
+ module Mongoid
3
+ module Fields
4
+ class Timespan
5
+ include Mongoid::Fields::Serializable
6
+
7
+ def self.instantiate(name, options = {})
8
+ super
9
+ end
10
+
11
+ # Deserialize a Timespan given the hash stored by Mongodb
12
+ #
13
+ # @param [Hash] Timespan as hash
14
+ # @return [Timespan] deserialized Timespan
15
+ def deserialize(hash)
16
+ return if !hash
17
+ ::Timespan.new :from => from(hash), :to => to(hash)
18
+ end
19
+
20
+ # Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to
21
+ # a BSON serializable type.
22
+ #
23
+ # @param [Timespan, Hash, Integer, String] value
24
+ # @return [Hash] Timespan in seconds
25
+ def serialize(value)
26
+ return if value.blank?
27
+ timespan = case value
28
+ when ::Timespan
29
+ value
30
+ else
31
+ ::Timespan.new(value)
32
+ end
33
+ {:from => serialize_time(timespan.start_time), :to => serialize_time(timespan.end_time.to_i), :duration => timespan.duration.total }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ class Timespan
2
+ module Mongoize
3
+ extend ActiveSupport::Concern
4
+ # See http://mongoid.org/en/mongoid/docs/upgrading.html
5
+
6
+ # Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to
7
+ # a BSON serializable type.
8
+ #
9
+ # @param [Timespan, Hash, Integer, String] value
10
+ # @return [Hash] Timespan in seconds
11
+ def mongoize
12
+ {:from => serialize_time(start_time), :to => serialize_time(end_time), :duration => duration.total }
13
+ end
14
+
15
+ module ClassMethods
16
+ # Deserialize a Timespan given the hash stored by Mongodb
17
+ #
18
+ # @param [Hash] Timespan as hash
19
+ # @return [Timespan] deserialized Timespan
20
+ def demongoize(value)
21
+ return if !value
22
+ case value
23
+ when Hash
24
+ ::Timespan.new :from => from(value), :to => to(value)
25
+ else
26
+ ::Timespan.new value
27
+ end
28
+ end
29
+
30
+ # TODO
31
+ # def evolve(object)
32
+ # { "$gte" => object.first, "$lte" => object.last }
33
+ # end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ class Timespan
40
+ include Mongoize
41
+
42
+ protected
43
+
44
+ include Mongoid::Fields::Timespan::Methods
45
+ extend Mongoid::Fields::Timespan::Methods
46
+ end
@@ -1,6 +1,7 @@
1
1
  class Account
2
- include Mongoid::Document
3
- field :period, :type => TimeSpan, :between => true
2
+ include Mongoid::Document
3
+
4
+ field :period, :type => TimeSpan, :between => true
4
5
 
5
6
  def self.create_it! duration
6
7
  s = self.new
@@ -0,0 +1,14 @@
1
+ class Account
2
+ include Mongoid::Document
3
+
4
+ field :period, :type => ::Timespan, :between => true
5
+
6
+ def self.create_it! duration
7
+ t = ::Timespan.new(duration: duration)
8
+ self.new period: t
9
+ end
10
+
11
+ def self.between from, to
12
+ Account.where(:'period.from'.gt => from.to_i, :'period.to'.lte => to.to_i)
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ module Mongoid
2
+ module VersionSetup
3
+ def self.configure config
4
+ version = Mongoid::VERSION
5
+ case
6
+ when version < '2'
7
+ raise ArgumentError, "Mongoid versions < 2 not supported"
8
+ when version < '3'
9
+ version_2 config
10
+ when version > '3'
11
+ version_3 config
12
+ end
13
+ end
14
+
15
+ def self.version_3 config
16
+ require 'moped'
17
+ config.connect_to('mongoid_geo_test')
18
+ end
19
+
20
+ def self.version_2 config
21
+ require 'bson'
22
+
23
+ opts = YAML.load(File.read(File.dirname(__FILE__) + '/support/mongoid.yml'))["test"]
24
+ opts.delete("slaves") # no slaves support for version < 3
25
+ name = opts.delete("database")
26
+ host = opts.delete("host")
27
+ port = opts.delete("port")
28
+ config.master = begin
29
+ Mongo::Connection.new(host, port, opts).db(name)
30
+ rescue Mongo::ConnectionFailure
31
+ Mongo::Connection.new(host, '27017', opts).db(name)
32
+ end
33
+ config.logger = nil
34
+ config.allow_dynamic_fields = true
35
+ end
36
+ end
37
+ end
@@ -30,21 +30,9 @@ describe TimeSpan do
30
30
  end
31
31
  end
32
32
 
33
- context '2 days duration using :duration => integer via ActiveSupport::Duration' do
34
- let(:account) do
35
- Account.create :period => {:duration => 2.days }
36
- end
37
-
38
- describe '.start_date' do
39
- it 'should default to today' do
40
- DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
41
- end
42
- end
43
- end
44
-
45
33
  context '2 days using integer via ActiveSupport::Duration' do
46
34
  let(:account) do
47
- Account.create :period => 2.days
35
+ Account.create :period => Timespan.new(2.days)
48
36
  end
49
37
 
50
38
  describe '.start_date' do
@@ -53,28 +41,4 @@ describe TimeSpan do
53
41
  end
54
42
  end
55
43
  end
56
-
57
- context '2 days duration using string' do
58
- let(:account) do
59
- Account.create :period => {:duration => '2 days'}
60
- end
61
-
62
- describe '.start_date' do
63
- it 'should default to today' do
64
- DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
65
- end
66
- end
67
- end
68
-
69
- context '2 days duration (from 1 day ago)' do
70
- let(:account) do
71
- Account.create :period => {:duration => '2 days', :from => from }
72
- end
73
-
74
- describe '.start_date' do
75
- it 'should default to today' do
76
- DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == 1.day.ago.strftime('%d %b %Y')
77
- end
78
- end
79
- end
80
44
  end
@@ -1,12 +1,31 @@
1
1
  require 'spec_helper'
2
2
  require 'mongoid'
3
- require 'bson'
4
3
 
5
- Mongoid.configure.master = Mongo::Connection.new.db('timespan')
4
+ # puts "version: #{Mongoid::VERSION}"
6
5
 
7
- Mongoid.database.collections.each do |coll|
8
- coll.remove
6
+ require 'timespan/mongoid/mongoid_setup'
7
+
8
+ Mongoid.configure do |config|
9
+ Mongoid::VersionSetup.configure config
10
+ end
11
+
12
+ if RUBY_VERSION >= '1.9.2'
13
+ YAML::ENGINE.yamler = 'syck'
14
+ end
15
+
16
+ RSpec.configure do |config|
17
+ # config.mock_with(:mocha)
18
+
19
+ config.before(:each) do
20
+ Mongoid.purge!
21
+ # Mongoid.database.collections.each do |collection|
22
+ # unless collection.name =~ /^system\./
23
+ # collection.remove
24
+ # end
25
+ # end
26
+ end
9
27
  end
10
28
 
11
29
  require 'timespan/mongoid'
12
- require 'timespan/mongoid/account'
30
+
31
+ require "timespan/mongoid/models/account_#{Mongoid::MAJOR_VERSION}x"
@@ -0,0 +1,3 @@
1
+ dbpath = /usr/local/var/mongodb
2
+ master = true
3
+ bind_ip = 127.0.0.1
@@ -0,0 +1,19 @@
1
+ test:
2
+ database: mongoid_timespan_test
3
+ host: localhost
4
+ port: 27017
5
+ # slaves:
6
+ # - host: localhost
7
+ # port: 27018
8
+ # - host: localhost
9
+ # port: 27019
10
+ # allow_dynamic_fields: false
11
+ # include_root_in_json: true
12
+ # parameterize_keys: false
13
+ # persist_in_safe_mode: false
14
+ # raise_not_found_error: false
15
+ # reconnect_time: 5
16
+ # autocreate_indexes: false
17
+ # persist_types: false
18
+ # option_no_exist: false
19
+ # skip_version_check: false
data/timespan.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.3.2"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2012-05-30"
12
+ s.date = "2012-06-05"
13
13
  s.description = "Makes it easy to calculate time distance in different units"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -29,6 +29,8 @@ Gem::Specification.new do |s|
29
29
  "lib/timespan.rb",
30
30
  "lib/timespan/compare.rb",
31
31
  "lib/timespan/mongoid.rb",
32
+ "lib/timespan/mongoid/mongoid_2x.rb",
33
+ "lib/timespan/mongoid/mongoid_3x.rb",
32
34
  "lib/timespan/printer.rb",
33
35
  "lib/timespan/rails/engine.rb",
34
36
  "lib/timespan/span.rb",
@@ -37,10 +39,14 @@ Gem::Specification.new do |s|
37
39
  "spec/timespan/compare_spec.rb",
38
40
  "spec/timespan/duration_macros_spec.rb",
39
41
  "spec/timespan/locales/duration_da.yml",
40
- "spec/timespan/mongoid/account.rb",
42
+ "spec/timespan/mongoid/models/account_2x.rb",
43
+ "spec/timespan/mongoid/models/account_3x.rb",
41
44
  "spec/timespan/mongoid/mongoid_search_spec.rb",
45
+ "spec/timespan/mongoid/mongoid_setup.rb",
42
46
  "spec/timespan/mongoid/mongoid_timespan_spec.rb",
43
47
  "spec/timespan/mongoid/spec_helper.rb",
48
+ "spec/timespan/mongoid/support/mongod.conf",
49
+ "spec/timespan/mongoid/support/mongoid.yml",
44
50
  "spec/timespan/printer_spec.rb",
45
51
  "spec/timespan/span_spec.rb",
46
52
  "spec/timespan/units_spec.rb",
@@ -64,8 +70,7 @@ Gem::Specification.new do |s|
64
70
  s.add_runtime_dependency(%q<xduration>, ["~> 2.2"])
65
71
  s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
66
72
  s.add_development_dependency(%q<rails>, ["~> 3.2"])
67
- s.add_development_dependency(%q<mongoid>, ["~> 2.4"])
68
- s.add_development_dependency(%q<bson>, ["~> 1.6"])
73
+ s.add_development_dependency(%q<mongoid>, [">= 2.4"])
69
74
  s.add_development_dependency(%q<rdoc>, [">= 3.12"])
70
75
  s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
71
76
  s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
@@ -77,8 +82,7 @@ Gem::Specification.new do |s|
77
82
  s.add_dependency(%q<xduration>, ["~> 2.2"])
78
83
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
79
84
  s.add_dependency(%q<rails>, ["~> 3.2"])
80
- s.add_dependency(%q<mongoid>, ["~> 2.4"])
81
- s.add_dependency(%q<bson>, ["~> 1.6"])
85
+ s.add_dependency(%q<mongoid>, [">= 2.4"])
82
86
  s.add_dependency(%q<rdoc>, [">= 3.12"])
83
87
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
84
88
  s.add_dependency(%q<jeweler>, [">= 1.8.3"])
@@ -91,8 +95,7 @@ Gem::Specification.new do |s|
91
95
  s.add_dependency(%q<xduration>, ["~> 2.2"])
92
96
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
93
97
  s.add_dependency(%q<rails>, ["~> 3.2"])
94
- s.add_dependency(%q<mongoid>, ["~> 2.4"])
95
- s.add_dependency(%q<bson>, ["~> 1.6"])
98
+ s.add_dependency(%q<mongoid>, [">= 2.4"])
96
99
  s.add_dependency(%q<rdoc>, [">= 3.12"])
97
100
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
98
101
  s.add_dependency(%q<jeweler>, [">= 1.8.3"])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timespan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-30 00:00:00.000000000 Z
12
+ date: 2012-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
@@ -112,7 +112,7 @@ dependencies:
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements:
115
- - - ~>
115
+ - - ! '>='
116
116
  - !ruby/object:Gem::Version
117
117
  version: '2.4'
118
118
  type: :development
@@ -120,25 +120,9 @@ dependencies:
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  none: false
122
122
  requirements:
123
- - - ~>
123
+ - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '2.4'
126
- - !ruby/object:Gem::Dependency
127
- name: bson
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ~>
132
- - !ruby/object:Gem::Version
133
- version: '1.6'
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ~>
140
- - !ruby/object:Gem::Version
141
- version: '1.6'
142
126
  - !ruby/object:Gem::Dependency
143
127
  name: rdoc
144
128
  requirement: !ruby/object:Gem::Requirement
@@ -223,6 +207,8 @@ files:
223
207
  - lib/timespan.rb
224
208
  - lib/timespan/compare.rb
225
209
  - lib/timespan/mongoid.rb
210
+ - lib/timespan/mongoid/mongoid_2x.rb
211
+ - lib/timespan/mongoid/mongoid_3x.rb
226
212
  - lib/timespan/printer.rb
227
213
  - lib/timespan/rails/engine.rb
228
214
  - lib/timespan/span.rb
@@ -231,10 +217,14 @@ files:
231
217
  - spec/timespan/compare_spec.rb
232
218
  - spec/timespan/duration_macros_spec.rb
233
219
  - spec/timespan/locales/duration_da.yml
234
- - spec/timespan/mongoid/account.rb
220
+ - spec/timespan/mongoid/models/account_2x.rb
221
+ - spec/timespan/mongoid/models/account_3x.rb
235
222
  - spec/timespan/mongoid/mongoid_search_spec.rb
223
+ - spec/timespan/mongoid/mongoid_setup.rb
236
224
  - spec/timespan/mongoid/mongoid_timespan_spec.rb
237
225
  - spec/timespan/mongoid/spec_helper.rb
226
+ - spec/timespan/mongoid/support/mongod.conf
227
+ - spec/timespan/mongoid/support/mongoid.yml
238
228
  - spec/timespan/printer_spec.rb
239
229
  - spec/timespan/span_spec.rb
240
230
  - spec/timespan/units_spec.rb
@@ -256,7 +246,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
256
246
  version: '0'
257
247
  segments:
258
248
  - 0
259
- hash: 1296550633399706024
249
+ hash: 3654551960923174159
260
250
  required_rubygems_version: !ruby/object:Gem::Requirement
261
251
  none: false
262
252
  requirements: