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

Instance Attribute Summary collapse

Instance Method Summary collapse

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



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

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



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

def provider
  @provider
end

Instance Method Details

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

Add new sparkle to collection

Parameters:

Returns:

  • (self)


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

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)


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

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

#componentsSmash

Returns:

  • (Smash)


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

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)


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

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)


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

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

Raises:



156
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
# File 'lib/sparkle_formation/sparkle_collection.rb', line 156

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)


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

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)


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

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

#set_root(sparkle) ⇒ self

Set the root sparkle which forces highest precedence

Parameters:

Returns:

  • (self)


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

def set_root(sparkle)
  @root = sparkle
  self
end

#sizeInteger

Returns:

  • (Integer)


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

def size
  sparkles.size
end

#sparkle_at(idx) ⇒ Sparkle, NilClass

Returns:



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

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

#templatesSmash

Returns:

  • (Smash)


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

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