Class: SparkleFormation::SparkleCollection::Rainbow

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ self

Create a new rainbow

Parameters:

  • name (String, Symbol)

    name of item

  • type (String, Symbol)

    type of item



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

#nameString (readonly)

Returns:

  • (String)


24
25
26
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 24

def name
  @name
end

#spectrumArray<Hash> (readonly)

Returns:

  • (Array<Hash>)


28
29
30
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 28

def spectrum
  @spectrum
end

#typeSymbol (readonly)

Returns:

  • (Symbol)


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

Parameters:

  • item (Hash)

Returns:

  • (self)


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

Parameters:

  • idx (Integer)

Returns:

  • (Hash)


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

#monochromeArray<Hash>

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

Returns:

  • (Array<Hash>)


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

#topHash

Returns:

  • (Hash)


85
86
87
# File 'lib/sparkle_formation/sparkle_collection/rainbow.rb', line 85

def top
  spectrum.last || {}
end