Class: IceCube::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/ice_cube/rule.rb

Direct Known Subclasses

SingleOccurrenceRule, ValidatedRule

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#usesObject (readonly)

Returns the value of attribute uses.



7
8
9
# File 'lib/ice_cube/rule.rb', line 7

def uses
  @uses
end

Class Method Details

.daily(interval = 1) ⇒ Object

Daily Rule



159
160
161
# File 'lib/ice_cube/rule.rb', line 159

def daily(interval = 1)
  DailyRule.new(interval)
end

.from_hash(hash) ⇒ Object

Convert from a hash and create a rule



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ice_cube/rule.rb', line 111

def self.from_hash(hash)
  return nil unless match = hash[:rule_type].match(/\:\:(.+?)Rule/)
  rule = IceCube::Rule.send(match[1].downcase.to_sym, hash[:interval] || 1)
  rule.until(TimeUtil.deserialize_time(hash[:until])) if hash[:until]
  rule.count(hash[:count]) if hash[:count]
  hash[:validations] && hash[:validations].each do |key, value|
    key = key.to_sym unless key.is_a?(Symbol)
    value.is_a?(::Array) ? rule.send(key, *value) : rule.send(key, value)
  end
  rule
end

.from_ical(ical) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
86
87
88
89
90
91
92
93
# File 'lib/ice_cube/rule.rb', line 31

def self.from_ical ical
  params = {:validations => {}}
  
  ical.split(';').each do |rule|
    (name, value) = rule.split('=')
    case name
    when 'FREQ'
      params[:freq] = value.downcase
    when 'INTERVAL'
      params[:interval] = value.to_i
    when 'COUNT'
      params[:count] = value.to_i
    when 'UNTIL'
      params[:until] = DateTime.parse(value).to_time.utc
    when 'WKST'
      params[:wkst] = TimeUtil.ical_day_to_symbol(value)
      
    when 'BYSECOND'
      params[:validations][:second_of_minute] = value.split(',').collect{ |v| v.to_i }
    when "BYMINUTE"
      params[:validations][:minute_of_hour] = value.split(',').collect{ |v| v.to_i }
    when "BYHOUR"
      params[:validations][:hour_of_day] = value.split(',').collect{ |v| v.to_i }
    when "BYDAY"
      dows = {}
      days = []
      value.split(',').each do |expr|
          day = TimeUtil.ical_day_to_symbol(expr.strip[-2..-1])
          if expr.strip.length > 2  # day with occurence
            occ = expr[0..-3].to_i 
            dows[day].nil? ? dows[day] = [occ] : dows[day].push(occ)
            days.delete(TimeUtil.symbol_to_day(day))
          else
            days.push TimeUtil.symbol_to_day(day) if dows[day].nil?
          end
      end
      params[:validations][:day_of_week] = dows unless dows.empty?
      params[:validations][:day] = days unless days.empty?
    when "BYMONTHDAY"
      params[:validations][:day_of_month] = value.split(',').collect{ |v| v.to_i }
    when "BYMONTH"
      params[:validations][:month_of_year] = value.split(',').collect{ |v| v.to_i }
    when "BYYEARDAY"
      params[:validations][:day_of_year] = value.split(',').collect{ |v| v.to_i }
      
    else
      raise "Invalid or unsupported rrule command : #{name}"
    end
  end

  params[:interval] ||= 1
  # WKST only valid for weekly rules
  params.delete(:wkst) unless params[:freq] == 'weekly'

  rule = IceCube::Rule.send(*params.values_at(:freq, :interval, :wkst).compact)
  rule.count(params[:count]) if params[:count]
  rule.until(params[:until]) if params[:until]
  params[:validations].each do |key, value|
    value.is_a?(Array) ? rule.send(key, *value) : rule.send(key, value)
  end

  rule
end

.from_yaml(yaml) ⇒ Object

From yaml



101
102
103
# File 'lib/ice_cube/rule.rb', line 101

def self.from_yaml(yaml)
  from_hash IceCube::use_psych? ? Psych::load(yaml) : YAML::load(yaml)
end

.hourly(interval = 1) ⇒ Object

Hourly Rule



154
155
156
# File 'lib/ice_cube/rule.rb', line 154

def hourly(interval = 1)
  HourlyRule.new(interval)
end

.minutely(interval = 1) ⇒ Object

Minutely Rule



149
150
151
# File 'lib/ice_cube/rule.rb', line 149

def minutely(interval = 1)
  MinutelyRule.new(interval)
end

.monthly(interval = 1) ⇒ Object

Monthly Rule



169
170
171
# File 'lib/ice_cube/rule.rb', line 169

def monthly(interval = 1)
  MonthlyRule.new(interval)
end

.secondly(interval = 1) ⇒ Object

Secondly Rule



144
145
146
# File 'lib/ice_cube/rule.rb', line 144

def secondly(interval = 1)
  SecondlyRule.new(interval)
end

.weekly(interval = 1, week_start = :sunday) ⇒ Object

Weekly Rule



164
165
166
# File 'lib/ice_cube/rule.rb', line 164

def weekly(interval = 1, week_start = :sunday)
  WeeklyRule.new(interval, week_start)
end

.yearly(interval = 1) ⇒ Object

Yearly Rule



174
175
176
# File 'lib/ice_cube/rule.rb', line 174

def yearly(interval = 1)
  YearlyRule.new(interval)
end

Instance Method Details

#==(rule) ⇒ Object



14
15
16
17
18
19
# File 'lib/ice_cube/rule.rb', line 14

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

Returns:

  • (Boolean)


136
137
138
# File 'lib/ice_cube/rule.rb', line 136

def full_required?
  !@count.nil?
end

#hashObject



21
22
23
24
# File 'lib/ice_cube/rule.rb', line 21

def hash
  h = to_hash
  h.nil? ? super : h.hash
end

#next_time(time, schedule, closing_time) ⇒ Object



128
129
# File 'lib/ice_cube/rule.rb', line 128

def next_time(time, schedule, closing_time)
end

#on?(time, schedule) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/ice_cube/rule.rb', line 131

def on?(time, schedule)
  next_time(time, schedule, time) == time
end

#resetObject

Reset the uses on the rule to 0



124
125
126
# File 'lib/ice_cube/rule.rb', line 124

def reset
  @uses = 0
end

#terminating?Boolean

Is this a terminating schedule?

Returns:

  • (Boolean)


10
11
12
# File 'lib/ice_cube/rule.rb', line 10

def terminating?
  until_time || occurrence_count
end

#to_hashObject

Expected to be overridden by subclasses



106
107
108
# File 'lib/ice_cube/rule.rb', line 106

def to_hash
  nil
end

#to_icalObject

Expected to be overridden by subclasses



27
28
29
# File 'lib/ice_cube/rule.rb', line 27

def to_ical
  nil
end

#to_yaml(*args) ⇒ Object

Yaml implementation



96
97
98
# File 'lib/ice_cube/rule.rb', line 96

def to_yaml(*args)
  IceCube::use_psych? ? Psych::dump(to_hash) : YAML::dump(to_hash, *args)
end