timespan 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -107,9 +107,19 @@ da:
107
107
 
108
108
  ## Timespan for Mongoid
109
109
 
110
- Tested and works with Mongoid 2.4 and 3.0.0.rc
110
+ Tested and works with Mongoid 2.4 and 3.0+
111
+
112
+ Custom Timespan datatype:
113
+
114
+ `Mongoid::Timespanned` adds the following class level macros:
115
+
116
+ * `timespan_methods name`
117
+ * `timespan_delegates name`
118
+ * `timespan_setters name`
119
+
120
+ * `timespan_container_delegates container, timespan_field, *names`
121
+ * `timespan_container_delegate container, timespan_field, name`
111
122
 
112
- Custom Timespan datatype
113
123
 
114
124
  ```ruby
115
125
  require 'timespan/mongoid'
@@ -118,17 +128,25 @@ class Account
118
128
  include Mongoid::Document
119
129
  include Mongoid::Timespanned
120
130
 
121
- field :period, :type => TimeSpan
131
+ field :period, :type => Timespan
122
132
 
123
133
  timespan_methods :period
134
+
135
+ embeds_one :time_period
136
+ timespan_container_delegates :time_period, :dates, :start, :end
124
137
  end
125
- ```
126
138
 
127
- `Mongoid::Timespanned` adds the following class level macros:
139
+ class TimePeriod
140
+ include Mongoid::Document
141
+ include Mongoid::Timespanned
128
142
 
129
- * `timespan_methods name`
130
- * `timespan_delegates name`
131
- * `timespan_setters name`
143
+ field :dates, :type => ::Timespan, :between => true
144
+
145
+ embedded_in :account
146
+
147
+ timespan_methods :dates
148
+ end
149
+ ```
132
150
 
133
151
  Usage example:
134
152
 
@@ -140,11 +158,17 @@ account.period.end_date
140
158
  account.period.days
141
159
  account.period.duration # => Duration
142
160
 
161
+ # using timespan setters defined by timespan_methods
143
162
  account.period_start = tomorrow
144
163
  account.period_end = 5.days.from_now
145
164
 
165
+ # using timespan delegates defined by timespan_methods
146
166
  account.start_date == tomorrow
147
167
  account.end_date == tomorrow
168
+
169
+ # using timespan_container_delegates on time_period
170
+ account.start_date = tomorrow
171
+ account.end_date = tomorrow + 5.days
148
172
  ```
149
173
 
150
174
  ## Searching periods
