Class: Apia::Definitions::Argument
- Inherits:
-
Apia::Definition
- Object
- Apia::Definition
- Apia::Definitions::Argument
- Defined in:
- lib/apia/definitions/argument.rb
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#default ⇒ Object
Returns the value of attribute default.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#required ⇒ Object
Returns the value of attribute required.
-
#type ⇒ Class
Return the type of object (either a ArgumentSet or a Scalar) which this argument represents.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Attributes inherited from Apia::Definition
#description, #id, #name, #schema
Instance Method Summary collapse
-
#array? ⇒ Boolean
Is this an array?.
- #dsl ⇒ Object
-
#initialize(name, id: nil) ⇒ Argument
constructor
A new instance of Argument.
-
#required? ⇒ Boolean
Is this argument required?.
- #validate(errors) ⇒ Object
-
#validate_value(value) ⇒ Array
Validate a given value through all validations and return an array of all errors.
Methods inherited from Apia::Definition
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
#array ⇒ Object
Returns the value of attribute array.
14 15 16 |
# File 'lib/apia/definitions/argument.rb', line 14 def array @array end |
#default ⇒ Object
Returns the value of attribute default.
15 16 17 |
# File 'lib/apia/definitions/argument.rb', line 15 def default @default end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/apia/definitions/argument.rb', line 11 def @options end |
#required ⇒ Object
Returns the value of attribute required.
13 14 15 |
# File 'lib/apia/definitions/argument.rb', line 13 def required @required end |
#type ⇒ Class
Return the type of object (either a ArgumentSet or a Scalar) which this argument represents.
46 47 48 |
# File 'lib/apia/definitions/argument.rb', line 46 def type Type.new(@type) end |
#validations ⇒ Object (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?
60 61 62 |
# File 'lib/apia/definitions/argument.rb', line 60 def array? @array == true end |
#dsl ⇒ Object
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?
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
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 |