Class: Apia::Definitions::Field
- Inherits:
-
Apia::Definition
- Object
- Apia::Definition
- Apia::Definitions::Field
- Defined in:
- lib/apia/definitions/field.rb
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#backend ⇒ Object
Returns the value of attribute backend.
-
#condition ⇒ Object
Returns the value of attribute condition.
-
#description ⇒ Object
Returns the value of attribute description.
-
#include ⇒ Object
Returns the value of attribute include.
-
#null ⇒ Object
Returns the value of attribute null.
-
#skip_if_null ⇒ Object
Returns the value of attribute skip_if_null.
-
#type ⇒ Class
Return the type of object.
Attributes inherited from Apia::Definition
Instance Method Summary collapse
-
#array? ⇒ Boolean
Is the result from this field expected to be an array?.
-
#dsl ⇒ Apia::DSLs::Field
Return a DSL instance for this field.
-
#include?(value, request) ⇒ Boolean
Should this field be inclued for the given value and request.
-
#initialize(name, id: nil) ⇒ Field
constructor
A new instance of Field.
-
#null? ⇒ Boolean
Can the result for thsi field be nil?.
-
#raw_value_from_object(object) ⇒ Object
Return the backend value from the given object based on the rules that exist for this field.
-
#skip_if_null? ⇒ Boolean
Should this field be skipped if the value is null?.
-
#value(object, request: nil, path: []) ⇒ Object
Return an instance of a Type or a Scalar for this field.
Methods inherited from Apia::Definition
Constructor Details
#initialize(name, id: nil) ⇒ Field
Returns a new instance of Field.
23 24 25 26 |
# File 'lib/apia/definitions/field.rb', line 23 def initialize(name, id: nil) @name = name @id = id end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
16 17 18 |
# File 'lib/apia/definitions/field.rb', line 16 def array @array end |
#backend ⇒ Object
Returns the value of attribute backend.
15 16 17 |
# File 'lib/apia/definitions/field.rb', line 15 def backend @backend end |
#condition ⇒ Object
Returns the value of attribute condition.
19 20 21 |
# File 'lib/apia/definitions/field.rb', line 19 def condition @condition end |
#description ⇒ Object
Returns the value of attribute description.
14 15 16 |
# File 'lib/apia/definitions/field.rb', line 14 def description @description end |
#include ⇒ Object
Returns the value of attribute include.
21 22 23 |
# File 'lib/apia/definitions/field.rb', line 21 def include @include end |
#null ⇒ Object
Returns the value of attribute null.
17 18 19 |
# File 'lib/apia/definitions/field.rb', line 17 def null @null end |
#skip_if_null ⇒ Object
Returns the value of attribute skip_if_null.
18 19 20 |
# File 'lib/apia/definitions/field.rb', line 18 def skip_if_null @skip_if_null end |
Instance Method Details
#array? ⇒ Boolean
Is the result from this field expected to be an array?
52 53 54 |
# File 'lib/apia/definitions/field.rb', line 52 def array? @array == true end |
#dsl ⇒ Apia::DSLs::Field
Return a DSL instance for this field
70 71 72 |
# File 'lib/apia/definitions/field.rb', line 70 def dsl @dsl ||= DSLs::Field.new(self) end |
#include?(value, request) ⇒ Boolean
Should this field be inclued for the given value and request
61 62 63 64 65 |
# File 'lib/apia/definitions/field.rb', line 61 def include?(value, request) return true if @condition.nil? @condition.call(value, request) == true end |
#null? ⇒ Boolean
Can the result for thsi field be nil?
38 39 40 |
# File 'lib/apia/definitions/field.rb', line 38 def null? @null == true end |
#raw_value_from_object(object) ⇒ Object
Return the backend value from the given object based on the rules that exist for this field.
79 80 81 82 83 84 85 |
# File 'lib/apia/definitions/field.rb', line 79 def raw_value_from_object(object) if @backend get_value_from_backend(object) else get_value_directly_from_object(object, @name) end end |
#skip_if_null? ⇒ Boolean
Should this field be skipped if the value is null?
45 46 47 |
# File 'lib/apia/definitions/field.rb', line 45 def skip_if_null? @skip_if_null == true end |
#value(object, request: nil, path: []) ⇒ Object
Return an instance of a Type or a Scalar for this field
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/apia/definitions/field.rb', line 91 def value(object, request: nil, path: []) raw_value = raw_value_from_object(object) if raw_value.nil? return :skip if skip_if_null? return nil if null? raise Apia::NullFieldValueError.new(self, object) end if array? && raw_value.is_a?(Array) raw_value.map { |v| type.cast(v, request: request, path: path) } else type.cast(raw_value, request: request, path: path) end end |