Class: WebFunction::ObjectSchema
- Inherits:
-
Object
- Object
- WebFunction::ObjectSchema
- Defined in:
- lib/web_function/object_schema.rb
Overview
Represents a named object definition as described in a Web Function package.
Objects are declared in a package under the "objects" key and can be referenced as a refined object type
(object.<name>) anywhere a type is expected. An object. reference appears in one of two contexts, which
determines which set of properties applies:
- Argument context — the object is referenced as an argument's
type. Its #arguments describe its properties. - Attribute context — the object is referenced as an endpoint's
returnsor as an attribute'stype. Its #attributes describe its properties.
Because an object MAY be referenced in both contexts within the same package, it MAY define both arguments and
attributes; each set is used only in its matching context.
It is named ObjectSchema rather than Object to avoid clashing with Ruby's built-in ::Object.
See the object definition documentation on the Web Function website for more details.
Constant Summary collapse
- CONTEXTS =
The contexts in which an object may be referenced. Each maps to the member set that applies in that context.
%i[arguments attributes].freeze
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The name of the object.
Class Method Summary collapse
-
.from_array(objects) ⇒ Array<ObjectSchema>
Creates a new ObjectSchema from an array of hashes.
-
.from_hash(object) ⇒ ObjectSchema?
Creates a new ObjectSchema from a hash.
Instance Method Summary collapse
-
#argument(name) ⇒ Argument?
Looks up a single argument member by name.
-
#arguments ⇒ Array<Argument>
The object's properties when it is referenced in an argument context.
-
#attribute(name) ⇒ Attribute?
Looks up a single attribute member by name.
-
#attributes ⇒ Array<Attribute>
The object's properties when it is referenced in an attribute context.
-
#initialize(name:, arguments: [], attributes: []) ⇒ ObjectSchema
constructor
A new instance of ObjectSchema.
-
#properties(context) ⇒ Array<Argument>, Array<Attribute>
The object's properties for the given context.
Constructor Details
#initialize(name:, arguments: [], attributes: []) ⇒ ObjectSchema
Returns a new instance of ObjectSchema.
30 31 32 33 34 |
# File 'lib/web_function/object_schema.rb', line 30 def initialize(name:, arguments: [], attributes: []) @name = name @arguments = arguments.to_h { |a| [a.name, a] } @attributes = attributes.to_h { |a| [a.name, a] } end |
Instance Attribute Details
#name ⇒ String (readonly)
The name of the object. It is referenced as a refined object type (object.<name>) and is unique within a
package.
78 79 80 |
# File 'lib/web_function/object_schema.rb', line 78 def name @name end |
Class Method Details
.from_array(objects) ⇒ Array<ObjectSchema>
66 67 68 69 70 |
# File 'lib/web_function/object_schema.rb', line 66 def from_array(objects) Utils.normalize_array objects do |object| from_hash(object) end end |
.from_hash(object) ⇒ ObjectSchema?
Creates a new ObjectSchema from a hash. Typically coming from a Package.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/web_function/object_schema.rb', line 43 def from_hash(object) unless object.is_a?(Hash) return end unless object["name"] return end new( name: object["name"], arguments: Argument.from_array(object["arguments"]), attributes: Attribute.from_array(object["attributes"]), ) end |
Instance Method Details
#argument(name) ⇒ Argument?
Looks up a single argument member by name.
94 95 96 |
# File 'lib/web_function/object_schema.rb', line 94 def argument(name) @arguments[name.to_s] end |
#arguments ⇒ Array<Argument>
The object's properties when it is referenced in an argument context.
84 85 86 |
# File 'lib/web_function/object_schema.rb', line 84 def arguments @arguments.values end |
#attribute(name) ⇒ Attribute?
Looks up a single attribute member by name.
112 113 114 |
# File 'lib/web_function/object_schema.rb', line 112 def attribute(name) @attributes[name.to_s] end |
#attributes ⇒ Array<Attribute>
The object's properties when it is referenced in an attribute context.
102 103 104 |
# File 'lib/web_function/object_schema.rb', line 102 def attributes @attributes.values end |
#properties(context) ⇒ Array<Argument>, Array<Attribute>
The object's properties for the given context.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/web_function/object_schema.rb', line 124 def properties(context) case context when :arguments arguments when :attributes attributes else raise ArgumentError, "context must be one of #{CONTEXTS.inspect}, got #{context.inspect}" end end |