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



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

#nameString (readonly)

Returns:

  • (String)


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

def name
  @name
end

#spectrumArray<Hash> (readonly)

Returns:

  • (Array<Hash>)


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

def spectrum
  @spectrum
end

#typeSymbol (readonly)

Returns:

  • (Symbol)


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

Parameters:

  • item (Hash)

Returns:

  • (self)


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

Parameters:

  • idx (Integer)

Returns:

  • (Hash)


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

#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>)


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

#topHash

Returns:

  • (Hash)


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

def top
  spectrum.last || {}
end