class Chrono::Fields::Base

Attributes

source[R]

Public Class Methods

new(source) click to toggle source
# File lib/chrono/fields/base.rb, line 15
def initialize(source)
  @source = source
end

Public Instance Methods

to_a() click to toggle source
# File lib/chrono/fields/base.rb, line 19
def to_a
  if has_multiple_elements?
    fields.map(&:to_a).flatten.uniq.sort
  else
    validate!
    lower.step(upper, step).to_a.sort
  end
end

Private Instance Methods

elements() click to toggle source
# File lib/chrono/fields/base.rb, line 78
def elements
  @elements ||= source.split(",")
end
fields() click to toggle source
# File lib/chrono/fields/base.rb, line 86
def fields
  elements.map {|element| self.class.new(element) }
end
has_multiple_elements?() click to toggle source
# File lib/chrono/fields/base.rb, line 82
def has_multiple_elements?
  elements.size >= 2
end
interpolated() click to toggle source
# File lib/chrono/fields/base.rb, line 30
def interpolated
  source.gsub("*", "#{range.first}-#{range.last}")
end
lower() click to toggle source
# File lib/chrono/fields/base.rb, line 50
def lower
  @lower ||= match_data[1].to_i
end
match_data() click to toggle source
# File lib/chrono/fields/base.rb, line 74
def match_data
  @match_data ||= interpolated.match(pattern)
end
pattern() click to toggle source
# File lib/chrono/fields/base.rb, line 70
def pattern
  %r<\A(\d+)(?:-(\d+))?(?:/(\d+))?\z>
end
step() click to toggle source
# File lib/chrono/fields/base.rb, line 62
def step
  if match_data[3]
    match_data[3].to_i
  else
    1
  end
end
upper() click to toggle source
# File lib/chrono/fields/base.rb, line 54
def upper
  if match_data[2]
    match_data[2].to_i
  else
    lower
  end
end
validate!() click to toggle source
# File lib/chrono/fields/base.rb, line 34
def validate!
  unless match_data
    raise InvalidField.new('Unparsable field', source)
  end

  if lower < range.begin || range.end < upper
    raise InvalidField.new('The field is out-of-range', source)
  end

  if upper < lower
    raise InvalidField.new('The range is evaluated to empty', source)
  end

  true
end