@@ -162,7 +186,7 @@ Make it easier by introducing a class helper:
162
186
  ```ruby
163
187
  class Account
164
188
  include Mongoid::Document
165
- field :period, :type => TimeSpan
189
+ field :period, :type => Timespan
166
190
 
167
191
  def self.between from, to
168
192
  Account.where(:'period.from'.gt => from.to_i, :'period.to'.lte => to.to_i)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.4.4
@@ -8,21 +8,51 @@ module Mongoid
8
8
  timespan_setters name
9
9
  end
10
10
 
11
- def timespan_delegates name = :period
11
+ # fx Account.timespan_container_delegates :period, :dates, :start, :end
12
+ # start_date= -> period.dates_start=
13
+ # end_date= -> period.dates_end=
14
+ def timespan_container_delegates container, timespan, *names
15
+ names = [:start, :end, :duration] if names.first == :all
16
+ names.flatten.each do |name|
17
+ timespan_container_delegate container, timespan, name
18
+ end
19
+ end
20
+
21
+ def timespan_container_delegate container, timespan, name
22
+ case name.to_sym
23
+ when :start
24
+ define_method "start_date=" do |date|
25
+ send(container).send("#{timespan}_start=", date)
26
+ end
27
+ when :end
28
+ define_method "end_date=" do |date|
29
+ send(container).send("#{timespan}_end=", date)
30
+ end
31
+ when :duration
32
+ define_method "duration=" do |date|
33
+ send(container).send("#{timespan}_duration=", date)
34
+ end
35
+ end
36
+ end
37
+
38
+ def timespan_delegates name = :period
12
39
  delegate :time_left, :duration, :start_date, :end_date, to: name
13
40
  end
14
41
 
15
42
  def timespan_setters name = :period
16
43
  define_method :"#{name}_start=" do |date|
17
- self.send "#{name}=", ::Timespan.new(start_date: date, end_date: self.send(name).end_date)
44
+ options = self.send(name) ? {end_date: self.send(name).end_date} : {}
45
+ self.send "#{name}=", ::Timespan.new(options.merge(start_date: date))
18
46
  end
19
47
 
20
48
  define_method :"#{name}_end=" do |date|
21
- self.send "#{name}=", ::Timespan.new(start_date: self.send(name).start_date, end_date: date)
49
+ options = self.send(name) ? {start_date: self.send(name).start_date} : {}
50
+ self.send "#{name}=", ::Timespan.new(options.merge(end_date: date))
22
51
  end
23
52
 
24
- define_method :"#{name}duration=" do |duration|
25
- self.send "#{name}=", ::Timespan.new(start_date: self.send(name).start_date, duration: duration)
53
+ define_method :"#{name}_duration=" do |duration|
54
+ options = self.send(name) ? {start_date: self.send(name).start_date} : {}
55
+ self.send "#{name}=", ::Timespan.new(options.merge(duration: duration))
26
56
  end
27
57
  end
28
58
  end
@@ -1,3 +1,5 @@
1
+ require 'timespan/mongoid/models/time_period'
2
+
1
3
  class Account
2
4
  include Mongoid::Document
3
5
  include Mongoid::Timespanned
@@ -6,9 +8,13 @@ class Account
6
8
 
7
9
  timespan_methods :period
8
10
 
11
+ embeds_one :time_period
12
+ timespan_container_delegates :time_period, :dates, :all#:start, :end
13
+
9
14
  def self.create_it! duration
10
- t = ::Timespan.new(duration: duration)
11
- self.new period: t
15
+ acc = self.new period: ::Timespan.new(duration: duration), time_period: ::TimePeriod.new
16
+ acc.time_period.dates_duration = 1.day
17
+ acc
12
18
  end
13
19
 
14
20
  def self.between from, to
@@ -0,0 +1,10 @@
1
+ class TimePeriod
2
+ include Mongoid::Document
3
+ include Mongoid::Timespanned
4
+
5
+ field :dates, :type => ::Timespan, :between => true
6
+
7
+ embedded_in :account
8
+
9
+ timespan_methods :dates
10
+ end
@@ -44,16 +44,37 @@ describe TimeSpan do
44
44
  DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
45
45
  end
46
46
  end
47
+ end
48
+
49
+ context 'Setters and delegates' do
50
+ let(:account) do
51
+ Account.create_it! 2.days
52
+ end
47
53
 
48
54
  describe 'set new start_date' do
49
55
  before :each do
50
56
  subject.period_start = tomorrow
57
+ subject.period_end = tomorrow + 5.days
58
+
59
+ subject.end_date = tomorrow + 3.days
60
+ end
61
+
62
+ specify do
63
+ Date.parse(subject.time_period.end_date.to_s).should == tomorrow + 3.days
64
+ end
65
+
66
+ specify do
67
+ subject.period.should be_a Timespan
51
68
  end
52
69
 
53
70
  specify do
54
71
  subject.start_date.should == subject.period.start_date
55
72
  end
56
73
 
74
+ specify do
75
+ subject.end_date.should == subject.period.end_date
76
+ end
77
+
57
78
  specify do
58
79
  Date.parse(subject.start_date.to_s).should == tomorrow
59
80
  end
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.4.3"
8
+ s.version = "0.4.4"
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"]
@@ -42,6 +42,7 @@ Gem::Specification.new do |s|
42
42
  "spec/timespan/locales/duration_da.yml",
43
43
  "spec/timespan/mongoid/models/account_2x.rb",
44
44
  "spec/timespan/mongoid/models/account_3x.rb",
45
+ "spec/timespan/mongoid/models/time_period.rb",
45
46
  "spec/timespan/mongoid/mongoid_search_spec.rb",
46
47
  "spec/timespan/mongoid/mongoid_setup.rb",
47
48
  "spec/timespan/mongoid/mongoid_timespan_spec.rb",
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.4.3
4
+ version: 0.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -220,6 +220,7 @@ files:
220
220
  - spec/timespan/locales/duration_da.yml
221
221
  - spec/timespan/mongoid/models/account_2x.rb
222
222
  - spec/timespan/mongoid/models/account_3x.rb
223
+ - spec/timespan/mongoid/models/time_period.rb
223
224
  - spec/timespan/mongoid/mongoid_search_spec.rb
224
225
  - spec/timespan/mongoid/mongoid_setup.rb
225
226
  - spec/timespan/mongoid/mongoid_timespan_spec.rb
@@ -247,7 +248,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
247
248
  version: '0'
248
249
  segments:
249
250
  - 0
250
- hash: -3678905643700355626
251
+ hash: 1165470586481426530
251
252
  required_rubygems_version: !ruby/object:Gem::Requirement
252
253
  none: false
253
254
  requirements: