Class: WebFunction::Attribute
- Inherits:
-
Object
- Object
- WebFunction::Attribute
- 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.
Instance Attribute Summary collapse
-
#docs ⇒ String
readonly
A markdown string describing this attribute and its purpose in the output object.
-
#name ⇒ String
readonly
The name of the attribute as it will appear in the endpoint's output object.
-
#type ⇒ String
readonly
The type of value returned for this attribute.
-
#values ⇒ Array
readonly
An array specifying the exact, case-sensitive values that may be returned for this attribute.
Attributes included from Flaggable
Class Method Summary collapse
-
.from_array(attributes) ⇒ Array<Attribute>
Creates a new Attribute from an array of hashes.
-
.from_hash(attribute) ⇒ Attribute
Creates a new Attribute from a hash.
Instance Method Summary collapse
-
#initialize(name:, type:, values: [], flags: [], docs: nil) ⇒ Attribute
constructor
A new instance of Attribute.
-
#nullable? ⇒ Boolean
Whether the attribute can be null.
Methods included from Flaggable
Constructor Details
#initialize(name:, type:, values: [], flags: [], docs: nil) ⇒ Attribute
Returns a new instance of Attribute.
15 16 17 18 19 20 21 |
# File 'lib/web_function/attribute.rb', line 15 def initialize(name:, type:, values: [], flags: [], docs: nil) @name = name @type = Type.parse(type) @values = values @flags = flags @docs = docs.to_s end |
Instance Attribute Details
#docs ⇒ String (readonly)
A markdown string describing this attribute and its purpose in the output object. Used by documentation tools, and highly recommended.
103 104 105 |
# File 'lib/web_function/attribute.rb', line 103 def docs @docs end |
#name ⇒ String (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.
73 74 75 |
# File 'lib/web_function/attribute.rb', line 73 def name @name end |
#type ⇒ String (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.
87 88 89 |
# File 'lib/web_function/attribute.rb', line 87 def type @type end |
#values ⇒ Array (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).
96 97 98 |
# File 'lib/web_function/attribute.rb', line 96 def values @values end |
Class Method Details
.from_array(attributes) ⇒ Array<Attribute>
59 60 61 62 63 |
# File 'lib/web_function/attribute.rb', line 59 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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/web_function/attribute.rb', line 30 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"], 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.
109 110 111 |
# File 'lib/web_function/attribute.rb', line 109 def nullable? flag?("nullable") end |