Sha256: 9398861aaabe40b1dc64a43e155c336ffbba273c6f6a30e881ed3659308ad8c0

Contents?: true

Size: 1.71 KB

Versions: 9

Compression:

Stored size: 1.71 KB

Contents

module SoberSwag
  module Nodes
    ##
    # Root node of the tree.
    # This contains "primitive values."
    # Initially, this is probably the types of attributes or array elements or whatever.
    # As we use {#cata} to transform this, it will start containing swagger-compatible type objects.
    #
    # This node can contain metadata as well.
    class Primitive < Base
      ##
      # @param value [Object] the primitive value to store
      # @param metadata [Hash] the metadata
      def initialize(value, metadata = {})
        @value = value
        @metadata = metadata
      end

      ##
      # @return [Object] the contained value
      attr_reader :value

      ##
      # @return [Hash] metadata associated with the contained value.
      attr_reader :metadata

      ##
      # @see SoberSwag::Nodes::Base#map
      #
      # This will actually map over {#value}.
      def map(&block)
        self.class.new(block.call(value), metadata.dup)
      end

      ##
      # Deconstructs to the value and the metadata.
      #
      # @return [Array(Object, Hash)] containing value and metadata.
      def deconstruct
        [value, metadata]
      end

      ##
      # Wraps the attributes in a hash.
      #
      # @return [Hash{Symbol => Object, Hash}]
      #   {#value} in `value:`, {#metadata} in `metadata:`
      def deconstruct_keys(_)
        { value: value, metadata: metadata }
      end

      ##
      # @see SoberSwag::Nodes::Base#cata
      # As this is a root node, we actually call the block directly.
      # @yieldparam node [SoberSwag::Nodes::Primitive] this node.
      # @return whatever the block returns.
      def cata(&block)
        block.call(self.class.new(value, metadata))
      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/primitive.rb
sober_swag-0.25.1 lib/sober_swag/nodes/primitive.rb
sober_swag-0.25.0 lib/sober_swag/nodes/primitive.rb
sober_swag-0.24.1 lib/sober_swag/nodes/primitive.rb
sober_swag-0.24.0 lib/sober_swag/nodes/primitive.rb
sober_swag-0.23.0 lib/sober_swag/nodes/primitive.rb
sober_swag-0.22.0 lib/sober_swag/nodes/primitive.rb
sober_swag-0.21.0 lib/sober_swag/nodes/primitive.rb
sober_swag-0.20.0 lib/sober_swag/nodes/primitive.rb