Class: SoberSwag::Nodes::Array

Inherits:
Base
  • Object
show all
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!!

Direct Known Subclasses

Object, OneOf

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #eql?, #hash

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

#elementsObject (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.

Returns:

  • whatever the block yields.

See Also:



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

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

Returns:



44
45
46
# File 'lib/sober_swag/nodes/array.rb', line 44

def deconstruct_keys(_keys)
  { elements: @elements }
end

#map(&block) ⇒ Object

See Also:



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