Class: Apia::Definitions::Argument

Inherits:
Apia::Definition show all
Defined in:
lib/apia/definitions/argument.rb

Instance Attribute Summary collapse

Attributes inherited from Apia::Definition

#description, #id, #name, #schema

Instance Method Summary collapse

Methods inherited from Apia::Definition

#schema?, #setup

Constructor Details

#initialize(name, id: nil) ⇒ Argument

Returns a new instance of Argument.



18
19
20
21
22
# File 'lib/apia/definitions/argument.rb', line 18

def initialize(name, id: nil)
  @id = id
  @name = name
  @validations = []
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



14
15
16
# File 'lib/apia/definitions/argument.rb', line 14

def array
  @array
end

#defaultObject

Returns the value of attribute default.



15
16
17
# File 'lib/apia/definitions/argument.rb', line 15

def default
  @default
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#requiredObject

Returns the value of attribute required.



13
14
15
# File 'lib/apia/definitions/argument.rb', line 13

def required
  @required
end

#typeClass

Return the type of object (either a ArgumentSet or a Scalar) which this argument represents.

Returns:

  • (Class)


46
47
48
# File 'lib/apia/definitions/argument.rb', line 46

def type
  Type.new(@type)
end

#validationsObject (readonly)

Returns the value of attribute validations.



12
13
14
# File 'lib/apia/definitions/argument.rb', line 12

def validations
  @validations
end

Instance Method Details

#array?Boolean

Is this an array?

Returns:

  • (Boolean)


60
61
62
# File 'lib/apia/definitions/argument.rb', line 60

def array?
  @array == true
end

#dslObject



24
25
26
# File 'lib/apia/definitions/argument.rb', line 24

def dsl
  @dsl ||= DSLs::Argument.new(self)
end

#required?Boolean

Is this argument required?

Returns:

  • (Boolean)


53
54
55
# File 'lib/apia/definitions/argument.rb', line 53

def required?
  @required == true
end

#validate(errors) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/apia/definitions/argument.rb', line 28

def validate(errors)
  if @name.nil?
    errors.add self, 'MissingName', 'Arguments must have a name'
  elsif @name.to_s !~ /\A[a-z0-9\-_]+\z/i
    errors.add self, 'InvalidName', 'Argument name must only include letters, numbers, hyphens and underscores'
  end

  if @type.nil?
    errors.add self, 'MissingType', 'Arguments must have a type'
  elsif !type.usable_for_argument?
    errors.add self, 'InvalidType', 'Type must be an argument set, scalar or enum'
  end
end

#validate_value(value) ⇒ Array

Validate a given value through all validations and return an array of all errors

Parameters:

Returns:

  • (Array)


69
70
71
72
73
# File 'lib/apia/definitions/argument.rb', line 69

def validate_value(value)
  @validations.each_with_object([]) do |validation, errors|
    errors << validation[:name] unless validation[:block].call(value)
  end
end