Class: SparkleFormation::SparkleCollection

Inherits:
Sparkle
  • Object
show all
Defined in:
lib/sparkle_formation/sparkle_collection.rb,
lib/sparkle_formation/sparkle_collection/rainbow.rb

Overview

TODO:

add unmemoize behavior on collection modification to prevent

Provides a collection of sparkles leak on long running processes with long lasting collections

Defined Under Namespace

Classes: Rainbow

Constant Summary

Constants inherited from Sparkle

SparkleFormation::Sparkle::DIRS, SparkleFormation::Sparkle::TYPES, SparkleFormation::Sparkle::VALID_ROOT_DIRS

Instance Attribute Summary collapse

Attributes inherited from Sparkle

#raw_data, #root

Instance Method Summary collapse

Methods inherited from Sparkle

#eval_wrapper, #inspect, path, register!

Constructor Details

#initialize(args = {}) ⇒ self

Create a new collection of sparkles

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :provider (Symbol, String)

    name of default provider



19
20
21
22
23
# File 'lib/sparkle_formation/sparkle_collection.rb', line 19

def initialize(args={})
  @provider = Bogo::Utility.snake(args.to_smash.fetch(:provider, 'aws')).to_sym
  @root = nil
  @sparkles = []
end

Instance Attribute Details

#providerSymbol

Returns provider.

Returns:

  • (Symbol)

    provider



12
13
14
# File 'lib/sparkle_formation/sparkle_collection.rb', line 12

def provider
  @provider
end

Instance Method Details

#add_sparkle(sparkle, precedence = :high) ⇒ self

Add new sparkle to collection

Parameters:

Returns:

  • (self)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sparkle_formation/sparkle_collection.rb', line 49

def add_sparkle(sparkle, precedence=:high)
  unless(sparkle.is_a?(Sparkle))
    raise TypeError.new "Expected type `SparkleFormation::Sparkle` but received `#{sparkle.class}`!"
  end
  if(precedence == :high)
    @sparkles.push(sparkle).uniq!
  else
    @sparkles.unshift(sparkle).uniq!
  end
  self
end

#apply(collection) ⇒ self

Note:

will overwrite existing set packs

Apply collection settings to this collection

Parameters:

  • collection (SparkleFormation::Collection)

Returns:

  • (self)


30
31
32
33
34
# File 'lib/sparkle_formation/sparkle_collection.rb', line 30

def apply(collection)
  @root = collection.sparkles.last
  @sparkles = collection.sparkles.slice(0, collection.sparkles.length - 1) || []
  self
end

#componentsSmash

Returns:

  • (Smash)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sparkle_formation/sparkle_collection.rb', line 86

def components
  memoize("components_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        sprkl.components.each_pair do |c_provider, c_info|
          c_info.each_pair do |c_name, c_value|
            unless(hsh.get(c_provider, c_name))
              hsh.set(c_provider, c_name, Rainbow.new(c_name, :component))
            end
            hsh.get(c_provider, c_name).add_layer(c_value)
          end
        end
      end
    end
  end
end

#dynamicsSmash

Returns:

  • (Smash)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sparkle_formation/sparkle_collection.rb', line 104

def dynamics
  memoize("dynamics_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        sprkl.dynamics.each_pair do |c_provider, c_info|
          c_info.each_pair do |c_name, c_value|
            unless(hsh.get(c_provider, c_name))
              hsh.set(c_provider, c_name, Rainbow.new(c_name, :dynamic))
            end
            hsh.get(c_provider, c_name).add_layer(c_value)
          end
        end
      end
    end
  end
end

#empty?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


81
82
83
# File 'lib/sparkle_formation/sparkle_collection.rb', line 81

def empty?
  size == 0 # rubocop:disable Style/ZeroLengthPredicate
end

#get(type, name, target_provider = nil) ⇒ Smash

Request item from the store

Parameters:

  • type (String, Symbol)

    item type (see: TYPES)

  • name (String, Symbol)

    name of item

  • target_provider (String, Symbol) (defaults to: nil)

    restrict to provider

Returns:

  • (Smash)

    requested item



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/sparkle_formation/sparkle_collection.rb', line 157

def get(type, name, target_provider=nil)
  type_name = Sparkle::TYPES[type.to_s]
  unless(type_name)
    raise ArgumentError.new "Unknown file type requested from collection `#{type}`"
  end
  result = nil
  error = nil
  unless(target_provider)
    target_provider = provider
  end
  result = send(type_name).get(target_provider, name)
  if(result.nil? && type_name == 'templates')
    t_direct = sparkles.map do |pack|
      begin
        pack.get(:template, name, target_provider)
      rescue Error::NotFound
      end
    end.compact.last
    if(t_direct)
      result = send(type_name).get(target_provider, t_direct[:name])
    end
  end
  unless(result)
    error_klass = Error::NotFound.const_get(
      Bogo::Utility.camel(type)
    )
    raise error_klass.new(:name => name)
  end
  result
end

#registriesSmash

Returns:

  • (Smash)


122
123
124
125
126
127
128
129
130
# File 'lib/sparkle_formation/sparkle_collection.rb', line 122

def registries
  memoize("registries_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        hsh.deep_merge!(sprkl.registries)
      end
    end
  end
end

#remove_sparkle(sparkle) ⇒ self

Remove sparkle from collection

Parameters:

Returns:

  • (self)


65
66
67
68
# File 'lib/sparkle_formation/sparkle_collection.rb', line 65

def remove_sparkle(sparkle)
  @sparkles.delete(sparkle)
  self
end

#set_root(sparkle) ⇒ self

Set the root sparkle which forces highest precedence

Parameters:

Returns:

  • (self)


40
41
42
43
# File 'lib/sparkle_formation/sparkle_collection.rb', line 40

def set_root(sparkle)
  @root = sparkle
  self
end

#sizeInteger

Returns:

  • (Integer)


76
77
78
# File 'lib/sparkle_formation/sparkle_collection.rb', line 76

def size
  sparkles.size
end

#sparkle_at(idx) ⇒ Sparkle, NilClass

Returns:



71
72
73
# File 'lib/sparkle_formation/sparkle_collection.rb', line 71

def sparkle_at(idx)
  sparkles.at(idx)
end

#templatesSmash

Returns:

  • (Smash)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sparkle_formation/sparkle_collection.rb', line 133

def templates
  memoize("templates_#{checksum}") do
    Smash.new.tap do |hsh|
      sparkles.each do |sprkl|
        sprkl.templates.each_pair do |c_provider, c_info|
          c_info.each_pair do |c_name, c_value|
            unless(hsh.get(c_provider, c_name))
              hsh.set(c_provider, c_name, Rainbow.new(c_name, :template))
            end
            hsh.get(c_provider, c_name).add_layer(c_value)
          end
        end
      end
    end
  end
end