Class: WebFunction::Attribute
- Inherits:
-
Object
- Object
- WebFunction::Attribute
- Includes:
- Flaggable
- Defined in:
- lib/web_function/attribute.rb
Overview
Instance Attribute Summary collapse
-
#docs ⇒ String
readonly
A markdown string describing this attribute and its purpose in the output object.
-
#hint ⇒ String?
readonly
A string hinting about the semantics of this attribute.
-
#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:, hint: nil, 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:, 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
#docs ⇒ String (readonly)
A markdown string describing this attribute and its purpose in the output object. Used by documentation tools, and highly recommended.
114 115 116 |
# File 'lib/web_function/attribute.rb', line 114 def docs @docs end |
#hint ⇒ String? (readonly)
A string hinting about the semantics of this attribute. See the [hints section] for possible values and documentation tooling guidance.
98 99 100 |
# File 'lib/web_function/attribute.rb', line 98 def hint @hint 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.
75 76 77 |
# File 'lib/web_function/attribute.rb', line 75 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.
89 90 91 |
# File 'lib/web_function/attribute.rb', line 89 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).
107 108 109 |
# File 'lib/web_function/attribute.rb', line 107 def values @values end |
Class Method Details
.from_array(attributes) ⇒ Array<Attribute>
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.
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.
120 121 122 |
# File 'lib/web_function/attribute.rb', line 120 def nullable? flag?("nullable") end |