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
36 37 38 39 40 41 42 43 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 36 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)
25 26 27 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 25 def name @name end |
#spectrum ⇒ Array<Hash> (readonly)
29 30 31 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 29 def spectrum @spectrum end |
#type ⇒ Symbol (readonly)
27 28 29 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 27 def type @type end |
Instance Method Details
#add_layer(item) ⇒ self
Add a new layer to the top of the spectrum
49 50 51 52 53 54 55 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 49 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
61 62 63 64 65 66 67 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 61 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
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 74 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
86 87 88 |
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 86 def top spectrum.last || {} end |