module Peregrine::Handlers::EntityPackage
This module provides methods which allow Entity
objects to be wrapped and unwrapped for use by an instance of the Package
class.
Public Class Methods
handles?(object)
click to toggle source
Returns true
if this package handler can wrap
and unwrap
the given object, false
otherwise.
# 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)
click to toggle source
Unwraps a generic hash into a newly instanced Entity
.
# 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)
click to toggle source
Wraps the given Entity
into a generic hash.
# 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