Module: ScheduleAtts

Defined in:
lib/schedule_atts.rb,
lib/schedule_atts/version.rb

Constant Summary collapse

DAY_NAMES =

Your code goes hereā€¦

Date::DAYNAMES.map(&:downcase).map(&:to_sym)
VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_in_timezone(str) ⇒ Object

TODO: test this



84
85
86
87
88
89
90
# File 'lib/schedule_atts.rb', line 84

def self.parse_in_timezone(str)
  if Time.respond_to? :zone
    Time.zone.parse(str)
  else
    Time.parse(str)
  end
end

Instance Method Details

#scheduleObject



9
10
11
12
13
14
15
16
17
# File 'lib/schedule_atts.rb', line 9

def schedule
  @schedule ||= begin
    if schedule_yaml.blank?
      IceCube::Schedule.new(Date.today.to_time).tap{|sched| sched.add_recurrence_rule(IceCube::Rule.daily) }
    else
      IceCube::Schedule.from_yaml(schedule_yaml)
    end
  end
end

#schedule_attributesObject



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
# File 'lib/schedule_atts.rb', line 47

def schedule_attributes
  atts = {}

  if rule = schedule.rrules.first
    atts[:repeat]     = 1
    atts[:start_date] = schedule.start_date.to_date
    atts[:date]       = Date.today # for populating the other part of the form

    rule_hash = rule.to_hash
    atts[:interval] = rule_hash[:interval]

    case rule
    when IceCube::DailyRule
      atts[:interval_unit] = 'day'
    when IceCube::WeeklyRule
      atts[:interval_unit] = 'week'
      rule_hash[:validations][:day].each do |day_idx|
        atts[ DAY_NAMES[day_idx] ] = 1
      end
    end

    if rule.until_date
      atts[:until_date] = rule.until_date.to_date
      atts[:ends] = 'eventually'
    else
      atts[:ends] = 'never'
    end
  else
    atts[:repeat]     = 0
    atts[:date]       = schedule.start_date.to_date
    atts[:start_date] = Date.today # for populating the other part of the form
  end

  OpenStruct.new(atts)
end

#schedule_attributes=(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schedule_atts.rb', line 19

def schedule_attributes=(options)
  options = options.dup
  options[:interval] = options[:interval].to_i
  options[:start_date] &&= ScheduleAttributes.parse_in_timezone(options[:start_date])
  options[:date]       &&= ScheduleAttributes.parse_in_timezone(options[:date])
  options[:until_date] &&= ScheduleAttributes.parse_in_timezone(options[:until_date])

  if options[:repeat].to_i == 0
    @schedule = IceCube::Schedule.new(options[:date])
    @schedule.add_recurrence_date(options[:date])
  else
    @schedule = IceCube::Schedule.new(options[:start_date])

    rule = case options[:interval_unit]
      when 'day'
        IceCube::Rule.daily options[:interval]
      when 'week'
        IceCube::Rule.weekly(options[:interval]).day( *IceCube::DAYS.keys.select{|day| options[day].to_i == 1 } )
    end

    rule.until(options[:until_date]) if options[:ends] == 'eventually'

    @schedule.add_recurrence_rule(rule)
  end

  self.schedule_yaml = @schedule.to_yaml
end