Class: WebFunction::Attribute

Inherits:
Object
  • Object
show all
Includes:
Flaggable
Defined in:
lib/web_function/attribute.rb

Overview

Attributes define output fields that may be produced and returned by a Web Function Endpoint when the type of the return is ‘object`.

See the [attributes section] on the Web Function website for more details about attribute definitions, recognized keys, and usage.

[0]: webfunction.org/package#attributes

Instance Attribute Summary collapse

Attributes included from Flaggable

#flags

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Flaggable

#flag?

Constructor Details

#initialize(name:, type:, hint: nil, values: [], flags: [], docs: nil) ⇒ Attribute

Returns a new instance of Attribute.



15
16
17
18
19
20
21
22
# File 'lib/web_function/attribute.rb', line 15

def initialize(name:, type:, hint: nil, values: [], flags: [], docs: nil)
  @name = name
  @type = type
  @hint = hint
  @values = values
  @flags = flags
  @docs = docs.to_s
end

Instance Attribute Details

#docsString (readonly)

A markdown string describing this attribute and its purpose in the output object. Used by documentation tools, and highly recommended.

Returns:

  • (String)


114
115
116
# File 'lib/web_function/attribute.rb', line 114

def docs
  @docs
end

#hintString? (readonly)

A string hinting about the semantics of this attribute. See the [hints section] for possible values and documentation tooling guidance.

[1]: webfunction.org/package#hints

Returns:

  • (String, nil)


98
99
100
# File 'lib/web_function/attribute.rb', line 98

def hint
  @hint
end

#nameString (readonly)

The name of the attribute as it will appear in the endpoint’s output object.

This is required for the argument to be valid.

Returns:

  • (String)


75
76
77
# File 'lib/web_function/attribute.rb', line 75

def name
  @name
end

#typeString (readonly)

The type of value returned for this attribute. Must be one of:

  • object

  • array

  • string

  • number

  • boolean

This is required for the argument to be valid.

Returns:

  • (String)


89
90
91
# File 'lib/web_function/attribute.rb', line 89

def type
  @type
end

#valuesArray (readonly)

An array specifying the exact, case-sensitive values that may be returned for this attribute. Each value in the values array must conform to the data type specified in the “type” key.

This is useful for attributes that can only take a select set of values (enums or constants).

Returns:

  • (Array)


107
108
109
# File 'lib/web_function/attribute.rb', line 107

def values
  @values
end

Class Method Details

.from_array(attributes) ⇒ Array<Attribute>

Creates a new Attribute from an array of hashes. Typically coming from a Package. Uses from_hash under the hood.

Parameters:

  • attributes (Array<Hash>)

    The attribute array of hashes

Returns:

  • (Array<Attribute>)

    A new array of Attribute instances



61
62
63
64
65
# File 'lib/web_function/attribute.rb', line 61

def from_array(attributes)
  Utils.normalize_array attributes do |attribute|
    from_hash(attribute)
  end
end

.from_hash(attribute) ⇒ Attribute

Creates a new Attribute from a hash. Typically coming from a Package.

Parameters:

  • attribute (Hash)

    The attribute hash

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/web_function/attribute.rb', line 31

def from_hash(attribute)
  unless attribute.is_a?(Hash)
    return
  end

  unless attribute["name"]
    return
  end

  unless attribute["type"]
    return
  end

  new(
    name: attribute["name"],
    type: attribute["type"],
    hint: attribute["hint"],
    values: [*attribute["values"]],
    flags: Utils.normalize_array_of_strings(attribute["flags"]),
    docs: attribute["docs"],
  )
end

Instance Method Details

#nullable?Boolean

Whether the attribute can be null.

Returns:

  • (Boolean)


120
121
122
# File 'lib/web_function/attribute.rb', line 120

def nullable?
  flag?("nullable")
end