Class: Apia::ArgumentSet

Inherits:
Object
  • Object
show all
Extended by:
Defineable
Defined in:
lib/apia/argument_set.rb

Direct Known Subclasses

LookupArgumentSet

Defined Under Namespace

Classes: MissingValue

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • hash (Hash)
  • path (Array) (defaults to: [])


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/apia/argument_set.rb', line 65

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.

Parameters:



39
40
41
42
43
# File 'lib/apia/argument_set.rb', line 39

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

Parameters:

Returns:



49
50
51
52
53
54
55
# File 'lib/apia/argument_set.rb', line 49

def create_from_request(request)
  json_body = request.json_body || {}
  params = request.params || {}
  merged_params = DeepMerge.merge(params, json_body)

  new(merged_params, request: request)
end

.definitionApia::Definitions::ArgumentSet

Return the definition for this argument set



30
31
32
# File 'lib/apia/argument_set.rb', line 30

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

Parameters:

  • value (String, Symbol)

Returns:



96
97
98
# File 'lib/apia/argument_set.rb', line 96

def [](value)
  @source[value.to_sym]
end

#dig(*values) ⇒ Object?

Return an item from this argument set

Parameters:

  • values (Array<String, Symbol>)

Returns:



104
105
106
# File 'lib/apia/argument_set.rb', line 104

def dig(*values)
  @source.dig(*values)
end

#empty?Boolean

Return whether the argument set has no arguments within?

Returns:

  • (Boolean)


128
129
130
# File 'lib/apia/argument_set.rb', line 128

def empty?
  @source.empty?
end

#has?(key) ⇒ Boolean

Return whether an argument has been provided or not?

Parameters:

  • name (Symbol)

Returns:

  • (Boolean)


121
122
123
# File 'lib/apia/argument_set.rb', line 121

def has?(key)
  @source.key?(key.to_sym)
end

#to_hashHash

Return the source object

Returns:

  • (Hash)


111
112
113
114
115
# File 'lib/apia/argument_set.rb', line 111

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

Parameters:

  • argument (Apia::Argument)


135
136
# File 'lib/apia/argument_set.rb', line 135

def validate(argument, index: nil)
end