class Apia::Definitions::PolymorphOption

Attributes

id[R]
matcher[R]
name[R]

Public Class Methods

new(id, name, type: nil, matcher: nil) click to toggle source
# File lib/apia/definitions/polymorph_option.rb, line 13
def initialize(id, name, type: nil, matcher: nil)
  @id = id
  @name = name
  @type = type
  @matcher = matcher
end

Public Instance Methods

cast(value, request: nil, path: []) click to toggle source
# File lib/apia/definitions/polymorph_option.rb, line 30
def cast(value, request: nil, path: [])
  {
    type: @name.to_s,
    value: type.cast(value, request: request, path: path)
  }
end
matches?(value) click to toggle source
# File lib/apia/definitions/polymorph_option.rb, line 24
def matches?(value)
  return false if @matcher.nil?

  @matcher.call(value) == true
end
type() click to toggle source
# File lib/apia/definitions/polymorph_option.rb, line 20
def type
  Type.new(@type)
end
validate(errors) click to toggle source
# File lib/apia/definitions/polymorph_option.rb, line 37
def validate(errors)
  if @type.nil?
    errors.add self, 'MissingType', "Type for #{name} is missing"
  elsif !type.usable_for_field?
    errors.add self, 'InvalidType', "Type for #{name} must a scalar, polymorph, object or enum "
  end

  unless @matcher.is_a?(Proc)
    errors.add self, 'MissingMatcher', "A matcher must be provided for all options (missing for #{name})"
  end

  true
end