Class: FileScheduler::TimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/file_scheduler/time_parser.rb

Constant Summary collapse

@@time_mark_fields =
[["y", 4], ["M", 2], ["d", 2], ["w", 1], ["h", 2], ["m", 2]]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.time_mark_formatObject



7
8
9
10
11
# File 'lib/file_scheduler/time_parser.rb', line 7

def self.time_mark_format
  @@time_mark_fields.collect do |field|
    "(\\d{#{field[1]}}#{field[0]})?"
  end.join
end

Instance Method Details

#parse(string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/file_scheduler/time_parser.rb', line 13

def parse(string)
  parsed_fields = string.scan(/^T?#{self.class.time_mark_format}(-#{self.class.time_mark_format})?/).first.map do |field|
    field.to_i if field
  end

  from = time_mark(parsed_fields.first(6))
  to = time_mark(parsed_fields.last(6))

  if to
    TimeInterval.new from, to
  else
    from
  end
end

#time_mark(parsed_fields) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/file_scheduler/time_parser.rb', line 28

def time_mark(parsed_fields)
  unless parsed_fields.all?(&:nil?)
    attributes = Hash[ [[:year, :month, :day, :week_day, :hour, :minute], parsed_fields].transpose ]
    attributes.delete_if { |k,v| v.nil? }
    TimeMark.new attributes
  end
end