Module: Peregrine::Handlers::EntityPackage

Defined in:
lib/peregrine/handlers/entity_package.rb

Overview

This module provides methods which allow Entity objects to be wrapped and unwrapped for use by an instance of the Package class.

Class Method Summary collapse

Class Method Details

.handles?(object) ⇒ Boolean

Returns true if this package handler can wrap and unwrap the given object, false otherwise.

Returns:

  • (Boolean)


8
9
10
11
12
13
14
# File 'lib/peregrine/handlers/entity_package.rb', line 8

def self.handles?(object)
  if object.class == Class
    object.ancestors.include?(Peregrine::Entity)
  else
    object.kind_of?(Peregrine::Entity)
  end
end

.unwrap(hash) ⇒ Object

Unwraps a generic hash into a newly instanced Entity.



26
27
28
29
30
31
32
# File 'lib/peregrine/handlers/entity_package.rb', line 26

def self.unwrap(hash)
  return nil unless handles?(hash[:type])
  hash[:type].new(*hash[:data]) do |entity|
    entity.name = hash[:name]
    entity.add_tags(*hash[:tags])
  end
end

.wrap(entity) ⇒ Object

Wraps the given Entity into a generic hash.



17
18
19
20
21
22
23
# File 'lib/peregrine/handlers/entity_package.rb', line 17

def self.wrap(entity)
  return nil unless handles?(entity)
  { :data => entity.components,
    :name => entity.name,
    :tags => entity.tags,
    :type => entity.class }
end