Class: PuppetX::Puppetlabs::Migration::OverviewModel::Overview

Inherits:
Object
  • Object
show all
Includes:
ModelObject
Defined in:
lib/puppet_x/puppetlabs/migration/overview_model.rb

Overview

Represents an immutable snapshot of a collection of entities. Serves as the “database” when doing queries

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities) ⇒ Overview

Returns a new instance of Overview.

Parameters:

  • entities (Hash<Integer,Entity] the entities contained in this overview)

    ntities [Hash<Integer,Entity] the entities contained in this overview



116
117
118
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 116

def initialize(entities)
  @entities = entities.freeze
end

Instance Attribute Details

#entitiesObject (readonly)



111
112
113
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 111

def entities
  @entities
end

Class Method Details

.from_hash(hash) ⇒ Object



105
106
107
108
109
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 105

def self.from_hash(hash)
  instance = allocate
  instance.initialize_from_hash(hash)
  instance.freeze
end

Instance Method Details

#[](id) ⇒ Object



124
125
126
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 124

def [](id)
  @entities[id]
end

#initialize_from_hash(hash) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 148

def initialize_from_hash(hash)
  entities = {}
  emap = hash['entities'] || hash[:entities]
  if emap.is_a?(Hash)
    mod = OverviewModel
    emap.each do |id, ea|
      id = id.to_i # since JSON converts integer keys to strings
      eclass = mod.const_get(ea[0])
      entities[id] = eclass.from_hash(ea[1].merge('_id' => id))
    end
  end
  @entities = entities.freeze
end

#of_class(entity_class) ⇒ WrappingArray<Wrapper>

Returns an array of wrappers for all contained entities of the given class

Parameters:

Returns:

  • (WrappingArray<Wrapper>)

    the wrapped entities



134
135
136
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 134

def of_class(entity_class)
  Query::WrappingArray.new(raw_of_class(entity_class).map { |entity| Query::Wrapper.new(self, entity) })
end

#raw_of_class(entity_class) ⇒ Array<Entity>

Returns an array of all contained entities of the given class

Parameters:

Returns:

  • (Array<Entity>)

    the entities



144
145
146
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 144

def raw_of_class(entity_class)
  @entities.values.select {|e| e.is_a?(entity_class)}
end

#to_hashObject



120
121
122
# File 'lib/puppet_x/puppetlabs/migration/overview_model.rb', line 120

def to_hash
  { :entities => Hash[@entities.map { |k, v| [k, [v.class.simple_name, v.to_hash]] }] }
end