Class: A4Tools::EventManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events = nil) ⇒ EventManager

Returns a new instance of EventManager.



5
6
7
8
9
10
11
# File 'lib/event_manager.rb', line 5

def initialize(events=nil)
  events = File.read(File.join(File.dirname(__FILE__), "events.json")) if events.nil?
  events = File.read(events) if events.is_a? String and File.exists? events
  events = JSON.parse(events) if events.is_a? String

  @events = transform_events(events)
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



3
4
5
# File 'lib/event_manager.rb', line 3

def events
  @events
end

Instance Method Details

#event(id) ⇒ Object



19
20
21
22
# File 'lib/event_manager.rb', line 19

def event(id)
  @events.each { |event| return event if event[:code] == id.to_i or event[:key] == id.to_sym }
  nil
end

#transform_events(events) ⇒ Object



13
14
15
16
17
# File 'lib/event_manager.rb', line 13

def transform_events(events)
  events.map do |event|
    { code:event["code"].to_i, key:event["key"].to_sym, description:event["description"] }
  end
end