class SparkleFormation::SparkleCollection

Provides a collection of sparkles @todo add unmemoize behavior on collection modification to prevent leak on long running processes with long lasting collections

Attributes

provider[RW]

@return [Symbol] provider

Public Class Methods

new(args = {}) click to toggle source

Create a new collection of sparkles

@param args [Hash] @option args [Symbol, String] :provider name of default provider @return [self]

# 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

Public Instance Methods

add_sparkle(sparkle, precedence = :high) click to toggle source

Add new sparkle to collection

@param sparkle [Sparkle] @return [self]

# 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) click to toggle source

Apply collection settings to this collection

@param collection [SparkleFormation::Collection] @return [self] @note will overwrite existing set packs

# 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
components() click to toggle source

@return [Smash]

# 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
dynamics() click to toggle source

@return [Smash]

# 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?() click to toggle source

@return [TrueClass, FalseClass]

# File lib/sparkle_formation/sparkle_collection.rb, line 80
def empty?
  size == 0 # rubocop:disable Style/ZeroLengthPredicate
end
get(type, name, target_provider = nil) click to toggle source

Request item from the store

@param type [String, Symbol] item type (see: TYPES) @param name [String, Symbol] name of item @param target_provider [String, Symbol] restrict to provider @return [Smash] requested item @raise [NameError, Error::NotFound]

# 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
registries() click to toggle source

@return [Smash]

# 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) click to toggle source

Remove sparkle from collection

@param sparkle [Sparkle] @return [self]

# File lib/sparkle_formation/sparkle_collection.rb, line 64
def remove_sparkle(sparkle)
  @sparkles.delete(sparkle)
  self
end
set_root(sparkle) click to toggle source

Set the root sparkle which forces highest precedence

@param sparkle [Sparkle] @return [self]

# File lib/sparkle_formation/sparkle_collection.rb, line 39
def set_root(sparkle)
  @root = sparkle
  self
end
size() click to toggle source

@return [Integer]

# File lib/sparkle_formation/sparkle_collection.rb, line 75
def size
  sparkles.size
end
sparkle_at(idx) click to toggle source

@return [Sparkle, NilClass]

# File lib/sparkle_formation/sparkle_collection.rb, line 70
def sparkle_at(idx)
  sparkles.at(idx)
end
templates() click to toggle source

@return [Smash]

# 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

Protected Instance Methods

checksum() click to toggle source

@return [String] checksum of sparkles

# File lib/sparkle_formation/sparkle_collection.rb, line 195
def checksum
  Smash.new.tap do |s|
    sparkles.each_with_index do |v, i|
      s[i.to_s] = v
    end
  end.checksum
end
sparkles() click to toggle source

@return [Array<Sparkle>]

# File lib/sparkle_formation/sparkle_collection.rb, line 190
def sparkles
  (@sparkles + [@root]).compact
end