Class: Bcome::ConfigFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/objects/config_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigFactory

Returns a new instance of ConfigFactory.



6
7
8
9
# File 'lib/objects/config_factory.rb', line 6

def initialize
  @tree = { views: [] }
  @collections = []
end

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



4
5
6
# File 'lib/objects/config_factory.rb', line 4

def tree
  @tree
end

Instance Method Details

#add_crumbs(crumbs, data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/objects/config_factory.rb', line 15

def add_crumbs(crumbs, data)
  views = @tree
  number_crumbs = crumbs.size

  crumbs.each_with_index do |crumb, index|
    is_last_crumb = number_crumbs == (index + 1)
    if this_view = hash_for_identifier_from_view(crumb, views)
      views = this_view
    else
      this_view = { identifier: crumb }
      this_view[:views] = [] unless is_last_crumb && data[:type].to_sym == :inventory
      views[:views] << this_view
      views = hash_for_identifier_from_view(crumb, views)
    end

    this_view.merge!(data) if is_last_crumb
  end
end

#flattenedObject



11
12
13
# File 'lib/objects/config_factory.rb', line 11

def flattened
  @tree[:views].first
end

#hash_for_identifier_from_view(identifier, views) ⇒ Object



34
35
36
37
38
# File 'lib/objects/config_factory.rb', line 34

def hash_for_identifier_from_view(identifier, views)
  raise Bcome::Exception::InventoriesCannotHaveSubViews, 'Inventories cannot hold other inventories - invalid network config' unless views.key?(:views)

  views[:views].select { |v| v[:identifier].to_s == identifier.to_s }.first
end