Class: ElapsedWatch::Event
- Inherits:
-
Object
- Object
- ElapsedWatch::Event
- Includes:
- Methadone::CLILogging
- Defined in:
- lib/elapsed_watch/event.rb
Overview
Description
Defines an object class that deals with individual events.
Events have a name and a time.
Class Method Summary collapse
-
.parse(event_string) ⇒ Object
Parse an event sting and return an event object.
Instance Method Summary collapse
-
#duration_string ⇒ Object
Returns a nicely formatted string of the duration between the event and now.
-
#format ⇒ Object
Returns the format string to use based on whether the event is in the past or the future.
-
#initialize(event_string) ⇒ Event
constructor
Instantiates a new Event object.
-
#time_diff ⇒ Object
Calculates the absolute value of the difference between the event and now.
-
#to_s ⇒ Object
(also: #duration)
Returns a formatted string for the event.
Constructor Details
#initialize(event_string) ⇒ Event
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)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 |
Class Method Details
.parse(event_string) ⇒ Object
Parse an event sting and return an event object
95 96 97 |
# File 'lib/elapsed_watch/event.rb', line 95 def self.parse(event_string) self.new(event_string) end |
Instance Method Details
#duration_string ⇒ Object
Returns a nicely formatted string of the duration between the event and now
74 75 76 |
# File 'lib/elapsed_watch/event.rb', line 74 def duration_string() ChronicDuration.output(time_diff()) end |
#format ⇒ Object
Returns the format string to use based on whether the event is in the past or the future.
86 87 88 89 90 91 92 |
# 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 ⇒ Object
Calculates the absolute value of the difference between the event and now
80 81 82 |
# File 'lib/elapsed_watch/event.rb', line 80 def time_diff() (now() - @event_time).to_i.abs end |
#to_s ⇒ Object Also known as: duration
Returns a formatted string for the event
66 67 68 |
# File 'lib/elapsed_watch/event.rb', line 66 def to_s() format() % [@event_name, duration_string()] end |