class ElapsedWatch::Event
Description¶ ↑
Defines an object class that deals with individual events.
Events have a name and a time.
Public Class Methods
new(event_string)
click to toggle source
Instantiates a new Event
object.
The event_string consists of a tiny piece of YAML that defines the event. The format is:
Event
Name: YYYY-MM-DD HH:MM (anything parsable by Chronic.parse)
# File lib/elapsed_watch/event.rb, line 44 def initialize(event_string) raise "event_string is empty" if event_string.nil? or event_string.empty? ev_yaml = YAML.load(ERB.new(event_string).result) @event_name = ev_yaml.keys.first @event_time = ev_yaml.values.first raise "No event time spec: #{event_string}" if @event_time.nil? case @event_time when String raise "No event time spec: #{event_string}" if @event_time.empty? @event_time = Chronic.parse(@event_time) when Date @event_time = @event_time.to_time when DateTime @event_time = @event_time.to_time when Integer @event_time = Time.new @event_time when nil end end
parse(event_string)
click to toggle source
Parse an event sting and return an event object
# File lib/elapsed_watch/event.rb, line 95 def self.parse(event_string) self.new(event_string) end
Public Instance Methods
duration_string()
click to toggle source
Returns a nicely formatted string of the duration between the event and now
# File lib/elapsed_watch/event.rb, line 74 def duration_string() ChronicDuration.output(time_diff()) end
format()
click to toggle source
Returns the format string to use based on whether the event is in the past or the future.
# File lib/elapsed_watch/event.rb, line 86 def format() if now() >= @event_time PAST_EVENT_FORMAT else FUTURE_EVENT_FORMAT end end
time_diff()
click to toggle source
Calculates the absolute value of the difference between the event and now
# File lib/elapsed_watch/event.rb, line 80 def time_diff() (now() - @event_time).to_i.abs end
to_s()
click to toggle source
Returns a formatted string for the event
# File lib/elapsed_watch/event.rb, line 66 def to_s() format() % [@event_name, duration_string()] end
Also aliased as: duration
Private Instance Methods
now(use_time=nil)
click to toggle source
Memoizes now
# File lib/elapsed_watch/event.rb, line 102 def now(use_time=nil) @now ||= use_time ||= Time.now end