Module: UniverseCompiler::Entity::AutoNamed

Included in:
Base
Defined in:
lib/universe_compiler/entity/auto_named.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auto_named_entity_type_seedObject (readonly)

Returns the value of attribute auto_named_entity_type_seed.



7
8
9
# File 'lib/universe_compiler/entity/auto_named.rb', line 7

def auto_named_entity_type_seed
  @auto_named_entity_type_seed
end

Instance Method Details

#auto_named_entity_type(seed = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/universe_compiler/entity/auto_named.rb', line 9

def auto_named_entity_type(seed = nil)
  @auto_named_entity_type = true
  @auto_named_entity_type_seed = if seed.nil?
                                   entity_type.to_s
                                 else
                                   seed
                                 end
  @entity_type_counter = 0
end

#auto_named_entity_type?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/universe_compiler/entity/auto_named.rb', line 19

def auto_named_entity_type?
  @auto_named_entity_type
end

#get_unique_name(universe) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/universe_compiler/entity/auto_named.rb', line 23

def get_unique_name(universe)
  return SecureRandom.uuid if universe.nil?

  uniq_name = nil
  loop do
    @entity_type_counter += 1
    raise UniverseCompiler::Error, "Too many '#{entity_type}' (> 999999) in universe '#{universe.name}'" if @entity_type_counter >= 1000000
    uniq_name = '%s_%06u' % [auto_named_entity_type_seed, @entity_type_counter]
    break if universe.get_entity(entity_type, uniq_name).nil?
  end
  uniq_name
end