Class: Clockwork::At

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

Defined Under Namespace

Classes: FailedToParse

Constant Summary collapse

NOT_SPECIFIED =
class << nil
  include Comparable
  def <=>(other); 0; end # equals to anything
end
WDAYS =
%w[sunday monday tuesday wednesday thursday friday saturday].map do |w|
  [w, w.capitalize, w[0...3], w[0...3].capitalize]
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.



37
38
39
40
41
42
43
44
45
46
# File 'lib/clockwork.rb', line 37

def initialize(min, hour=NOT_SPECIFIED, wday=NOT_SPECIFIED)
  if min.nil? || min < 0 || min > 59 ||
      hour < 0 || hour > 23 ||
      wday < 0 || wday > 6
    raise ArgumentError
  end
  @min = min
  @hour = hour
  @wday = wday
end

Instance Attribute Details

#hour=(value) ⇒ Object (writeonly)

Sets the attribute hour

Parameters:

  • value

    the value to set the attribute hour to.



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

def hour=(value)
  @hour = value
end

#min=(value) ⇒ Object (writeonly)

Sets the attribute min

Parameters:

  • value

    the value to set the attribute min to.



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

def min=(value)
  @min = value
end

#wday=(value) ⇒ Object (writeonly)

Sets the attribute wday

Parameters:

  • value

    the value to set the attribute wday to.



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

def wday=(value)
  @wday = value
end

Class Method Details

.parse(at) ⇒ Object



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

def self.parse(at)
  return unless at
  case at
  when /^([[:alpha:]]+)\s(.*)$/
    ret = parse($2)
    wday = WDAYS.find_index {|x| x.include?($1) }
    raise FailedToParse, at if wday.nil?
    ret.wday = wday
    ret
  when /^(\d{1,2}):(\d\d)$/
    new($2.to_i, $1.to_i)
  when /^\*{1,2}:(\d\d)$/
    new($1.to_i)
  else
    raise FailedToParse, at
  end
rescue ArgumentError
  raise FailedToParse, at
end

Instance Method Details

#ready?(t) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/clockwork.rb', line 48

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