timespan 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,6 +8,8 @@ gem 'ruby-duration', :git => 'git://github.com/kristianmandrup/ruby-duration.git
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
13
  # gem 'i18n'
12
14
  end
13
15
 
data/Gemfile.lock CHANGED
@@ -37,6 +37,7 @@ GEM
37
37
  i18n (~> 0.6)
38
38
  multi_json (~> 1.0)
39
39
  arel (3.0.2)
40
+ bson (1.6.2)
40
41
  builder (3.0.0)
41
42
  chronic (0.6.7)
42
43
  chronic_duration (0.9.6)
@@ -58,6 +59,12 @@ GEM
58
59
  mime-types (~> 1.16)
59
60
  treetop (~> 1.4.8)
60
61
  mime-types (1.18)
62
+ mongo (1.6.2)
63
+ bson (~> 1.6.2)
64
+ mongoid (2.4.9)
65
+ activemodel (~> 3.1)
66
+ mongo (~> 1.3)
67
+ tzinfo (~> 0.3.22)
61
68
  multi_json (1.3.2)
62
69
  numerizer (0.1.1)
63
70
  polyglot (0.3.3)
@@ -114,10 +121,12 @@ PLATFORMS
114
121
  ruby
115
122
 
116
123
  DEPENDENCIES
124
+ bson (~> 1.6)
117
125
  bundler (>= 1.0.0)
118
126
  chronic
119
127
  chronic_duration
120
128
  jeweler (>= 1.8.3)
129
+ mongoid (~> 2.4)
121
130
  rails (~> 3.2)
122
131
  rdoc (>= 3.12)
123
132
  rspec (>= 2.8.0)
data/README.md CHANGED
@@ -90,12 +90,22 @@ Custom Timespan datatype
90
90
  ```ruby
91
91
  require 'timespan/mongoid'
92
92
 
93
- class MyModel
93
+ class Account
94
94
  include Mongoid::Document
95
- field :period, type => Timespan
95
+ field :period, :type => TimeSpan
96
96
  end
97
97
  ```
98
98
 
