Sha256: a8c81a3af46dbf4cfb6cd1c392839efed50df152b0bb2a994e33d4638f9887e1

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

module SoberSwag
  module Nodes
    ##
    # 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!!
    class Array < Base
      def initialize(elements)
        @elements = elements
      end

      attr_reader :elements

      ##
      # @see SoberSwag::Nodes::Array#map
      #
      def map(&block)
        self.class.new(elements.map { |elem| elem.map(&block) })
      end

      ##
      # @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.
      def cata(&block)
        block.call(self.class.new(elements.map { |elem| elem.cata(&block) }))
      end

      ##
      # Deconstructs into the elements.
      #
      # @return [Array<SoberSwag::Nodes::Base>]
      def deconstruct
        @elements
      end

      ##
      # Deconstruction for pattern-matching
      #
      # @return [Hash{Symbol => ::Array<SoberSwag::Nodes::Base>}]
      #   a hash with the elements in the `:elements` key.
      def deconstruct_keys(_keys)
        { elements: @elements }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sober_swag-0.25.2 lib/sober_swag/nodes/array.rb
sober_swag-0.25.1 lib/sober_swag/nodes/array.rb
sober_swag-0.25.0 lib/sober_swag/nodes/array.rb
sober_swag-0.24.1 lib/sober_swag/nodes/array.rb
sober_swag-0.24.0 lib/sober_swag/nodes/array.rb
sober_swag-0.23.0 lib/sober_swag/nodes/array.rb
sober_swag-0.22.0 lib/sober_swag/nodes/array.rb
sober_swag-0.21.0 lib/sober_swag/nodes/array.rb
sober_swag-0.20.0 lib/sober_swag/nodes/array.rb