Class: ElapsedWatch::Event

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

Overview

event.rb – Class for holding events

  • Time-stamp: <2013-09-13 01:42:22 tamara>

  • Copyright © 2013 Tamara Temple Web Development

  • Author: Tamara Temple <[email protected]>

  • License: MIT

Description

Defines an object class that deals with individual events.

Events have a name and a time.

Class Method Summary collapse

Instance Method Summary collapse

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 Time.parse)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/elapsed_watch/event.rb', line 39

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 = Time.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



90
91
92
# File 'lib/elapsed_watch/event.rb', line 90

def self.parse(event_string)
  self.new(event_string)
end

Instance Method Details

#duration_stringObject

Returns a nicely formatted string of the duration between the event and now



69
70
71
# File 'lib/elapsed_watch/event.rb', line 69

def duration_string()
  ChronicDuration.output(time_diff())
end

#formatObject

Returns the format string to use based on whether the event is in the past or the future.



81
82
83
84
85
86
87
# File 'lib/elapsed_watch/event.rb', line 81

def format()
  if now() >= @event_time
    PAST_EVENT_FORMAT
  else
    FUTURE_EVENT_FORMAT
  end
end

#time_diffObject

Calculates the absolute value of the difference between the event and now



75
76
77
# File 'lib/elapsed_watch/event.rb', line 75

def time_diff()
  (now() - @event_time).to_i.abs
end

#to_sObject Also known as: duration

Returns a formatted string for the event



61
62
63
# File 'lib/elapsed_watch/event.rb', line 61

def to_s()
  format() % [@event_name, duration_string()]
end