Class: Apia::Definitions::PolymorphOption

Inherits:
Object
  • Object
show all
Defined in:
lib/apia/definitions/polymorph_option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, type: nil, matcher: nil) ⇒ PolymorphOption

Returns a new instance of PolymorphOption.



13
14
15
16
17
18
# 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/apia/definitions/polymorph_option.rb', line 9

def id
  @id
end

#matcherObject (readonly)

Returns the value of attribute matcher.



11
12
13
# File 'lib/apia/definitions/polymorph_option.rb', line 11

def matcher
  @matcher
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/apia/definitions/polymorph_option.rb', line 10

def name
  @name
end

Instance Method Details

#cast(value, request: nil, path: []) ⇒ Object



30
31
32
33
34
35
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/apia/definitions/polymorph_option.rb', line 24

def matches?(value)
  return false if @matcher.nil?

  @matcher.call(value) == true
end

#typeObject



20
21
22
# File 'lib/apia/definitions/polymorph_option.rb', line 20

def type
  Type.new(@type)
end

#validate(errors) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# 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