Class: Crntb::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/crntb/field.rb

Direct Known Subclasses

Command, DayOfMonth, DayOfWeek, Hour, Minute, Month

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ Field

Returns a new instance of Field.



11
12
13
# File 'lib/crntb/field.rb', line 11

def initialize(field)
  @field = field
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



3
4
5
# File 'lib/crntb/field.rb', line 3

def field
  @field
end

Class Method Details

.parse(field) ⇒ Object



6
7
8
# File 'lib/crntb/field.rb', line 6

def parse(field)
  self.new(field).parse
end

Instance Method Details

#every_time?Boolean

Returns:

  • (Boolean)


100
101
# File 'lib/crntb/field.rb', line 100

def every_time?
end

#expand_range(collections) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/crntb/field.rb', line 79

def expand_range(collections)
  result = []
  collections.each do |collection|
    if collection.include?('-')
      min, max = collection.split('-', 2)
      for i in min..max do
        result << i
      end
    else
      result << collection
    end
  end
end

#first_to_last?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/crntb/field.rb', line 93

def first_to_last?
  field == '*'
end

#get_collectionObject



60
61
62
# File 'lib/crntb/field.rb', line 60

def get_collection

end

#get_range(value) ⇒ Object

e.g

"*/1"     => ["*", "1"]
"*/1,2-4" => ["*", "1,2-4"]


67
68
69
70
71
72
73
# File 'lib/crntb/field.rb', line 67

def get_range(value)
  if value == '*'
    [field_range.first, field_range.last]
  else
    value.split('-')
  end
end

#get_step(value) ⇒ Object



75
76
77
# File 'lib/crntb/field.rb', line 75

def get_step(value)
  value.split('/')
end

#interpretationObject

先頭文字列を処理

*/...
n...
それ以降はカンマ区切りでsplitして-はloop処理しての繰り返し


25
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
# File 'lib/crntb/field.rb', line 25

def interpretation
  step_collections = field.split(',')
  collections = []
  step_collections.each do |step_collection|
    # /でsplitした結果
    step = get_step(step_collection)

    # /が使われてればstep[1]には分割数が来るはず, なければ1
    # 次のrange処理で使う
    if step[1] && step[1].match(/^[0-9]+$/)
      step_size = step[1].to_i
    else
      step_size = 1
    end

    # step[0] : *   => min..max
    # step[0] : n-m => n..m
    range = get_range(step[0])
    if range.length > 1
      s = range[0].to_i
      e = range[1].to_i
      s.step(e, step_size) { |r| collections << r }
    end
  end
  collections.uniq.sort
  # 曜日や月は文字列にして追加
  # そうでなければ数字を追加
  result = collections.inject '' do |res, collection|
    res += collection.to_s
    res += ", "
  end
  result.slice!(result.size - 2, 2)
  result
end

#parseObject



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

def parse
  #return nil unless valid?
  return '*' if first_to_last?
  interpretation
end

#range_time?Boolean

Returns:

  • (Boolean)


97
98
# File 'lib/crntb/field.rb', line 97

def range_time?
end