class Lwes::Event

Represent a LWES event, can read from and write to IO objects

Attributes

attributes[RW]
name[RW]

Public Class Methods

new(hash={}) click to toggle source

Creates a new {Lwes::Emitter}. @param [Hash] hash @option hash [String] :name The event name @option hash [Hash] :attributes Attributes of the event.

The hash key is the attribute name, the hash value should be an Array of two elements.
The first element is the type see {Lwes::TYPE_TO_BYTE} for all available types.
The second element is the actual value of the attribute.

@option hash [Number] :address_family, Address family of the socket, defaults to Socket::AF_INET.

# File lib/lwes/event.rb, line 19
def initialize(hash={})
  self.name = hash[:name] || ""
  self.attributes = hash[:attributes] || {}
end
read(io) click to toggle source

Read from an IO object @param [IO] io The IO object to read from. @return [Lwes::Event] a new {Lwes::Event} instance with data filled from the IO.

# File lib/lwes/event.rb, line 41
def self.read(io)
  event = new
  event.read(io)
  event
end

Public Instance Methods

read(io) click to toggle source

Read from an IO object @param [IO] io The IO object to read from.

# File lib/lwes/event.rb, line 33
def read(io)
  serializer = Lwes::Serialization::Event.read(io)
  set_attributes_from_serializer(serializer)
end
to_hash() click to toggle source

Converts the event to a hash representation @return [Hash] the hash representation of the instance.

# File lib/lwes/event.rb, line 49
def to_hash
  {
    :name => name,
    :attributes => attributes
  }
end
write(io) click to toggle source

Writes the event to an IO object @param [IO] io The IO object to write to. @return [Number] Number of bytes written to IO

# File lib/lwes/event.rb, line 27
def write(io)
  serializer.write(io)
end

Private Instance Methods

attributes_to_serializer() click to toggle source
# File lib/lwes/event.rb, line 61
def attributes_to_serializer
  attributes.collect do |key, value|
    [ key, value[0], value[1] ]
  end
end
serializer() click to toggle source
# File lib/lwes/event.rb, line 57
def serializer
  Lwes::Serialization::Event.new(:name => name, :attributes => attributes_to_serializer)
end
set_attributes_from_serializer(serializer) click to toggle source
# File lib/lwes/event.rb, line 67
def set_attributes_from_serializer(serializer)
  @name = serializer.name
  @attributes = serializer.attributes.inject({}) do |hash, attribute|
    key, vtype, value = *attribute
    # convert to real string
    key = key.to_s
    # convert BinData types to actual types
    value = value.value

    hash[key] = [vtype, value]
    hash
  end
end