class SparkleFormation::SparkleCollection::Rainbow

Contains a layered number of a specific item defined via a Sparkle. Items higher in the spectrum (greater layer index) have higher precedence than those below. This can be used for properly generating the end result based on merging or knockout rules.

Constants

VALID_TYPES

Valid types for a rainbow

Attributes

name[R]

@return [String]

spectrum[R]

@return [Array<Hash>]

type[R]

@return [Symbol]

Public Class Methods

new(name, type) click to toggle source

Create a new rainbow

@param name [String, Symbol] name of item @param type [String, Symbol] type of item @return [self]

# File lib/sparkle_formation/sparkle_collection/rainbow.rb, line 35
def initialize(name, type)
  unless VALID_TYPES.include?(type.to_sym)
    raise ArgumentError.new "Invalid type provdied for Rainbow instance `#{type}`"
  end
  @name = name.to_s
  @type = type.to_sym
  @spectrum = []
end

Public Instance Methods

add_layer(item) click to toggle source

Add a new layer to the top of the spectrum

@param item [Hash] @return [self]

# File lib/sparkle_formation/sparkle_collection/rainbow.rb, line 48
def add_layer(item)
  unless item.is_a?(Hash)
    raise TypeError.new "Expecting type `Hash` but received type `#{item.class}`"
  end
  spectrum << item.to_smash
  self
end
layer_at(idx) click to toggle source

Fetch item defined at given layer

@param idx [Integer] @return [Hash]

# File lib/sparkle_formation/sparkle_collection/rainbow.rb, line 60
def layer_at(idx)
  if idx <= spectrum.size
    spectrum.at(idx)
  else
    raise KeyError.new "Invalid layer requested for #{type} - #{name} (index: #{idx})"
  end
end
monochrome() click to toggle source

Generates a list of items to be processed in order to achieve the correct result based on expected merging behavior

@return [Array<Hash>]

# File lib/sparkle_formation/sparkle_collection/rainbow.rb, line 73
def monochrome
  Array.new.tap do |result|
    spectrum.each do |item|
      unless item.get(:args, :layering).to_s == "merge"
        result.clear
      end
      result << item
    end
  end
end
top() click to toggle source

@return [Hash]

# File lib/sparkle_formation/sparkle_collection/rainbow.rb, line 85
def top
  spectrum.last || {}
end