Class: WebFunction::ObjectSchema

Inherits:
Object
  • Object
show all
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 returns or as an attribute's type. 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.

Returns:

  • (Array<Symbol>)
%i[arguments attributes].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameString (readonly)

The name of the object. It is referenced as a refined object type (object.<name>) and is unique within a package.

Returns:

  • (String)


78
79
80
# File 'lib/web_function/object_schema.rb', line 78

def name
  @name
end

Class Method Details

.from_array(objects) ⇒ Array<ObjectSchema>

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

Parameters:

  • objects (Array<Hash>)

    The object array of hashes

Returns:

  • (Array<ObjectSchema>)

    A new array of ObjectSchema instances



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.

Parameters:

  • object (Hash)

    The object hash

Returns:

  • (ObjectSchema, nil)

    A new ObjectSchema instance, or nil if the hash is invalid.



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.

Parameters:

  • name (String, Symbol)

    The name of the argument to look up.

Returns:

  • (Argument, nil)

    The matching argument, or nil if none is found.



94
95
96
# File 'lib/web_function/object_schema.rb', line 94

def argument(name)
  @arguments[name.to_s]
end

#argumentsArray<Argument>

The object's properties when it is referenced in an argument context.

Returns:



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.

Parameters:

  • name (String, Symbol)

    The name of the attribute to look up.

Returns:

  • (Attribute, nil)

    The matching attribute, or nil if none is found.



112
113
114
# File 'lib/web_function/object_schema.rb', line 112

def attribute(name)
  @attributes[name.to_s]
end

#attributesArray<Attribute>

The object's properties when it is referenced in an attribute context.

Returns:



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.

Parameters:

  • context (Symbol)

    The context to resolve properties for. One of CONTEXTS.

Returns:

  • (Array<Argument>, Array<Attribute>)

    The properties that apply in the given context.

Raises:

  • (ArgumentError)

    If the context is not one of CONTEXTS.



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