99
+ Usage example:
100
+
101
+ ```ruby
102
+ account = Account.create :period => {:duration => '2 days', :from => Date.today }
103
+
104
+ account.period.start_date
105
+ account.period.end_date
106
+ account.period.days
107
+ account.period.duration # => Duration
108
+
99
109
  ## Chronic duration
100
110
 
101
111
  Is used to parse duration strings if Spanner can't be handle it
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -19,7 +19,6 @@ class Timespan
19
19
  end
20
20
 
21
21
  module Compare
22
-
23
22
  include Comparable
24
23
 
25
24
  def time_left time = nil
@@ -48,6 +47,15 @@ class Timespan
48
47
  end
49
48
  end
50
49
 
50
+ def +(other)
51
+ self.duration += Duration.new(other)
52
+ end
53
+
54
+ def -(other)
55
+ self.duration -= Duration.new(other)
56
+ end
57
+
58
+
51
59
  def valid_compare? time
52
60
  valid_compare_types.any? {|type| time.kind_of? type }
53
61
  end
@@ -7,6 +7,10 @@ module Mongoid
7
7
  class Timespan
8
8
  include Mongoid::Fields::Serializable
9
9
 
10
+ def self.instantiate(name, options = {})
11
+ super
12
+ end
13
+
10
14
  # Deserialize a Timespan given the hash stored by Mongodb
11
15
  #
12
16
  # @param [Hash] Timespan as hash
@@ -24,8 +28,10 @@ module Mongoid
24
28
  def serialize(value)
25
29
  return if value.blank?
26
30
  timespan = ::Timespan.new(value)
27
- {:from => time_span.start_time, :to => time_span.end_time, :Timespan => time_span.Timespan}
31
+ {:from => timespan.start_time, :to => timespan.end_time, :duration => timespan.duration.total}
28
32
  end
29
33
  end
30
34
  end
31
- end
35
+ end
36
+
37
+ TimeSpan = Mongoid::Fields::Timespan
data/lib/timespan/span.rb CHANGED
@@ -6,6 +6,8 @@ class Timespan
6
6
 
7
7
  def duration= duration
8
8
  @duration = case duration
9
+ when Timespan
10
+ duration.duration
9
11
  when Duration
10
12
  duration
11
13
  when Numeric, Hash
@@ -0,0 +1,4 @@
1
+ class Account
2
+ include Mongoid::Document
3
+ field :period, :type => ::TimeSpan
4
+ end
@@ -0,0 +1,20 @@
1
+ require 'timespan/mongoid/spec_helper'
2
+
3
+ describe Timespan do
4
+ subject { account }
5
+
6
+ let(:from) { Chronic.parse("1 day ago") }
7
+ let(:to) { Time.now }
8
+
9
+ context '2 days duration (from now - default)' do
10
+ let(:account) do
11
+ Account.create :period => {:duration => '2 days', :from => Date.today }
12
+ end
13
+
14
+ describe '.start_date' do
15
+ it 'should default to today' do
16
+ DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'mongoid'
3
+ require 'bson'
4
+
5
+ Mongoid.configure.master = Mongo::Connection.new.db('timespan')
6
+
7
+ Mongoid.database.collections.each do |coll|
8
+ coll.remove
9
+ end
10
+
11
+ require 'timespan/mongoid'
12
+ require 'timespan/mongoid/account'
data/timespan.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
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"]
@@ -36,6 +36,9 @@ Gem::Specification.new do |s|
36
36
  "spec/spec_helper.rb",
37
37
  "spec/timespan/compare_spec.rb",
38
38
  "spec/timespan/locales/duration_da.yml",
39
+ "spec/timespan/mongoid/account.rb",
40
+ "spec/timespan/mongoid/mongoid_timespan_spec.rb",
41
+ "spec/timespan/mongoid/spec_helper.rb",
39
42
  "spec/timespan/printer_spec.rb",
40
43
  "spec/timespan/span_spec.rb",
41
44
  "spec/timespan/units_spec.rb",
@@ -58,6 +61,8 @@ Gem::Specification.new do |s|
58
61
  s.add_runtime_dependency(%q<ruby-duration>, [">= 0"])
59
62
  s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
60
63
  s.add_development_dependency(%q<rails>, ["~> 3.2"])
64
+ s.add_development_dependency(%q<mongoid>, ["~> 2.4"])
65
+ s.add_development_dependency(%q<bson>, ["~> 1.6"])
61
66
  s.add_development_dependency(%q<rdoc>, [">= 3.12"])
62
67
  s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
63
68
  s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
@@ -69,6 +74,8 @@ Gem::Specification.new do |s|
69
74
  s.add_dependency(%q<ruby-duration>, [">= 0"])
70
75
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
71
76
  s.add_dependency(%q<rails>, ["~> 3.2"])
77
+ s.add_dependency(%q<mongoid>, ["~> 2.4"])
78
+ s.add_dependency(%q<bson>, ["~> 1.6"])
72
79
  s.add_dependency(%q<rdoc>, [">= 3.12"])
73
80
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
74
81
  s.add_dependency(%q<jeweler>, [">= 1.8.3"])
@@ -81,6 +88,8 @@ Gem::Specification.new do |s|
81
88
  s.add_dependency(%q<ruby-duration>, [">= 0"])
82
89
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
83
90
  s.add_dependency(%q<rails>, ["~> 3.2"])
91
+ s.add_dependency(%q<mongoid>, ["~> 2.4"])
92
+ s.add_dependency(%q<bson>, ["~> 1.6"])
84
93
  s.add_dependency(%q<rdoc>, [">= 3.12"])
85
94
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
86
95
  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.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -107,6 +107,38 @@ dependencies:
107
107
  - - ~>
108
108
  - !ruby/object:Gem::Version
109
109
  version: '3.2'
110
+ - !ruby/object:Gem::Dependency
111
+ name: mongoid
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '2.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
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'
110
142
  - !ruby/object:Gem::Dependency
111
143
  name: rdoc
112
144
  requirement: !ruby/object:Gem::Requirement
@@ -198,6 +230,9 @@ files:
198
230
  - spec/spec_helper.rb
199
231
  - spec/timespan/compare_spec.rb
200
232
  - spec/timespan/locales/duration_da.yml
233
+ - spec/timespan/mongoid/account.rb
234
+ - spec/timespan/mongoid/mongoid_timespan_spec.rb
235
+ - spec/timespan/mongoid/spec_helper.rb
201
236
  - spec/timespan/printer_spec.rb
202
237
  - spec/timespan/span_spec.rb
203
238
  - spec/timespan/units_spec.rb
@@ -218,7 +253,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
253
  version: '0'
219
254
  segments:
220
255
  - 0
221
- hash: 2914636864152776546
256
+ hash: -3275887994789672968
222
257
  required_rubygems_version: !ruby/object:Gem::Requirement
223
258
  none: false
224
259
  requirements: