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.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#universeObject

Returns the value of attribute universe.



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

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



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

def encode_with(coder)
  coder['type'] = type
  coder['name'] = name
end

#entity=(entity) ⇒ Object



43
44
45
46
47
# File 'lib/universe_compiler/entity/reference.rb', line 43

def entity=(entity)
  @type = entity.type
  @name = entity.name
  self.universe = entity.universe
end

#init_with(coder) ⇒ Object



68
69
70
71
72
# File 'lib/universe_compiler/entity/reference.rb', line 68

def init_with(coder)
  initialize
  @type = coder['type']
  @name = coder['name']
end

#to_entity(raise_error: true) ⇒ Object



56
57
58
59
60
61
# File 'lib/universe_compiler/entity/reference.rb', line 56

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