Class: SoberSwag::Nodes::Attribute

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

Overview

This is a node for one attribute of an object. An object type is represented by a SoberSwag::Nodes::Object full of these keys.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #eql?, #hash

Constructor Details

#initialize(key, required, value, meta = {}) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • key (Symbol)

    the key of this attribute

  • required (Boolean)

    if this attribute must be set or not

  • value (Class)

    the type of this attribute

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

    the metadata associated with this attribute



14
15
16
17
18
19
# File 'lib/sober_swag/nodes/attribute.rb', line 14

def initialize(key, required, value, meta = {})
  @key = key
  @required = required
  @value = value
  @meta = meta
end

Instance Attribute Details

#keySymbol (readonly)

Returns:

  • (Symbol)


42
43
44
# File 'lib/sober_swag/nodes/attribute.rb', line 42

def key
  @key
end

#metaHash (readonly)

Returns the metadata for this attribute.

Returns:

  • (Hash)

    the metadata for this attribute.



54
55
56
# File 'lib/sober_swag/nodes/attribute.rb', line 54

def meta
  @meta
end

#requiredBoolean (readonly)

Returns true if this attribute must be set, false otherwise.

Returns:

  • (Boolean)

    true if this attribute must be set, false otherwise.



46
47
48
# File 'lib/sober_swag/nodes/attribute.rb', line 46

def required
  @required
end

#valueClass (readonly)

Returns the type of this attribute.

Returns:

  • (Class)

    the type of this attribute



50
51
52
# File 'lib/sober_swag/nodes/attribute.rb', line 50

def value
  @value
end

Instance Method Details

#cata(&block) ⇒ Object

See Also:



64
65
66
67
68
69
70
# File 'lib/sober_swag/nodes/attribute.rb', line 64

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

#deconstructArray(Symbol, Boolean, Class, Hash)

Deconstruct into the #key, #required, #value, and #meta attributes of this SoberSwag::Nodes::Attribute object.

Returns:

  • (Array(Symbol, Boolean, Class, Hash))

    the attributes of this object



26
27
28
# File 'lib/sober_swag/nodes/attribute.rb', line 26

def deconstruct
  [key, required, value, meta]
end

#deconstruct_keys(_keys) ⇒ Hash

Deconstructs into #key, #required, #value, and #meta attributes, as a hash with the attribute names as the keys.

Parameters:

  • _keys (void)

    ignored

Returns:

  • (Hash)

    the attributes as keys.



36
37
38
# File 'lib/sober_swag/nodes/attribute.rb', line 36

def deconstruct_keys(_keys)
  { key: key, required: required, value: value, meta: meta }
end

#map(&block) ⇒ Object

See Also:



58
59
60
# File 'lib/sober_swag/nodes/attribute.rb', line 58

def map(&block)
  self.class.new(key, required, value.map(&block), meta)
end