Module: Crntb::Outputer::Text

Defined in:
lib/crntb/outputer/text.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/crntb/outputer/text.rb', line 5

def fields
  @fields
end

.resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/crntb/outputer/text.rb', line 5

def result
  @result
end

Class Method Details

.build(fields) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/crntb/outputer/text.rb', line 7

def build(fields)
  @result = ''
  @fields = fields
  day_week_month_result
  min_hour_result
  %Q{#{@result}\n  run command "#{fields.command}"}
end

.build_day_of_monthObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crntb/outputer/text.rb', line 26

def build_day_of_month
  if fields.day_of_month == '*'
    @result += 'every day ' if fields.day_of_week == '*'
  else
    @result += 'the '
    days = fields.day_of_month.split(',')
    days.each do |day|
      case day.to_i
      when 1
        @result += "#{day}st,"
      when 2
        @result += "#{day}nd,"
      when 3
        @result += "#{day}rd,"
      else
        @result += "#{day}th,"
      end
    end
    @result.slice!(@result.size - 1, 1)
    @result += ' '
  end
end

.build_day_of_weekObject



49
50
51
52
53
54
# File 'lib/crntb/outputer/text.rb', line 49

def build_day_of_week
  if fields.day_of_week != '*'
    @result += 'and on ' if fields.day_of_month != '*'
    @result += fields.day_of_week + ' '
  end
end

.build_monthObject



21
22
23
24
# File 'lib/crntb/outputer/text.rb', line 21

def build_month
  @result += "in #{fields.month}, on " if fields.month != '*'
  @result += "every month on " if fields.month == '*' and fields.day_of_month != '*'
end

.day_week_month_resultObject



15
16
17
18
19
# File 'lib/crntb/outputer/text.rb', line 15

def day_week_month_result
  build_month
  build_day_of_month
  build_day_of_week
end

.min_hour_resultObject



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
94
95
96
# File 'lib/crntb/outputer/text.rb', line 56

def min_hour_result
  hour_collections = fields.hour.split(',')
  min_collections = fields.minute.split(',')
  if hour_collections.length > 1 or hour_collections[0].to_i.to_s == hour_collections[0]
    # input exp. ["1,2"]
    if min_collections.length > 1 or min_collections[0].to_i.to_s == min_collections[0]
      @result += 'at '
      hour_collections.each do |hour_collection|
        min_collections.each do |min_collection|
          @result += "%#02d" % hour_collection + ':' + "%#02d" % min_collections + ', '
        end
      end
      @result.slice!(@result.size - 2, 2)
    else
      @result += "on #{fields.minute} when hour is ("
      hour_collections.each do |hour_collection|
        @result += "%#02d" % hour_collection + ', '
      end
      @result.slice!(@result.size - 2, 2)
      @result += ')'
    end
  else
    # input exp. ["every hour"]
    if min_collections.length > 1
      @result += "on #{fields.hour} when minute equals one of ("
      min_collections.each do |min_collection|
        @result += "%#02d" % min_collection + ', '
      end
      @result.slice!(@result.size - 2, 2)
      @result += ')'
    else
      if fields.hour.to_i.to_s == fields.hour and fields.minute.to_i.to_s == fields.minute
        @result += "on #{fields.hour}:#{fields.minute}"
      elsif fields.minute.to_i.to_s == fields.minute
        @result += "on #{fields.hour} when minute equals " + "%#02d" % fields.minute
      else
        @result += "on #{fields.hour} on #{fields.minute}"
      end
    end
  end
end