Class: Apia::ArgumentSet
- Inherits:
-
Object
- Object
- Apia::ArgumentSet
- Extended by:
- Defineable
- Defined in:
- lib/apia/argument_set.rb
Direct Known Subclasses
Defined Under Namespace
Classes: MissingValue
Class Method Summary collapse
-
.collate_objects(set) ⇒ void
Finds all objects referenced by this argument set and add them to the provided set.
-
.create_from_request(request) ⇒ Apia::ArgumentSet
Create a new argument set from a request object.
-
.definition ⇒ Apia::Definitions::ArgumentSet
Return the definition for this argument set.
Instance Method Summary collapse
-
#[](value) ⇒ Object?
Return an item from the argument set.
-
#dig(*values) ⇒ Object?
Return an item from this argument set.
-
#has?(key) ⇒ Boolean
Return whether an argument has been provided or not?.
-
#initialize(hash, path: [], request: nil) ⇒ Apia::ArgumentSet
constructor
Create a new argument set by providing a hash containing the raw arguments.
-
#to_hash ⇒ Hash
Return the source object.
-
#validate(argument, index: nil) ⇒ Object
Validate an argument set and return any errors as appropriate.
Methods included from Defineable
create, inspect, method_missing, name, respond_to_missing?
Constructor Details
#initialize(hash, path: [], request: nil) ⇒ Apia::ArgumentSet
Create a new argument set by providing a hash containing the raw arguments
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/apia/argument_set.rb', line 60 def initialize(hash, path: [], request: nil) unless hash.is_a?(Hash) raise Apia::RuntimeError, 'Hash was expected for argument' end @path = path @request = request @source = self.class.definition.arguments.each_with_object({}) do |(arg_key, argument), source| given_value = lookup_value(hash, arg_key, argument, request) if argument.required? && (given_value.nil? || given_value.is_a?(MissingValue)) raise MissingArgumentError.new(argument, path: @path + [argument]) end # If the given value is missing, we'll just skip adding this to the hash next if given_value.is_a?(MissingValue) given_value = parse_value(argument, given_value) validation_errors = argument.validate_value(given_value) unless validation_errors.empty? raise InvalidArgumentError.new(argument, issue: :validation_errors, errors: validation_errors, path: @path + [argument]) end source[argument.name.to_sym] = given_value end end |
Class Method Details
.collate_objects(set) ⇒ void
This method returns an undefined value.
Finds all objects referenced by this argument set and add them to the provided set.
38 39 40 41 42 |
# File 'lib/apia/argument_set.rb', line 38 def collate_objects(set) definition.arguments.each_value do |argument| set.add_object(argument.type.klass) if argument.type.usable_for_argument? end end |
.create_from_request(request) ⇒ Apia::ArgumentSet
Create a new argument set from a request object
48 49 50 |
# File 'lib/apia/argument_set.rb', line 48 def create_from_request(request) new(request.json_body || request.params || {}, request: request) end |
.definition ⇒ Apia::Definitions::ArgumentSet
Return the definition for this argument set
29 30 31 |
# File 'lib/apia/argument_set.rb', line 29 def definition @definition ||= Definitions::ArgumentSet.new(Helpers.class_name_to_id(name)) end |
Instance Method Details
#[](value) ⇒ Object?
Return an item from the argument set
91 92 93 |
# File 'lib/apia/argument_set.rb', line 91 def [](value) @source[value.to_sym] end |
#dig(*values) ⇒ Object?
Return an item from this argument set
99 100 101 |
# File 'lib/apia/argument_set.rb', line 99 def dig(*values) @source.dig(*values) end |
#has?(key) ⇒ Boolean
Return whether an argument has been provided or not?
116 117 118 |
# File 'lib/apia/argument_set.rb', line 116 def has?(key) @source.key?(key.to_sym) end |
#to_hash ⇒ Hash
Return the source object
106 107 108 109 110 |
# File 'lib/apia/argument_set.rb', line 106 def to_hash @source.transform_values do |value| value.is_a?(ArgumentSet) ? value.to_hash : value end end |
#validate(argument, index: nil) ⇒ Object
Validate an argument set and return any errors as appropriate
123 124 |
# File 'lib/apia/argument_set.rb', line 123 def validate(argument, index: nil) end |