timespan 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -3
- data/VERSION +1 -1
- data/lib/timespan/mongoid/timespanned.rb +81 -27
- data/spec/timespan/mongoid/models/account_3x.rb +1 -1
- data/spec/timespan/mongoid/mongoid_search_spec.rb +0 -2
- data/timespan.gemspec +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -113,13 +113,16 @@ Custom Timespan datatype:
|
|
113
113
|
|
114
114
|
`Mongoid::Timespanned` adds the following class level macros:
|
115
115
|
|
116
|
-
* `timespan_methods
|
117
|
-
* `timespan_delegates
|
118
|
-
* `
|
116
|
+
* `timespan_methods target, *names`
|
117
|
+
* `timespan_delegates target, *names`
|
118
|
+
* `timespan_delegate name, target = :period`
|
119
|
+
* `timespan_setters target, *names`
|
120
|
+
* `timespan_setter target, name`
|
119
121
|
|
120
122
|
* `timespan_container_delegates container, timespan_field, *names`
|
121
123
|
* `timespan_container_delegate container, timespan_field, name`
|
122
124
|
|
125
|
+
Note that all the macros, take an `options` Hash as the last argument, where you can set `override: true` in order to override any existing methods. Otherwise an `ArgumentError` exception will be raised, to warn you of a method name conflict!
|
123
126
|
|
124
127
|
```ruby
|
125
128
|
require 'timespan/mongoid'
|
@@ -148,6 +151,8 @@ class TimePeriod
|
|
148
151
|
end
|
149
152
|
```
|
150
153
|
|
154
|
+
Note: See `mongoid_timespan_spec.rb` for more examples of usage, and also see the `ClassMethods` module in `timespanned.rb` :)
|
155
|
+
|
151
156
|
Usage example:
|
152
157
|
|
153
158
|
```ruby
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.5
|
@@ -3,56 +3,110 @@ module Mongoid
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def timespan_methods name
|
7
|
-
timespan_delegates name
|
8
|
-
timespan_setters name
|
9
|
-
end
|
10
|
-
|
11
6
|
# fx Account.timespan_container_delegates :period, :dates, :start, :end
|
12
7
|
# start_date= -> period.dates_start=
|
13
8
|
# end_date= -> period.dates_end=
|
14
9
|
def timespan_container_delegates container, timespan, *names
|
15
|
-
|
10
|
+
options = names.extract_options!
|
11
|
+
names = [:start, :end, :duration] if names.first == :all || names.empty?
|
16
12
|
names.flatten.each do |name|
|
17
|
-
timespan_container_delegate container, timespan, name
|
13
|
+
timespan_container_delegate container, timespan, name, options = {}
|
18
14
|
end
|
19
15
|
end
|
20
16
|
|
21
|
-
def timespan_container_delegate container, timespan, name
|
17
|
+
def timespan_container_delegate container, timespan, name, options = {}
|
18
|
+
override = options[:override]
|
22
19
|
case name.to_sym
|
23
20
|
when :start
|
24
|
-
|
25
|
-
|
21
|
+
meth = "start_date="
|
22
|
+
raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && ! override
|
23
|
+
define_method meth do |date|
|
24
|
+
self.send(container).send("#{timespan}_start=", date)
|
26
25
|
end
|
27
26
|
when :end
|
28
|
-
|
29
|
-
|
27
|
+
meth = "end_date="
|
28
|
+
raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
|
29
|
+
define_method meth do |date|
|
30
|
+
self.send(container).send("#{timespan}_end=", date)
|
30
31
|
end
|
31
32
|
when :duration
|
32
|
-
|
33
|
-
|
33
|
+
meth = "duration="
|
34
|
+
raise ArgumentError, "method duration= already defined on #{self}" if self.respond_to?(meth) && !override
|
35
|
+
define_method meth do |date|
|
36
|
+
self.send(container).send("#{timespan}_duration=", date)
|
34
37
|
end
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
+
def timespan_methods target = :period, *names
|
42
|
+
options = names.extract_options!
|
43
|
+
names = [:start, :end, :duration] if names.first == :all || names.empty?
|
44
|
+
timespan_delegates target, names, options
|
45
|
+
timespan_setters target, names, options
|
46
|
+
end
|
41
47
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
48
|
+
def timespan_delegates target = :period, *names
|
49
|
+
options = names.extract_options!
|
50
|
+
names = names.flatten
|
51
|
+
names = [:start_date, :end_date, :duration] if names.first == :all || names.empty?
|
52
|
+
names.map! do |name|
|
53
|
+
case name.to_sym
|
54
|
+
when :start then :start_date
|
55
|
+
when :end then :end_date
|
56
|
+
else
|
57
|
+
name.to_sym
|
58
|
+
end
|
59
|
+
end
|
60
|
+
names.flatten.each do |name|
|
61
|
+
timespan_delegate name, target, options
|
46
62
|
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def timespan_delegate meth, target = :period, options = {}
|
66
|
+
override = options[:override]
|
67
|
+
raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
|
68
|
+
delegate meth, to: target
|
69
|
+
end
|
47
70
|
|
48
|
-
|
49
|
-
|
50
|
-
|
71
|
+
def timespan_setters target = :period, *names
|
72
|
+
options = names.extract_options!
|
73
|
+
names = [:start, :end, :duration] if names.first == :all || names.empty?
|
74
|
+
|
75
|
+
names.flatten.each do |name|
|
76
|
+
timespan_setter target, name, options
|
51
77
|
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def timespan_setter name, meth_name, options = {}
|
81
|
+
override = options[:override]
|
82
|
+
case meth_name.to_sym
|
83
|
+
when :start
|
84
|
+
meth = "#{name}_start="
|
85
|
+
raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
|
86
|
+
|
87
|
+
define_method :"#{name}_start=" do |date|
|
88
|
+
options = self.send(name) ? {end_date: self.send(name).end_date} : {}
|
89
|
+
timespan = ::Timespan.new(options.merge(start_date: date))
|
90
|
+
self.send "#{name}=", timespan
|
91
|
+
end
|
92
|
+
when :end
|
93
|
+
meth = "#{name}_end="
|
94
|
+
raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
|
52
95
|
|
53
|
-
|
54
|
-
|
55
|
-
|
96
|
+
define_method :"#{name}_end=" do |date|
|
97
|
+
options = self.send(name) ? {start_date: self.send(name).start_date} : {}
|
98
|
+
timespan = ::Timespan.new(options.merge(end_date: date))
|
99
|
+
self.send "#{name}=", timespan
|
100
|
+
end
|
101
|
+
when :duration
|
102
|
+
meth = "#{name}_duration="
|
103
|
+
raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
|
104
|
+
|
105
|
+
define_method :"#{name}_duration=" do |duration|
|
106
|
+
options = self.send(name) ? {start_date: self.send(name).start_date} : {}
|
107
|
+
timespan = ::Timespan.new(options.merge(duration: duration))
|
108
|
+
self.send "#{name}=", timespan
|
109
|
+
end
|
56
110
|
end
|
57
111
|
end
|
58
112
|
end
|
@@ -9,7 +9,7 @@ class Account
|
|
9
9
|
timespan_methods :period
|
10
10
|
|
11
11
|
embeds_one :time_period
|
12
|
-
timespan_container_delegates :time_period, :dates, :all#:start, :end
|
12
|
+
timespan_container_delegates :time_period, :dates, :all #:start, :end
|
13
13
|
|
14
14
|
def self.create_it! duration
|
15
15
|
acc = self.new period: ::Timespan.new(duration: duration), time_period: ::TimePeriod.new
|
@@ -9,8 +9,6 @@ describe TimeSpan do
|
|
9
9
|
@acc1 = Account.create :period => Timespan.new(:start_date => 2.days.ago)
|
10
10
|
@acc2 = Account.create :period => Timespan.new(:start_date => 5.days.ago, :end_date => 2.days.ago)
|
11
11
|
@acc3 = Account.create :period => Timespan.new(:start_date => 10.days.ago)
|
12
|
-
|
13
|
-
# puts [@acc1._id, @acc2._id, @acc3._id]
|
14
12
|
end
|
15
13
|
|
16
14
|
describe 'find accounts within specific period' do
|
data/timespan.gemspec
CHANGED
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.
|
4
|
+
version: 0.4.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -248,7 +248,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
248
248
|
version: '0'
|
249
249
|
segments:
|
250
250
|
- 0
|
251
|
-
hash:
|
251
|
+
hash: 4117334554078744124
|
252
252
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
253
253
|
none: false
|
254
254
|
requirements:
|