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
- #==(other) ⇒ Object
- #hash ⇒ Object
- #next_time(time, schedule, closing_time) ⇒ Object
- #on?(time, schedule) ⇒ Boolean
- #reset ⇒ Object
-
#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.
10 11 12 |
# File 'lib/ice_cube/rule.rb', line 10 def uses @uses end |
Class Method Details
.daily(interval = 1) ⇒ Object
Daily Rule
118 119 120 |
# File 'lib/ice_cube/rule.rb', line 118 def daily(interval = 1) DailyRule.new(interval) end |
.from_hash(original_hash) ⇒ Object
Convert from a hash and create a rule
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ice_cube/rule.rb', line 61 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) rule.interval(hash[:interval] || 1, TimeUtil.wday_to_sym(hash[:week_start] || 0)) if rule.is_a? WeeklyRule rule.until(TimeUtil.deserialize_time(hash[:until])) if hash[:until] rule.count(hash[:count]) if hash[:count] 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
34 35 36 |
# File 'lib/ice_cube/rule.rb', line 34 def self.from_ical(ical) IceCube::IcalParser.rule_from_ical(ical) end |
.from_yaml(yaml) ⇒ Object
From yaml
44 45 46 |
# File 'lib/ice_cube/rule.rb', line 44 def self.from_yaml(yaml) from_hash YAML.safe_load(yaml, permitted_classes: [Date, Symbol, Time]) end |
.hourly(interval = 1) ⇒ Object
Hourly Rule
113 114 115 |
# File 'lib/ice_cube/rule.rb', line 113 def hourly(interval = 1) HourlyRule.new(interval) end |
.minutely(interval = 1) ⇒ Object
Minutely Rule
108 109 110 |
# File 'lib/ice_cube/rule.rb', line 108 def minutely(interval = 1) MinutelyRule.new(interval) end |
.monthly(interval = 1) ⇒ Object
Monthly Rule
128 129 130 |
# File 'lib/ice_cube/rule.rb', line 128 def monthly(interval = 1) MonthlyRule.new(interval) end |
.secondly(interval = 1) ⇒ Object
Secondly Rule
103 104 105 |
# File 'lib/ice_cube/rule.rb', line 103 def secondly(interval = 1) SecondlyRule.new(interval) end |
.weekly(interval = 1, week_start = :sunday) ⇒ Object
Weekly Rule
123 124 125 |
# File 'lib/ice_cube/rule.rb', line 123 def weekly(interval = 1, week_start = :sunday) WeeklyRule.new(interval, week_start) end |
.yearly(interval = 1) ⇒ Object
Yearly Rule
133 134 135 |
# File 'lib/ice_cube/rule.rb', line 133 def yearly(interval = 1) YearlyRule.new(interval) end |
Instance Method Details
#==(other) ⇒ Object
20 21 22 23 |
# File 'lib/ice_cube/rule.rb', line 20 def ==(other) return false unless other.is_a? Rule hash == other.hash end |
#hash ⇒ Object
25 26 27 |
# File 'lib/ice_cube/rule.rb', line 25 def hash to_hash.hash end |
#next_time(time, schedule, closing_time) ⇒ Object
52 53 |
# File 'lib/ice_cube/rule.rb', line 52 def next_time(time, schedule, closing_time) end |
#on?(time, schedule) ⇒ Boolean
55 56 57 |
# File 'lib/ice_cube/rule.rb', line 55 def on?(time, schedule) next_time(time, schedule, time).to_i == time.to_i end |
#reset ⇒ Object
12 13 |
# File 'lib/ice_cube/rule.rb', line 12 def reset end |
#terminating? ⇒ Boolean
Is this a terminating schedule?
16 17 18 |
# File 'lib/ice_cube/rule.rb', line 16 def terminating? until_time || occurrence_count end |
#to_hash ⇒ Object
48 49 50 |
# File 'lib/ice_cube/rule.rb', line 48 def to_hash raise MethodNotImplemented, "Expected to be overridden by subclasses" end |
#to_ical ⇒ Object
29 30 31 |
# File 'lib/ice_cube/rule.rb', line 29 def to_ical raise MethodNotImplemented, "Expected to be overridden by subclasses" end |
#to_yaml(*args) ⇒ Object
Yaml implementation
39 40 41 |
# File 'lib/ice_cube/rule.rb', line 39 def to_yaml(*args) YAML.dump(to_hash, *args) end |