lib/zhong/at.rb in zhong-0.1.5 vs lib/zhong/at.rb in zhong-0.1.6
- old
+ new
@@ -22,11 +22,11 @@
MultiAt.new(at.map { |a| parse_at(a, grace) })
else
parse_at(at, grace)
end
rescue ArgumentError
- fail FailedToParse, at
+ raise FailedToParse, at
end
def self.deserialize(at)
parse_serialized(MessagePack.unpack(at))
end
@@ -35,36 +35,33 @@
@minute = minute
@hour = hour
@wday = wday
@grace = grace
- fail ArgumentError unless valid?
+ raise ArgumentError unless valid?
end
def next_at(time = Time.now)
at_time = at_time_day_hour_minute_adjusted(time)
grace_cutoff = time.change(sec: 0) - @grace
if at_time < grace_cutoff
- if @wday.nil?
- at_time += @hour.nil? ? 1.hour : 1.day
- else
- at_time += 1.week
- end
+ at_time + if @wday.nil?
+ @hour.nil? ? 1.hour : 1.day
+ else
+ 1.week
+ end
else
at_time
end
end
def to_s
str = "#{formatted_time(@hour)}:#{formatted_time(@minute)}"
+ str += " on #{WDAYS.invert[@wday].capitalize}" if @wday
- if @wday
- str += " on #{WDAYS.invert[@wday].capitalize}"
- end
-
str
end
def as_json
{m: @minute, h: @hour, w: @wday, g: @grace}
@@ -72,61 +69,63 @@
def serialize
MessagePack.pack(as_json)
end
- protected
-
- def formatted_time(t)
- if t.nil?
- "**"
+ def self.parse_serialized(at)
+ if at.is_a?(Array)
+ MultiAt.new(at.map { |a| parse_serialized(a) })
else
- t.to_s.rjust(2, "0")
+ new(minute: at["m"], hour: at["h"], wday: at["w"], grace: at["g"])
end
end
+ private_class_method :parse_serialized
- def ==(o)
- o.class == self.class && o.state == state
- end
-
- def state
- [@minute, @hour, @wday]
- end
-
- private
-
def self.parse_at(at, grace)
case at
when /\A([[:alpha:]]+)\s+(.*)\z/
wday = WDAYS[$1.downcase]
- if wday
- parsed_time = parse_at($2, grace)
- parsed_time.wday = wday
- parsed_time
- else
- fail FailedToParse, at
- end
+ raise FailedToParse, at unless wday
+
+ parsed_time = parse_at($2, grace)
+ parsed_time.wday = wday
+ parsed_time
when /\A(\d{1,2}):(\d\d)\z/
new(minute: $2.to_i, hour: $1.to_i, grace: grace)
when /\A\*{1,2}:(\d\d)\z/
new(minute: $1.to_i, grace: grace)
when /\A(\d{1,2}):\*{1,2}\z/
new(hour: $1.to_i, grace: grace)
+ when /\A\*{1,2}:\*{1,2}\z/
+ new(grace: grace)
else
- fail FailedToParse, at
+ raise FailedToParse, at
end
end
+ private_class_method :parse_at
- def self.parse_serialized(at)
- if at.is_a?(Array)
- MultiAt.new(at.map { |a| parse_serialized(a) })
+ protected
+
+ def formatted_time(t)
+ if t.nil?
+ "**"
else
- new(minute: at["m"], hour: at["h"], wday: at["w"], grace: at["g"])
+ t.to_s.rjust(2, "0")
end
end
+ def ==(other)
+ other.class == self.class && other.state == state
+ end
+
+ def state
+ [@minute, @hour, @wday]
+ end
+
+ private
+
def at_time_hour_minute_adjusted(time)
if @minute && @hour
time.change(hour: @hour, min: @minute)
elsif @minute
time.change(min: @minute)
@@ -153,11 +152,11 @@
def initialize(ats = [])
@ats = ats
end
- def ==(o)
- o.class == self.class && @ats == o.ats
+ def ==(other)
+ other.class == self.class && @ats == other.ats
end
def next_at(time = Time.now)
ats.map { |at| at.next_at(time) }.min
end