ice_cube_model 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +5 -5
- data/ice_cube_model.gemspec +1 -1
- data/lib/ice_cube_model/base.rb +20 -16
- data/lib/ice_cube_model/version.rb +1 -1
- data/spec/lib/ice_cube_model/base_spec.rb +67 -21
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b96485ea1cc05ea45c25c4a59d311a583697ca09
|
4
|
+
data.tar.gz: 4874513770391540298d29be1dfe90c24dd526fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d1cc3024fff992cc68a3e93f421621de8b0db0224c65e38a36a2e708565472df3745d9f082ab05b2083f9783d33dfbb6d1375ea5e1ba3cacbe1f3585fddf1ca
|
7
|
+
data.tar.gz: 4f916dc2e3dc2d28984cc5c7e8849a32c58462ca3f2f73f962996c3257562ca6afcdcf41ebac2252a7f35dbede46670a4071b3349cc9f3a265e7b2662dc3882a
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/ice_cube_model.svg)](https://badge.fury.io/rb/ice_cube_model)
|
2
|
+
|
1
3
|
# ice_cube_model
|
4
|
+
|
2
5
|
Extend any "cron-expression" object with [ice_cube](https://github.com/seejohnrun/ice_cube) (calendar repeating event) capabilities.
|
3
6
|
|
4
7
|
Add ice_cube methods to a class (e.g. active_record, active_model) that has cron expression fields.
|
@@ -49,7 +52,6 @@ appointment.occurrences_between(::Date.new(2015, 3, 5), ::Date.new(2015, 6, 5))
|
|
49
52
|
If needed, you can remap the attributes expected by ice_cube to other attributes or methods on the class.
|
50
53
|
|
51
54
|
```ruby
|
52
|
-
|
53
55
|
# map to another attribute
|
54
56
|
class Appointment
|
55
57
|
attr_accessor
|
@@ -115,11 +117,9 @@ end
|
|
115
117
|
- This gem is a work-in-progress.
|
116
118
|
- `occurrences_between` is the only method currently supported.
|
117
119
|
- Does not yet support all recurrence options. More coming.
|
118
|
-
-
|
120
|
+
- Supports inheritance but will not pick up dynamic changes in parent class parameter mappings.
|
119
121
|
|
120
122
|
## todo
|
121
|
-
- Add support for
|
122
|
-
- Add support for inheritance
|
123
|
-
- Add support for time
|
123
|
+
- Add support for time options
|
124
124
|
- Allow mapping of a single cron expression (string) field, rather than individual fields.
|
125
125
|
|
data/ice_cube_model.gemspec
CHANGED
data/lib/ice_cube_model/base.rb
CHANGED
@@ -12,10 +12,18 @@ module IceCubeModel
|
|
12
12
|
end
|
13
13
|
alias_method :occurrences_between, :events_between
|
14
14
|
|
15
|
+
def respond_to?(name, include_private = false)
|
16
|
+
return true if self.class.repeat_parameter_mappings.values.include?(name.to_sym)
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
15
20
|
private
|
16
21
|
|
17
22
|
def read_repeat_parameter(param_name)
|
18
|
-
|
23
|
+
param_method = self.class.repeat_parameter_mappings[param_name.to_sym] || param_name.to_sym
|
24
|
+
return nil unless respond_to?(param_method)
|
25
|
+
|
26
|
+
send(param_method)
|
19
27
|
end
|
20
28
|
|
21
29
|
def read_repeat_params
|
@@ -24,34 +32,30 @@ module IceCubeModel
|
|
24
32
|
:repeat_year => read_repeat_parameter(:repeat_year),
|
25
33
|
:repeat_month => read_repeat_parameter(:repeat_month),
|
26
34
|
:repeat_day => read_repeat_parameter(:repeat_day),
|
27
|
-
:repeat_weekday => read_repeat_parameter(:repeat_weekday)
|
35
|
+
:repeat_weekday => read_repeat_parameter(:repeat_weekday),
|
36
|
+
:repeat_until => read_repeat_parameter(:repeat_until)
|
28
37
|
}
|
29
38
|
end
|
30
39
|
|
31
40
|
module ClassMethods
|
32
41
|
def repeat_parameter_mappings
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
:repeat_month => :repeat_month,
|
40
|
-
:repeat_day => :repeat_day,
|
41
|
-
:repeat_weekday => :repeat_weekday)
|
42
|
+
@repeat_parameter_mappings ||= begin
|
43
|
+
if superclass.respond_to?(:repeat_parameter_mappings)
|
44
|
+
superclass.repeat_parameter_mappings.dup
|
45
|
+
else
|
46
|
+
{}
|
47
|
+
end
|
42
48
|
end
|
43
|
-
|
44
|
-
class_variable_get(mappings_name)
|
45
49
|
end
|
46
50
|
|
47
51
|
def with_repeat_param(param_name, replacement)
|
48
|
-
repeat_parameter_mappings[param_name] =
|
52
|
+
repeat_parameter_mappings[param_name] = param_sym = "ice_cube_model #{param_name}".to_sym
|
49
53
|
if replacement.is_a?(::Proc)
|
50
|
-
define_method(
|
54
|
+
define_method(param_sym) do
|
51
55
|
replacement.call
|
52
56
|
end
|
53
57
|
else
|
54
|
-
define_method(
|
58
|
+
define_method(param_sym) do
|
55
59
|
send(replacement)
|
56
60
|
end
|
57
61
|
end
|
@@ -10,7 +10,8 @@ describe ::IceCubeModel do
|
|
10
10
|
:repeat_month => nil,
|
11
11
|
:repeat_day => nil,
|
12
12
|
:repeat_weekday => nil,
|
13
|
-
:repeat_week => nil
|
13
|
+
:repeat_week => nil,
|
14
|
+
:repeat_until => nil
|
14
15
|
}.merge(attributes)
|
15
16
|
end
|
16
17
|
|
@@ -24,6 +25,10 @@ describe ::IceCubeModel do
|
|
24
25
|
|
25
26
|
fail ArgumentError, "Method `#{m}` doesn't exist."
|
26
27
|
end
|
28
|
+
|
29
|
+
def respond_to?(name, _include_private = false)
|
30
|
+
@attributes.key?(name)
|
31
|
+
end
|
27
32
|
end
|
28
33
|
|
29
34
|
class IceCubeObj < HashAttrs
|
@@ -254,6 +259,20 @@ describe ::IceCubeModel do
|
|
254
259
|
end
|
255
260
|
end
|
256
261
|
|
262
|
+
context 'every month until date' do
|
263
|
+
let(:ice_cube_model) do
|
264
|
+
::IceCubeObj.new(
|
265
|
+
:repeat_start_date => ::Date.new(2015, 1, 1),
|
266
|
+
:repeat_day => '1',
|
267
|
+
:repeat_until => ::Date.new(2015, 3, 1)
|
268
|
+
)
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'ends on specified end date' do
|
272
|
+
expect(ice_cube_model.events_between(::Date.new(2015, 1, 1), ::Date.new(2015, 6, 30))).to eq([::Date.new(2015, 1, 1), ::Date.new(2015, 2, 1), ::Date.new(2015, 3, 1)])
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
257
276
|
context 'input types' do
|
258
277
|
let(:ice_cube_model) do
|
259
278
|
::IceCubeObj.new(
|
@@ -290,7 +309,7 @@ describe ::IceCubeModel do
|
|
290
309
|
|
291
310
|
with_repeat_param(:repeat_start_date, :start_date) # remap attribute to another
|
292
311
|
with_repeat_param(:repeat_interval, :interval) # remap attribute to a method
|
293
|
-
with_repeat_param(:repeat_day, -> {
|
312
|
+
with_repeat_param(:repeat_day, -> { 5 }) # map parameter to lambda
|
294
313
|
|
295
314
|
def initialize(options = {})
|
296
315
|
super(
|
@@ -313,35 +332,62 @@ describe ::IceCubeModel do
|
|
313
332
|
end
|
314
333
|
|
315
334
|
it 'should use class mappings' do
|
316
|
-
expect(ice_cube_model.events_between(::Date.new(2015, 1, 1), ::Date.new(2015, 12, 31))).to eq([::Date.new(2015, 2,
|
335
|
+
expect(ice_cube_model.events_between(::Date.new(2015, 1, 1), ::Date.new(2015, 12, 31))).to eq([::Date.new(2015, 2, 5)])
|
317
336
|
end
|
318
337
|
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
338
|
+
context 'with inheritance' do
|
339
|
+
class IceCubeObjWithParameterMappingsChild < IceCubeObjWithParameterMappings
|
340
|
+
with_repeat_param(:repeat_start_date, :starting) # remap attribute to another
|
341
|
+
with_repeat_param(:repeat_interval, :interval) # remap attribute to a method
|
342
|
+
# with_repeat_param(:repeat_day, -> { 5 }) # comes from parent ::IceCubeObjWithParameterMappings
|
343
|
+
|
344
|
+
def initialize(options = {})
|
345
|
+
super(
|
346
|
+
{
|
347
|
+
:starting => nil
|
348
|
+
}.merge(options)
|
349
|
+
)
|
350
|
+
end
|
351
|
+
|
352
|
+
def interval
|
353
|
+
2
|
354
|
+
end
|
355
|
+
end
|
323
356
|
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
:starting => nil
|
328
|
-
}.merge(options)
|
357
|
+
let(:ice_cube_model) do
|
358
|
+
::IceCubeObjWithParameterMappingsChild.new(
|
359
|
+
:starting => ::Date.new(2015, 1, 1)
|
329
360
|
)
|
330
361
|
end
|
331
362
|
|
332
|
-
|
333
|
-
|
363
|
+
it 'should inherit mappings and remap new mappings' do
|
364
|
+
expect(ice_cube_model.events_between(::Date.new(2015, 1, 1), ::Date.new(2015, 4, 30))).to eq([::Date.new(2015, 1, 5), ::Date.new(2015, 3, 5)])
|
334
365
|
end
|
335
366
|
end
|
336
367
|
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
:repeat_day => 2
|
341
|
-
)
|
368
|
+
context 'with missing parameters' do
|
369
|
+
class IceCubeObjWithMissingParameters
|
370
|
+
include ::IceCubeModel::Base
|
342
371
|
|
343
|
-
|
344
|
-
|
372
|
+
attr_accessor :repeat_start_date
|
373
|
+
attr_accessor :repeat_day
|
374
|
+
end
|
375
|
+
|
376
|
+
it 'should work when class missing parameters' do
|
377
|
+
ice_cube_model = ::IceCubeObjWithMissingParameters.new
|
378
|
+
|
379
|
+
ice_cube_model.repeat_start_date = ::Date.new(2015, 1, 1)
|
380
|
+
ice_cube_model.repeat_day = 2
|
381
|
+
|
382
|
+
expect(ice_cube_model.events_between(::Date.new(2015, 1, 1), ::Date.new(2015, 2, 28))).to eq([::Date.new(2015, 1, 2), ::Date.new(2015, 2, 2)])
|
383
|
+
end
|
384
|
+
|
385
|
+
it '#respond_to?' do
|
386
|
+
ice_cube_model = ::IceCubeObjWithMissingParameters.new
|
387
|
+
expect(ice_cube_model.respond_to?(:repeat_day)).to be(true)
|
388
|
+
expect(ice_cube_model.respond_to?(:repeat_interval)).to be(false)
|
389
|
+
expect(ice_cube_model.respond_to?(:methods)).to be(true)
|
390
|
+
end
|
345
391
|
end
|
346
392
|
end
|
347
393
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ice_cube_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Nichols
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ice_cube_cron
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.0.
|
19
|
+
version: 0.0.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.0.
|
26
|
+
version: 0.0.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|