Class: UniverseCompiler::Entity::Reference

Inherits:
Object
  • Object
show all
Includes:
Conversion, Utils::ErrorPropagation
Defined in:
lib/universe_compiler/entity/reference.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Conversion

#==, #as_path, #eql?, #inspect, #to_composite_key, #to_hash, #to_reference, #to_uniq_id

Constructor Details

#initialize(entity = nil) ⇒ Reference

Returns a new instance of Reference.



40
41
42
# File 'lib/universe_compiler/entity/reference.rb', line 40

def initialize(entity = nil)
  self.entity = entity unless entity.nil?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



38
39
40
# File 'lib/universe_compiler/entity/reference.rb', line 38

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/universe_compiler/entity/reference.rb', line 38

def type
  @type
end

#universeObject

Returns the value of attribute universe.



38
39
40
# File 'lib/universe_compiler/entity/reference.rb', line 38

def universe
  @universe
end

Class Method Details

.entity_to_cache_key(entity_or_reference) ⇒ Object



22
23
24
25
26
# 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_instance(entity = nil) ⇒ Object



14
15
16
17
18
19
20
# 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

.referencesObject



10
11
12
# File 'lib/universe_compiler/entity/reference.rb', line 10

def references
  @references ||= {}
end

Instance Method Details

#encode_with(coder) ⇒ Object



64
65
66
# File 'lib/universe_compiler/entity/reference.rb', line 64

def encode_with(coder)
  coder.scalar = '%s/%s' % [type, name]
end

#entity=(entity) ⇒ Object



44
45
46
47
48
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



57
58
59
60
61
62
# 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