Class: Zhong::At
- Inherits:
-
Object
- Object
- Zhong::At
- Defined in:
- lib/zhong/at.rb
Overview
Strongly inspired by the Clockwork At class
Defined Under Namespace
Classes: FailedToParse
Constant Summary collapse
- WDAYS =
%w(sunday monday tuesday wednesday thursday friday saturday).each.with_object({}).with_index do |(w, wdays), index| [w, w[0...3]].each do |k| wdays[k] = index if k == "tue" wdays["tues"] = index elsif k == "thu" wdays["thr"] = index end end end.freeze
Instance Attribute Summary collapse
-
#hour ⇒ Object
Returns the value of attribute hour.
-
#minute ⇒ Object
Returns the value of attribute minute.
-
#wday ⇒ Object
Returns the value of attribute wday.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_json ⇒ Object
-
#initialize(minute: nil, hour: nil, wday: nil, grace: 0.seconds) ⇒ At
constructor
A new instance of At.
- #next_at(time = Time.now) ⇒ Object
- #serialize ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(minute: nil, hour: nil, wday: nil, grace: 0.seconds) ⇒ At
Returns a new instance of At.
34 35 36 37 38 39 40 41 |
# File 'lib/zhong/at.rb', line 34 def initialize(minute: nil, hour: nil, wday: nil, grace: 0.seconds) @minute = minute @hour = hour @wday = wday @grace = grace raise ArgumentError unless valid? end |
Instance Attribute Details
#hour ⇒ Object
Returns the value of attribute hour.
18 19 20 |
# File 'lib/zhong/at.rb', line 18 def hour @hour end |
#minute ⇒ Object
Returns the value of attribute minute.
18 19 20 |
# File 'lib/zhong/at.rb', line 18 def minute @minute end |
#wday ⇒ Object
Returns the value of attribute wday.
18 19 20 |
# File 'lib/zhong/at.rb', line 18 def wday @wday end |
Class Method Details
.deserialize(at) ⇒ Object
30 31 32 |
# File 'lib/zhong/at.rb', line 30 def self.deserialize(at) parse_serialized(MessagePack.unpack(at)) end |
.parse(at, grace: 0.seconds) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/zhong/at.rb', line 20 def self.parse(at, grace: 0.seconds) if at.respond_to?(:each) MultiAt.new(at.map { |a| parse_at(a, grace) }) else parse_at(at, grace) end rescue ArgumentError raise FailedToParse, at end |
Instance Method Details
#==(other) ⇒ Object
74 75 76 |
# File 'lib/zhong/at.rb', line 74 def ==(other) other.class == self.class && other.state == state end |
#as_json ⇒ Object
66 67 68 |
# File 'lib/zhong/at.rb', line 66 def as_json {m: @minute, h: @hour, w: @wday, g: @grace} end |
#next_at(time = Time.now) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/zhong/at.rb', line 43 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 at_time + if @wday.nil? @hour.nil? ? 1.hour : 1.day else 1.week end else at_time end end |
#serialize ⇒ Object
70 71 72 |
# File 'lib/zhong/at.rb', line 70 def serialize MessagePack.pack(as_json) end |
#to_s ⇒ Object
59 60 61 62 63 64 |
# File 'lib/zhong/at.rb', line 59 def to_s str = "#{formatted_time(@hour)}:#{formatted_time(@minute)}" str += " on #{WDAYS.invert[@wday].capitalize}" if @wday str end |