Class: Apia::Definitions::Field

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

Instance Attribute Summary collapse

Attributes inherited from Apia::Definition

#id, #name, #schema

Instance Method Summary collapse

Methods inherited from Apia::Definition

#schema?, #setup, #validate

Constructor Details

#initialize(name, id: nil) ⇒ Field

Returns a new instance of Field.



22
23
24
25
# File 'lib/apia/definitions/field.rb', line 22

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

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



16
17
18
# File 'lib/apia/definitions/field.rb', line 16

def array
  @array
end

#backendObject

Returns the value of attribute backend.



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

def backend
  @backend
end

#conditionObject

Returns the value of attribute condition.



18
19
20
# File 'lib/apia/definitions/field.rb', line 18

def condition
  @condition
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#includeObject

Returns the value of attribute include.



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

def include
  @include
end

#nullObject

Returns the value of attribute null.



17
18
19
# File 'lib/apia/definitions/field.rb', line 17

def null
  @null
end

#typeClass

Return the type of object

Returns:

  • (Class)


30
31
32
# File 'lib/apia/definitions/field.rb', line 30

def type
  Type.new(@type)
end

Instance Method Details

#array?Boolean

Is the result from this field expected to be an array?

Returns:

  • (Boolean)


44
45
46
# File 'lib/apia/definitions/field.rb', line 44

def array?
  @array == true
end

#dslApia::DSLs::Field

Return a DSL instance for this field

Returns:



62
63
64
# File 'lib/apia/definitions/field.rb', line 62

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

#include?(value, request) ⇒ Boolean

Should this field be inclued for the given value and request

Parameters:

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/apia/definitions/field.rb', line 53

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?

Returns:

  • (Boolean)


37
38
39
# File 'lib/apia/definitions/field.rb', line 37

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.

Parameters:

Returns:



71
72
73
74
75
76
77
# File 'lib/apia/definitions/field.rb', line 71

def raw_value_from_object(object)
  if @backend
    get_value_from_backend(object)
  else
    get_value_directly_from_object(object, @name)
  end
end

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

Return an instance of a Type or a Scalar for this field

Parameters:

Returns:

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/apia/definitions/field.rb', line 83

def value(object, request: nil, path: [])
  raw_value = raw_value_from_object(object)

  return nil if raw_value.nil? && null?
  raise Apia::NullFieldValueError.new(self, object) if raw_value.nil?

  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