Class: GraphQL::Schema::ImplementationValidator
- Inherits:
-
Object
- Object
- GraphQL::Schema::ImplementationValidator
- Defined in:
- lib/graph_ql/schema/implementation_validator.rb
Overview
A helper to ensure ‘object` implements the concept `as`
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#implementation_as ⇒ Object
readonly
Returns the value of attribute implementation_as.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
-
#initialize(object, as:, errors:) ⇒ ImplementationValidator
constructor
A new instance of ImplementationValidator.
-
#must_respond_to(method_name, args: [], as: nil) ⇒ Object
Ensure the object responds to ‘method_name`.
Constructor Details
#initialize(object, as:, errors:) ⇒ ImplementationValidator
Returns a new instance of ImplementationValidator.
4 5 6 7 8 |
# File 'lib/graph_ql/schema/implementation_validator.rb', line 4 def initialize(object, as:, errors:) @object = object @implementation_as = as @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
3 4 5 |
# File 'lib/graph_ql/schema/implementation_validator.rb', line 3 def errors @errors end |
#implementation_as ⇒ Object (readonly)
Returns the value of attribute implementation_as.
3 4 5 |
# File 'lib/graph_ql/schema/implementation_validator.rb', line 3 def implementation_as @implementation_as end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
3 4 5 |
# File 'lib/graph_ql/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 |
# File 'lib/graph_ql/schema/implementation_validator.rb', line 13 def must_respond_to(method_name, args: [], as: nil) if !object.respond_to?(method_name) local_as = as || implementation_as errors << "#{object.to_s} must respond to ##{method_name}(#{args.join(", ")}) to be a #{local_as}" elsif block_given? yield(object.public_send(method_name)) end end |