Class: SparkleFormation::SparkleCollection::Rainbow
- Inherits:
-
Object
- Object
- SparkleFormation::SparkleCollection::Rainbow
- Extended by:
- Forwardable
- Defined in:
- lib/sparkle_formation/sparkle_collection/rainbow.rb
Overview
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.
Constant Summary collapse
- VALID_TYPES =
Valid types for a rainbow
[ :template, :component, :dynamic, ].freeze
Instance Attribute Summary collapse
- #name ⇒ String readonly
- #spectrum ⇒ Array<Hash> readonly
- #type ⇒ Symbol readonly
Instance Method Summary collapse
-
#add_layer(item) ⇒ self
Add a new layer to the top of the spectrum.
-
#initialize(name, type) ⇒ self
constructor
Create a new rainbow.
-
#layer_at(idx) ⇒ Hash
Fetch item defined at given layer.
-
#monochrome ⇒ Array<Hash>
Generates a list of items to be processed in order to achieve the correct result based on expected merging behavior.
- #top ⇒ Hash
Constructor Details
#initialize(name, type) ⇒ self
Create a new rainbow
35 36 37 38 39 40 41 42 |
# 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 |
Instance Attribute Details
#name ⇒ String (readonly)
24 25 26 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 24 def name @name end |
#spectrum ⇒ Array<Hash> (readonly)
28 29 30 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 28 def spectrum @spectrum end |
#type ⇒ Symbol (readonly)
26 27 28 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 26 def type @type end |
Instance Method Details
#add_layer(item) ⇒ self
Add a new layer to the top of the spectrum
48 49 50 51 52 53 54 |
# 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) ⇒ Hash
Fetch item defined at given layer
60 61 62 63 64 65 66 |
# 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 ⇒ Array<Hash>
Generates a list of items to be processed in order to achieve the correct result based on expected merging behavior
73 74 75 76 77 78 79 80 81 82 |
# 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 ⇒ Hash
85 86 87 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 85 def top spectrum.last || {} end |