Class: IceCube::Rule
- Inherits:
-
Object
- Object
- IceCube::Rule
- Includes:
- ValidationTypes
- Defined in:
- lib/ice_cube/rule.rb
Direct Known Subclasses
DailyRule, HourlyRule, MinutelyRule, MonthlyRule, SecondlyRule, WeeklyRule, YearlyRule
Instance Attribute Summary collapse
-
#occurrence_count ⇒ Object
readonly
Returns the value of attribute occurrence_count.
-
#until_date ⇒ Object
readonly
Returns the value of attribute until_date.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Class Method Summary collapse
-
.daily(interval = 1) ⇒ Object
create a new daily rule.
- .from_hash(hash) ⇒ Object
- .from_yaml(str) ⇒ Object
-
.hourly(interval = 1) ⇒ Object
create a new hourly rule.
-
.minutely(interval = 1) ⇒ Object
create a new minutely rule.
-
.monthly(interval = 1) ⇒ Object
create a new monthly rule.
-
.secondly(interval = 1) ⇒ Object
create a new secondly rule.
-
.weekly(interval = 1) ⇒ Object
create a new weekly rule.
-
.yearly(interval = 1) ⇒ Object
create a new yearly rule.
Instance Method Summary collapse
-
#count(count) ⇒ Object
set the number of occurrences after which this rule is no longer effective.
-
#next_suggestion(date) ⇒ Object
The key - extremely educated guesses This spidering behavior will go through look for the next suggestion by constantly moving the farthest back value forward.
- #to_hash ⇒ Object
- #to_yaml ⇒ Object
-
#until(until_date) ⇒ Object
Set the time when this rule will no longer be effective.
- #validate_single_date(date) ⇒ Object
Methods included from ValidationTypes
#day, #day_of_month, #day_of_week, #day_of_year, #hour_of_day, #minute_of_hour, #month_of_year, #second_of_minute
Instance Attribute Details
#occurrence_count ⇒ Object (readonly)
Returns the value of attribute occurrence_count.
5 6 7 |
# File 'lib/ice_cube/rule.rb', line 5 def occurrence_count @occurrence_count end |
#until_date ⇒ Object (readonly)
Returns the value of attribute until_date.
5 6 7 |
# File 'lib/ice_cube/rule.rb', line 5 def until_date @until_date end |
#validations ⇒ Object (readonly)
Returns the value of attribute validations.
123 124 125 |
# File 'lib/ice_cube/rule.rb', line 123 def validations @validations end |
Class Method Details
.daily(interval = 1) ⇒ Object
create a new daily rule
38 39 40 |
# File 'lib/ice_cube/rule.rb', line 38 def self.daily(interval = 1) DailyRule.new(interval) end |
.from_hash(hash) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/ice_cube/rule.rb', line 19 def self.from_hash(hash) rule = hash[:rule_type].split('::').inject(Object) { |namespace, const_name| namespace.const_get(const_name) }.new(hash[:interval]) rule.count(hash[:count]) if hash[:count] rule.until(hash[:until]) if hash[:until] hash[:validations].each do |validation, data| data.is_a?(Array) ? rule.send(validation, *data) : rule.send(validation, data) end rule end |
.from_yaml(str) ⇒ Object
33 34 35 |
# File 'lib/ice_cube/rule.rb', line 33 def self.from_yaml(str) from_hash(YAML::load(str)) end |
.hourly(interval = 1) ⇒ Object
create a new hourly rule
58 59 60 |
# File 'lib/ice_cube/rule.rb', line 58 def self.hourly(interval = 1) HourlyRule.new(interval) end |
.minutely(interval = 1) ⇒ Object
create a new minutely rule
63 64 65 |
# File 'lib/ice_cube/rule.rb', line 63 def self.minutely(interval = 1) MinutelyRule.new(interval) end |
.monthly(interval = 1) ⇒ Object
create a new monthly rule
48 49 50 |
# File 'lib/ice_cube/rule.rb', line 48 def self.monthly(interval = 1) MonthlyRule.new(interval) end |
.secondly(interval = 1) ⇒ Object
create a new secondly rule
68 69 70 |
# File 'lib/ice_cube/rule.rb', line 68 def self.secondly(interval = 1) SecondlyRule.new(interval) end |
.weekly(interval = 1) ⇒ Object
create a new weekly rule
43 44 45 |
# File 'lib/ice_cube/rule.rb', line 43 def self.weekly(interval = 1) WeeklyRule.new(interval) end |
.yearly(interval = 1) ⇒ Object
create a new yearly rule
53 54 55 |
# File 'lib/ice_cube/rule.rb', line 53 def self.yearly(interval = 1) YearlyRule.new(interval) end |
Instance Method Details
#count(count) ⇒ Object
set the number of occurrences after which this rule is no longer effective
80 81 82 83 84 |
# File 'lib/ice_cube/rule.rb', line 80 def count(count) raise ArgumentError.new('Argument must be a positive integer') unless Integer(count) && count >= 0 @occurrence_count = count self end |
#next_suggestion(date) ⇒ Object
The key - extremely educated guesses This spidering behavior will go through look for the next suggestion by constantly moving the farthest back value forward
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ice_cube/rule.rb', line 96 def next_suggestion(date) # get the next date recommendation set suggestions = {}; @validation_types.each { |k, validation| suggestions[k] = validation.send(:closest, date) } compact_suggestions = suggestions.values.compact # find the next date to go to if compact_suggestions.empty? next_date = date loop do # keep going through rule suggestions next_date = self.default_jump(next_date) return next_date if validate_single_date(next_date) end else loop do compact_suggestions = suggestions.values.compact min_suggestion = compact_suggestions.min # validate all against the minimum return min_suggestion if validate_single_date(min_suggestion) # move anything that is the minimum to its next closest @validation_types.each do |k, validation| suggestions[k] = validation.send(:closest, min_suggestion) if min_suggestion == suggestions[k] end end end end |
#to_hash ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/ice_cube/rule.rb', line 9 def to_hash hash = Hash.new hash[:rule_type] = self.class.name hash[:interval] = @interval hash[:until] = @until_date hash[:count] = @occurrence_count hash[:validations] = @validations hash end |
#to_yaml ⇒ Object
29 30 31 |
# File 'lib/ice_cube/rule.rb', line 29 def to_yaml to_hash.to_yaml end |
#until(until_date) ⇒ Object
Set the time when this rule will no longer be effective
73 74 75 76 77 |
# File 'lib/ice_cube/rule.rb', line 73 def until(until_date) raise ArgumentError.new('Cannot specify until and count on the same rule') if @count #as per rfc @until_date = until_date self end |
#validate_single_date(date) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/ice_cube/rule.rb', line 86 def validate_single_date(date) @validation_types.values.all? do |validation| response = validation.send(:validate, date) response.nil? || response end end |