class UniverseCompiler::Entity::Reference

Attributes

name[R]
type[R]
universe[R]

Public Class Methods

entity_to_cache_key(entity_or_reference) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 22
def entity_to_cache_key(entity_or_reference)
  universe_name = entity_or_reference.universe.nil? ? '' : entity_or_reference.universe.name
  universe_name ||= ''
  [entity_or_reference.type, entity_or_reference.name, universe_name]
end
new(entity = nil) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 40
def initialize(entity = nil)
  self.entity = entity unless entity.nil?
end
new_instance(entity = nil) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 14
def new_instance(entity = nil)
  k = entity_to_cache_key entity
  return references[k] if references[k]
  e = new entity
  references[k] = e
  e
end
references() click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 10
def references
  @references ||= {}
end

Public Instance Methods

encode_with(coder) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 64
def encode_with(coder)
  coder.scalar = '%s/%s' % [type, name]
end
entity=(entity) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 44
def entity=(entity)
  @type = entity.type
  @name = entity.name
  self.universe = entity.universe
end
init_with(coder) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 68
def init_with(coder)
  initialize
  case coder.tag
  when '!psref'
    @type = coder['type']
    @name = coder['name']
  when '!entity'
    if md = coder.scalar.match(/^(?<type>[^\/]+)\/(?<name>.+)$/)
      @type = md['type'].to_sym
      @name = md['name']
    else
      raise UniverseCompiler::Error 'Invalid deserialization !'
    end
  else
    raise UniverseCompiler::Error 'Invalid deserialization !'
  end

end
to_entity(raise_error: true) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 57
def to_entity(raise_error: true)
  false_or_raise "Cannot get entity '#{to_composite_key.inspect}' if its universe is not defined!", raise_error: raise_error if universe.nil?
  entity = universe.get_entity *to_composite_key
  false_or_raise "Cannot find entity '#{to_composite_key.inspect}' in the universe '#{universe}'", raise_error: raise_error if entity.nil?
  entity
end
universe=(another_universe) click to toggle source
# File lib/universe_compiler/entity/reference.rb, line 50
def universe=(another_universe)
  k = self.class.entity_to_cache_key self
  self.class.references.delete k
  @universe = another_universe
  self.class.entity_to_cache_key self
end