class SoberSwag::Nodes::Array

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!!

Attributes

elements[R]

Public Class Methods

new(elements) click to toggle source
# File lib/sober_swag/nodes/array.rb, line 7
def initialize(elements)
  @elements = elements
end

Public Instance Methods

cata(&block) click to toggle source

@see SoberSwag::Nodes::Array#cata

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.

@return whatever the block yields.

# 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() click to toggle source

Deconstructs into the elements.

@return [Array<SoberSwag::Nodes::Base>]

# File lib/sober_swag/nodes/array.rb, line 35
def deconstruct
  @elements
end
deconstruct_keys(_keys) click to toggle source

Deconstruction for pattern-matching

@return [Hash{Symbol => ::Array<SoberSwag::Nodes::Base>}]

a hash with the elements in the `:elements` key.
# File lib/sober_swag/nodes/array.rb, line 44
def deconstruct_keys(_keys)
  { elements: @elements }
end
map(&block) click to toggle source

@see SoberSwag::Nodes::Array#map

# File lib/sober_swag/nodes/array.rb, line 16
def map(&block)
  self.class.new(elements.map { |elem| elem.map(&block) })
end