Class: SparkleFormation::Composition

Inherits:
Object
  • Object
show all
Defined in:
lib/sparkle_formation/composition.rb

Overview

Internal template composition

Defined Under Namespace

Classes: Component, Override

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, args = {}) ⇒ self

Create a new composition

Parameters:

  • origin (SparkleFormation)

    owner of composition

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :components (Array<Component>)

    seed components for composition

  • :overrides (Array<Override>)

    seed overrides for composition



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sparkle_formation/composition.rb', line 27

def initialize(origin, args = {})
  unless origin.is_a?(SparkleFormation)
    raise TypeError.new "Composition requires `SparkleFormation` instance as origin. " \
                        "Received origin type `#{origin.class}`."
  end
  @origin = origin
  @components_list = []
  @overrides_list = []
  seed_value(args[:overrides], Override).each do |item|
    add_override(item)
  end
  seed_value(args[:components], [Component, Override]).each do |item|
    add_component(item)
  end
end

Instance Attribute Details

#originSparkleFormation (readonly)

Returns owner of composition

Returns:



18
19
20
# File 'lib/sparkle_formation/composition.rb', line 18

def origin
  @origin
end

Instance Method Details

#add_component(item, location = :append) ⇒ self

Add an existing component

Parameters:

  • item (Component, Override)
  • location (Symbol) (defaults to: :append)

    :prepend or :append (defaults to :append)

Returns:

  • (self)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sparkle_formation/composition.rb', line 65

def add_component(item, location = :append)
  unless item.is_a?(Component) || item.is_a?(Override)
    raise TypeError.new("Expecting `Component` or `Override` but received `#{item.class}`")
  end
  if item.respond_to?(:key) && component_keys.include?(item.key)
    # do nothing
  else
    case location
    when :append
      components_list.push(item)
    when :prepend
      components_list.unshift(item)
    else
      raise ArgumentError.new "Unknown addition location provided. Valid: `:append, :prepend`. " \
                              "Received: `#{location.inspect}`"
    end
  end
  self
end

#add_override(item, location = :append) ⇒ self

Add an existing override

Parameters:

  • item (Override)
  • location (Symbol) (defaults to: :append)

    :prepend or :append (defaults to :append)

Returns:

  • (self)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sparkle_formation/composition.rb', line 90

def add_override(item, location = :append)
  unless item.is_a?(Override)
    raise TypeError.new("Expecting `Override` but received `#{item.class}`")
  end
  case location
  when :append
    overrides_list.push(item)
  when :prepend
    overrides_list.unshift(item)
  else
    raise ArgumentError.new "Unknown addition location provided. Valid: " \
                            "`:append, :prepend`. Received: `#{location.inspect}`"
  end
  self
end

#componentsArray<Component, Override>

Returns:



44
45
46
# File 'lib/sparkle_formation/composition.rb', line 44

def components
  @components_list.dup.freeze
end

#compositeArray<Component, Override>

Provides the full list of items in order

Returns:



56
57
58
# File 'lib/sparkle_formation/composition.rb', line 56

def composite
  [components + overrides].flatten.freeze
end

#each {|| ... } ⇒ self

Iterate full composition

Yields:

  • block to execute each item

Yield Parameters:

Returns:

  • (self)


139
140
141
142
143
144
145
146
# File 'lib/sparkle_formation/composition.rb', line 139

def each
  if block_given?
    composite.each do |item|
      yield item
    end
  end
  self
end

#new_component(key, location = :append) { ... } ⇒ self

Add a new component

Parameters:

  • key (Symbol, String)

    component identifier

  • location (Symbol) (defaults to: :append)

    :prepend or :append (defaults to :append)

Yields:

  • component block (optional)

Returns:

  • (self)


112
113
114
115
116
# File 'lib/sparkle_formation/composition.rb', line 112

def new_component(key, location = :append, &block)
  comp = Component.new(origin, key, block)
  add_component(comp, location)
  self
end

#new_override(args = {}, location = :append) { ... } ⇒ self

Add a new override

Parameters:

  • args (Hash) (defaults to: {})

    local state provided to override

  • location (Symbol) (defaults to: :append)

    :prepend or :append (defaults to :append)

Yields:

  • override block

Returns:

  • (self)


124
125
126
127
128
129
130
131
132
# File 'lib/sparkle_formation/composition.rb', line 124

def new_override(args = {}, location = :append, &block)
  if args.is_a?(Symbol)
    location = args
    args = {}
  end
  ovr = Override.new(origin, args, block)
  add_override(ovr, location)
  self
end

#overridesArray<Override>

Returns:



49
50
51
# File 'lib/sparkle_formation/composition.rb', line 49

def overrides
  @overrides_list.dup.freeze
end