Class: EventRecurrenceRule

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/models/event_recurrence_rule.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(rule) ⇒ Object

ical string or RiCal::PropertyValue::RecurrenceRule



70
71
72
73
74
75
# File 'app/models/event_recurrence_rule.rb', line 70

def self.from(rule)         # ical string or RiCal::PropertyValue::RecurrenceRule
  rrule = self.new
  rrule.rule = rule
  rrule.active = true
  rrule
end

Instance Method Details

#activeObject



9
10
11
# File 'app/models/event_recurrence_rule.rb', line 9

def active
  new_record? ? false : read_attribute(:active)
end

#basisObject



13
14
15
16
17
# File 'app/models/event_recurrence_rule.rb', line 13

def basis
  basis = read_attribute(:basis)
  return nil unless basis == 'count' || basis == 'limit'
  basis
end

#ruleObject



61
62
63
64
65
66
67
68
# File 'app/models/event_recurrence_rule.rb', line 61

def rule
  rule = RiCal::PropertyValue::RecurrenceRule.convert(nil, "") unless rule.is_a? RiCal::PropertyValue::RecurrenceRule
  rule.freq = period.upcase
  rule.until = limiting_date if basis == 'limit' && limiting_date
  rule.count = limiting_count if basis == 'count' && limiting_count
  rule.interval = interval
  rule
end

#rule=(rule) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/event_recurrence_rule.rb', line 48

def rule=(rule)
  rule = RiCal::PropertyValue::RecurrenceRule.convert(nil, rule) unless rule.is_a? RiCal::PropertyValue::RecurrenceRule
  self.period = rule.freq
  self.interval = rule.interval
  if rule.until
    self.basis = 'limit'
    self.limiting_date = rule.until.value
  elsif rule.count
    self.basis = 'count'
    self.limiting_count = rule.count
  end
end

#single?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/event_recurrence_rule.rb', line 23

def single?
  true if basis == 'limit' && limiting_count == 1
end

#to_icalObject



77
78
79
# File 'app/models/event_recurrence_rule.rb', line 77

def to_ical
  rule.to_ical
end

#to_sObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/event_recurrence_rule.rb', line 27

def to_s
  summary = []
  if interval > 1
    unit = period.downcase.sub(/ly$/, '')
    unit = "day" if unit == 'dai'
    summary << "every #{interval} #{unit.pluralize}"
  else 
    summary << period.downcase
  end
  if unbounded?
    summary.last << ","
    summary << 'indefinitely'
  elsif basis == 'limit' && limiting_date
    summary << "until #{limiting_date.to_datetime.strftime('%d %B %Y')}" if limiting_date
  elsif basis == 'count' && limiting_count
    summary.last << ","
    summary << "#{limiting_count} times"
  end
  summary.join(' ')
end

#unbounded?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/models/event_recurrence_rule.rb', line 19

def unbounded?
  basis.nil?
end