Class: SoberSwag::Nodes::Array
- Defined in:
- lib/sober_swag/nodes/array.rb
Overview
Base class for nodes that contain arrays of other nodes. This is very different from an attribute representing a node which is an array of some element type!!
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Instance Method Summary collapse
-
#cata(&block) ⇒ Object
The block will be called with each element contained in this array node in turn, then called with a Array constructed from the resulting values.
-
#deconstruct ⇒ Array<SoberSwag::Nodes::Base>
Deconstructs into the elements.
-
#deconstruct_keys(_keys) ⇒ Hash{Symbol => ::Array<SoberSwag::Nodes::Base>}
Deconstruction for pattern-matching.
-
#initialize(elements) ⇒ Array
constructor
A new instance of Array.
- #map(&block) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(elements) ⇒ Array
Returns a new instance of Array.
7 8 9 |
# File 'lib/sober_swag/nodes/array.rb', line 7 def initialize(elements) @elements = elements end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
11 12 13 |
# File 'lib/sober_swag/nodes/array.rb', line 11 def elements @elements end |
Instance Method Details
#cata(&block) ⇒ Object
The block will be called with each element contained in this array node in turn, then called with a SoberSwag::Nodes::Array constructed from the resulting values.
27 28 29 |
# File 'lib/sober_swag/nodes/array.rb', line 27 def cata(&block) block.call(self.class.new(elements.map { |elem| elem.cata(&block) })) end |
#deconstruct ⇒ Array<SoberSwag::Nodes::Base>
Deconstructs into the elements.
35 36 37 |
# File 'lib/sober_swag/nodes/array.rb', line 35 def deconstruct @elements end |
#deconstruct_keys(_keys) ⇒ Hash{Symbol => ::Array<SoberSwag::Nodes::Base>}
Deconstruction for pattern-matching
44 45 46 |
# File 'lib/sober_swag/nodes/array.rb', line 44 def deconstruct_keys(_keys) { elements: @elements } end |
#map(&block) ⇒ Object
16 17 18 |
# File 'lib/sober_swag/nodes/array.rb', line 16 def map(&block) self.class.new(elements.map { |elem| elem.map(&block) }) end |