Class: IceCube::Rule
- Inherits:
-
Object
- Object
- IceCube::Rule
- Defined in:
- lib/ice_cube/rule.rb
Direct Known Subclasses
Constant Summary collapse
- INTERVAL_TYPES =
[ :secondly, :minutely, :hourly, :daily, :weekly, :monthly, :yearly ]
Instance Attribute Summary collapse
-
#uses ⇒ Object
readonly
Returns the value of attribute uses.
Class Method Summary collapse
-
.daily(interval = 1) ⇒ Object
Daily Rule.
-
.from_hash(original_hash) ⇒ Object
Convert from a hash and create a rule.
-
.from_ical(ical) ⇒ Object
Convert from ical string and create a rule.
-
.from_yaml(yaml) ⇒ Object
From yaml.
-
.hourly(interval = 1) ⇒ Object
Hourly Rule.
-
.minutely(interval = 1) ⇒ Object
Minutely Rule.
-
.monthly(interval = 1) ⇒ Object
Monthly Rule.
-
.secondly(interval = 1) ⇒ Object
Secondly Rule.
-
.weekly(interval = 1, week_start = :sunday) ⇒ Object
Weekly Rule.
-
.yearly(interval = 1) ⇒ Object
Yearly Rule.
Instance Method Summary collapse
- #==(rule) ⇒ Object
-
#full_required? ⇒ Boolean
Whether this rule requires a full run.
- #hash ⇒ Object
- #next_time(time, schedule, closing_time) ⇒ Object
- #on?(time, schedule) ⇒ Boolean
-
#overrides_duration? ⇒ Boolean
Does this rule override the schedule’s duration?.
-
#reset ⇒ Object
Reset the uses on the rule to 0.
-
#terminating? ⇒ Boolean
Is this a terminating schedule?.
- #to_hash ⇒ Object
- #to_ical ⇒ Object
-
#to_yaml(*args) ⇒ Object
Yaml implementation.
Instance Attribute Details
#uses ⇒ Object (readonly)
Returns the value of attribute uses.
12 13 14 |
# File 'lib/ice_cube/rule.rb', line 12 def uses @uses end |
Class Method Details
.daily(interval = 1) ⇒ Object
Daily Rule
141 142 143 |
# File 'lib/ice_cube/rule.rb', line 141 def daily(interval = 1) DailyRule.new(interval) end |
.from_hash(original_hash) ⇒ Object
Convert from a hash and create a rule
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ice_cube/rule.rb', line 79 def from_hash(original_hash) hash = IceCube::FlexibleHash.new original_hash unless hash[:rule_type] && match = hash[:rule_type].match(/\:\:(.+?)Rule/) raise ArgumentError, 'Invalid rule type' end interval_type = match[1].downcase.to_sym unless INTERVAL_TYPES.include?(interval_type) raise ArgumentError, "Invalid rule frequency type: #{match[1]}" end rule = IceCube::Rule.send(interval_type, hash[:interval] || 1) if match[1] == "Weekly" rule.interval(hash[:interval] || 1, TimeUtil.wday_to_sym(hash[:week_start] || 0)) end rule.until(TimeUtil.deserialize_time(hash[:until])) if hash[:until] rule.count(hash[:count]) if hash[:count] hash[:validations] && hash[:validations].each do |name, args| apply_validation(rule, name, args) end rule end |
.from_ical(ical) ⇒ Object
Convert from ical string and create a rule
41 42 43 |
# File 'lib/ice_cube/rule.rb', line 41 def self.from_ical(ical) IceCube::IcalParser.rule_from_ical(ical) end |
.from_yaml(yaml) ⇒ Object
From yaml
51 52 53 |
# File 'lib/ice_cube/rule.rb', line 51 def self.from_yaml(yaml) from_hash YAML::load(yaml) end |
.hourly(interval = 1) ⇒ Object
Hourly Rule
136 137 138 |
# File 'lib/ice_cube/rule.rb', line 136 def hourly(interval = 1) HourlyRule.new(interval) end |
.minutely(interval = 1) ⇒ Object
Minutely Rule
131 132 133 |
# File 'lib/ice_cube/rule.rb', line 131 def minutely(interval = 1) MinutelyRule.new(interval) end |
.monthly(interval = 1) ⇒ Object
Monthly Rule
151 152 153 |
# File 'lib/ice_cube/rule.rb', line 151 def monthly(interval = 1) MonthlyRule.new(interval) end |
.secondly(interval = 1) ⇒ Object
Secondly Rule
126 127 128 |
# File 'lib/ice_cube/rule.rb', line 126 def secondly(interval = 1) SecondlyRule.new(interval) end |
.weekly(interval = 1, week_start = :sunday) ⇒ Object
Weekly Rule
146 147 148 |
# File 'lib/ice_cube/rule.rb', line 146 def weekly(interval = 1, week_start = :sunday) WeeklyRule.new(interval, week_start) end |
.yearly(interval = 1) ⇒ Object
Yearly Rule
156 157 158 |
# File 'lib/ice_cube/rule.rb', line 156 def yearly(interval = 1) YearlyRule.new(interval) end |
Instance Method Details
#==(rule) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/ice_cube/rule.rb', line 24 def ==(rule) if rule.is_a? Rule hash = to_hash hash && hash == rule.to_hash end end |
#full_required? ⇒ Boolean
Whether this rule requires a full run
72 73 74 |
# File 'lib/ice_cube/rule.rb', line 72 def full_required? !@count.nil? end |
#hash ⇒ Object
31 32 33 34 |
# File 'lib/ice_cube/rule.rb', line 31 def hash h = to_hash h.nil? ? super : h.hash end |
#next_time(time, schedule, closing_time) ⇒ Object
64 65 |
# File 'lib/ice_cube/rule.rb', line 64 def next_time(time, schedule, closing_time) end |
#on?(time, schedule) ⇒ Boolean
67 68 69 |
# File 'lib/ice_cube/rule.rb', line 67 def on?(time, schedule) next_time(time, schedule, time).to_i == time.to_i end |
#overrides_duration? ⇒ Boolean
Does this rule override the schedule’s duration?
20 21 22 |
# File 'lib/ice_cube/rule.rb', line 20 def overrides_duration? !duration.nil? end |
#reset ⇒ Object
Reset the uses on the rule to 0
60 61 62 |
# File 'lib/ice_cube/rule.rb', line 60 def reset @uses = 0 end |
#terminating? ⇒ Boolean
Is this a terminating schedule?
15 16 17 |
# File 'lib/ice_cube/rule.rb', line 15 def terminating? until_time || occurrence_count end |
#to_hash ⇒ Object
55 56 57 |
# File 'lib/ice_cube/rule.rb', line 55 def to_hash raise MethodNotImplemented, "Expected to be overridden by subclasses" end |
#to_ical ⇒ Object
36 37 38 |
# File 'lib/ice_cube/rule.rb', line 36 def to_ical raise MethodNotImplemented, "Expected to be overrridden by subclasses" end |
#to_yaml(*args) ⇒ Object
Yaml implementation
46 47 48 |
# File 'lib/ice_cube/rule.rb', line 46 def to_yaml(*args) YAML::dump(to_hash, *args) end |