Class: SparkleFormation::Composition
- Inherits:
-
Object
- Object
- SparkleFormation::Composition
- Defined in:
- lib/sparkle_formation/composition.rb
Overview
Internal template composition
Defined Under Namespace
Instance Attribute Summary collapse
-
#origin ⇒ SparkleFormation
readonly
Owner of composition.
Instance Method Summary collapse
-
#add_component(item, location = :append) ⇒ self
Add an existing component.
-
#add_override(item, location = :append) ⇒ self
Add an existing override.
- #components ⇒ Array<Component, Override>
-
#composite ⇒ Array<Component, Override>
Provides the full list of items in order.
-
#each {|| ... } ⇒ self
Iterate full composition.
-
#initialize(origin, args = {}) ⇒ self
constructor
Create a new composition.
-
#new_component(key, location = :append) { ... } ⇒ self
Add a new component.
-
#new_override(args = {}, location = :append) { ... } ⇒ self
Add a new override.
- #overrides ⇒ Array<Override>
Constructor Details
#initialize(origin, args = {}) ⇒ self
Create a new 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
#origin ⇒ SparkleFormation (readonly)
Returns owner of composition
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
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
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 |
#components ⇒ Array<Component, Override>
44 45 46 |
# File 'lib/sparkle_formation/composition.rb', line 44 def components @components_list.dup.freeze end |
#composite ⇒ Array<Component, Override>
Provides the full list of items in order
56 57 58 |
# File 'lib/sparkle_formation/composition.rb', line 56 def composite [components + overrides].flatten.freeze end |
#each {|| ... } ⇒ self
Iterate full composition
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
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
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 |
#overrides ⇒ Array<Override>
49 50 51 |
# File 'lib/sparkle_formation/composition.rb', line 49 def overrides @overrides_list.dup.freeze end |