Class: Clockwork::At

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork/at.rb

Defined Under Namespace

Classes: FailedToParse

Constant Summary collapse

NOT_SPECIFIED =
nil
WDAYS =
%w[sunday monday tuesday wednesday thursday friday saturday].each.with_object({}).with_index do |(w, wdays), index|
  [w, w.capitalize, w[0...3], w[0...3].capitalize].each do |k|
    wdays[k] = index
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min, hour = NOT_SPECIFIED, wday = NOT_SPECIFIED) ⇒ At

Returns a new instance of At

Raises:

  • (ArgumentError)

38
39
40
41
42
43
# File 'lib/clockwork/at.rb', line 38

def initialize(min, hour=NOT_SPECIFIED, wday=NOT_SPECIFIED)
  @min = min
  @hour = hour
  @wday = wday
  raise ArgumentError unless valid?
end

Instance Attribute Details

#hourObject

Returns the value of attribute hour


36
37
38
# File 'lib/clockwork/at.rb', line 36

def hour
  @hour
end

#minObject

Returns the value of attribute min


36
37
38
# File 'lib/clockwork/at.rb', line 36

def min
  @min
end

#wdayObject

Returns the value of attribute wday


36
37
38
# File 'lib/clockwork/at.rb', line 36

def wday
  @wday
end

Class Method Details

.parse(at) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/clockwork/at.rb', line 12

def self.parse(at)
  return unless at
  case at
  when /\A([[:alpha:]]+)\s(.*)\z/
    if wday = WDAYS[$1]
      parsed_time = parse($2)
      parsed_time.wday = wday
      parsed_time
    else
      raise FailedToParse, at
    end
  when /\A(\d{1,2}):(\d\d)\z/
    new($2.to_i, $1.to_i)
  when /\A\*{1,2}:(\d\d)\z/
    new($1.to_i)
  when /\A(\d{1,2}):\*\*\z/
    new(NOT_SPECIFIED, $1.to_i)
  else
    raise FailedToParse, at
  end
rescue ArgumentError
  raise FailedToParse, at
end

Instance Method Details

#==(other) ⇒ Object


51
52
53
# File 'lib/clockwork/at.rb', line 51

def == other
  @min == other.min && @hour == other.hour && @wday == other.wday
end

#ready?(t) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
48
49
# File 'lib/clockwork/at.rb', line 45

def ready?(t)
  (@min == NOT_SPECIFIED or t.min == @min) and
    (@hour == NOT_SPECIFIED or t.hour == @hour) and
    (@wday == NOT_SPECIFIED or t.wday == @wday)
end