Module: UniverseCompiler::Universe::Entities

Included in:
Base
Defined in:
lib/universe_compiler/universe/entities.rb

Instance Method Summary collapse

Instance Method Details

#<<(entity) ⇒ Object



30
31
32
33
# File 'lib/universe_compiler/universe/entities.rb', line 30

def <<(entity)
  add entity
  self
end

#add(entity) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/universe_compiler/universe/entities.rb', line 10

def add(entity)
  raise UniverseCompiler::Error, 'An entity cannot be null !' if entity.nil?
  raise UniverseCompiler::Error, 'An entity must have a name !' unless entity.respond_to? :name
  raise UniverseCompiler::Error, 'An entity must have a name !' if entity.name.nil? or entity.name.empty?
  unless UniverseCompiler::Entity::TypeManagement.valid_for_type? entity
    entity.extend UniverseCompiler::Entity::TypeManagement
  end
  raise UniverseCompiler::Error, "An entity named '#{entity.name}' already exists with the type '#{entity.type}' !" if by_uniq_key.keys.include? [entity.type, entity.name]
  raise UniverseCompiler::Error, 'Invalid type specified' if entity.type.nil?
  raise UniverseCompiler::Error, 'Type cannot contain spaces' if entity.type =~ /\s/
  # Normally here the entity is valid
  # We add it to the universe and index it
  entities << entity
  if entity.respond_to? :'universe='
    entity.universe = self
  end
  index entity
  entity
end

#clearObject



109
110
111
112
# File 'lib/universe_compiler/universe/entities.rb', line 109

def clear
  entities.clear
  clear_indices
end

#delete(entity) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/universe_compiler/universe/entities.rb', line 35

def delete(entity)
  # Remove references to entity
  impacted_entities = {}
  entities.each do |e|
    e.class.fields_constraints.each do |field_name, constraints|
      if constraints.keys.include? :has_one
        if e[field_name] == entity
          e[field_name] = nil
          impacted_entities[e] ||= []
          impacted_entities[e] << field_name
        end
      end
      if constraints.keys.include? :has_many
        if e[field_name].include? entity
          e[field_name].delete entity
          impacted_entities[e] ||= []
          impacted_entities[e] << field_name unless impacted_entities[e].include? field_name
        end
      end
    end
  end
  # Then delete the entity
  entities.delete entity
  reindex_all entities
  unless impacted_entities.empty?
    UniverseCompiler.logger.warn "Impacts of '#{entity.as_path}' deletion:"
    impacted_entities.each do |k, v|
      UniverseCompiler.logger.warn " - '#{k.as_path}' fields : #{v.join ', '}"
    end
  end
  impacted_entities
end

#empty?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/universe_compiler/universe/entities.rb', line 6

def empty?
  entities.empty?
end

#replace(entity, by) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/universe_compiler/universe/entities.rb', line 68

def replace(entity, by)
  raise UniverseCompiler::Error, "Wrong type, cannot replace '#{entity.as_path}' by '#{by.as_path}' !" unless entity.type == by.type
  raise UniverseCompiler::Error, "Cannot replace '#{entity.as_path}' with '#{by.as_path}' which is alreadyin the universe!" if entity.universe == by.universe

  # Change references to entity
  impacted_entities = {}
  entities.each do |e|
    e.class.fields_constraints.each do |field_name, constraints|
      if constraints.keys.include? :has_one
        if e[field_name] == entity
          e[field_name] = by
          impacted_entities[e] ||= []
          impacted_entities[e] << field_name
        end
      end
      if constraints.keys.include? :has_many
        if e[field_name].map! do |entity_list_item|
          if (entity == entity_list_item)
            impacted_entities[e] ||= []
            impacted_entities[e] << field_name unless impacted_entities[e].include? field_name
            by
          else
            entity_list_item
          end
        end
        end
      end
    end
  end
  # Then replace the entity
  entities[entities.index(entity)] = by
  reindex_all entities
  unless impacted_entities.empty?
    UniverseCompiler.logger.warn "Impacts of '#{entity.as_path}' deletion:"
    impacted_entities.each do |k, v|
      UniverseCompiler.logger.warn " - '#{k.as_path}' fields : #{v.join ', '}"
    end
  end
  impacted_entities
end