Class: SoberSwag::Nodes::Primitive

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/nodes/primitive.rb

Overview

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.

Direct Known Subclasses

NullablePrimitive

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #eql?, #hash

Constructor Details

#initialize(value, metadata = {}) ⇒ Primitive

Returns a new instance of Primitive.

Parameters:

  • value (Object)

    the primitive value to store

  • metadata (Hash) (defaults to: {})

    the metadata



14
15
16
17
# File 'lib/sober_swag/nodes/primitive.rb', line 14

def initialize(value,  = {})
  @value = value
  @metadata = 
end

Instance Attribute Details

#metadataHash (readonly)

Returns metadata associated with the contained value.

Returns:

  • (Hash)

    metadata associated with the contained value.



25
26
27
# File 'lib/sober_swag/nodes/primitive.rb', line 25

def 
  @metadata
end

#valueObject (readonly)

Returns the contained value.

Returns:

  • (Object)

    the contained value



21
22
23
# File 'lib/sober_swag/nodes/primitive.rb', line 21

def value
  @value
end

Instance Method Details

#cata {|node| ... } ⇒ Object

As this is a root node, we actually call the block directly.

Yield Parameters:

Returns:

  • whatever the block returns.

See Also:



57
58
59
# File 'lib/sober_swag/nodes/primitive.rb', line 57

def cata(&block)
  block.call(self.class.new(value, ))
end

#deconstructArray(Object, Hash)

Deconstructs to the value and the metadata.

Returns:

  • (Array(Object, Hash))

    containing value and metadata.



39
40
41
# File 'lib/sober_swag/nodes/primitive.rb', line 39

def deconstruct
  [value, ]
end

#deconstruct_keys(_) ⇒ Hash{Symbol => Object, Hash}

Wraps the attributes in a hash.

Returns:



48
49
50
# File 'lib/sober_swag/nodes/primitive.rb', line 48

def deconstruct_keys(_)
  { value: value, metadata:  }
end

#map(&block) ⇒ Object

This will actually map over #value.

See Also:



31
32
33
# File 'lib/sober_swag/nodes/primitive.rb', line 31

def map(&block)
  self.class.new(block.call(value), .dup)
end