Class: GraphQL::Schema::ImplementationValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/implementation_validator.rb

Overview

A helper to ensure ‘object` implements the concept `as`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, as:, errors:) ⇒ ImplementationValidator

Returns a new instance of ImplementationValidator.



4
5
6
7
8
# File 'lib/graphql/schema/implementation_validator.rb', line 4

def initialize(object, as:, errors:)
  @object = object
  @implementation_as = as
  @errors = errors
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/graphql/schema/implementation_validator.rb', line 3

def errors
  @errors
end

#implementation_asObject (readonly)

Returns the value of attribute implementation_as.



3
4
5
# File 'lib/graphql/schema/implementation_validator.rb', line 3

def implementation_as
  @implementation_as
end

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/graphql/schema/implementation_validator.rb', line 3

def object
  @object
end

Instance Method Details

#must_respond_to(method_name, args: [], as: nil) ⇒ Object

Ensure the object responds to ‘method_name`. If `block_given?`, yield the return value of that method If provided, use `as` in the error message, overriding class-level `as`.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql/schema/implementation_validator.rb', line 13

def must_respond_to(method_name, args: [], as: nil)
  local_as = as || implementation_as
  method_signature = "##{method_name}(#{args.join(", ")})"
  if !object.respond_to?(method_name)
    errors << "#{object.to_s} must respond to #{method_signature} to be a #{local_as}"
  elsif block_given?
    return_value = object.public_send(method_name)
    if return_value.nil?
      errors << "#{object.to_s} must return a value for #{method_signature} to be a #{local_as}"
    else
      yield(return_value)
    end
  end
end