Method: Timely::WeekDays#initialize

Defined in:
lib/timely/week_days.rb

#initialize(weekdays) ⇒ WeekDays

Create a new Weekdays object weekdays can be in three formats integer representing binary string

e.g. 1 = Sun, 2 = Mon, 3 = Sun + Mon, etc.

hash with symbol keys for :sun, :mon, … with true/false values

e.g. {:sun => true, :tue => true} is Sunday and Tuesday
Not passing in values is the same as setting them explicitly to true

array with true/false values from sun to sat

e.g. [1, 0, 1, 0, 0, 0, 0] is Sunday and Tuesday


26
27
28
29
30
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
# File 'lib/timely/week_days.rb', line 26

def initialize(weekdays)
  @weekdays = {
    sun: false,
    mon: false,
    tue: false,
    wed: false,
    thu: false,
    fri: false,
    sat: false
  }

  case weekdays
  when Integer
    # 4 -> 0000100 (binary) -> "0010000" (reversed string) -> {:tue => true}
    weekdays.to_s(2).reverse.each_char.with_index do |char, index|
      set_day(index, char == '1')
    end
  when Hash
    weekdays.each_pair do |day, value|
      set_day(day, value)
    end
  when Array
    weekdays.each.with_index do |value, index|
      set_day(index, value)
    end
  when NilClass
    @weekdays = {
      sun: true,
      mon: true,
      tue: true,
      wed: true,
      thu: true,
      fri: true,
      sat: true
    }
  else
    raise ArgumentError,
          'You must initialize with an Integer, Hash or Array'
  end
end