Class: Kindergarten::Event

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, purpose, payload) ⇒ Event

Returns a new instance of Event.



20
21
22
23
24
# File 'lib/kindergarten/event.rb', line 20

def initialize(name, purpose, payload)
  @name    = name    || raise("An event must have a name")
  @purpose = purpose || raise("An event must have a purpose")
  @payload = payload
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/kindergarten/event.rb', line 5

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/kindergarten/event.rb', line 5

def payload
  @payload
end

#purposeObject (readonly)

Returns the value of attribute purpose.



5
6
7
# File 'lib/kindergarten/event.rb', line 5

def purpose
  @purpose
end

Class Method Details

.load(hash) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kindergarten/event.rb', line 7

def self.load(hash)
  if hash.is_a?(String)
    hash = begin
      Rufus::Json.decode(hash)
    rescue => ex
      raise ArgumentError.new("The provided string could not be decoded as JSON")
    end
  end

  hash.symbolize_keys!
  self.new(hash[:name], hash[:purpose], hash[:payload])
end

Instance Method Details

#to_jsonObject



26
27
28
29
30
# File 'lib/kindergarten/event.rb', line 26

def to_json
  Rufus::Json.encode(
    :name => @name, :purpose => @purpose, :payload => @payload
  )
end