Class: AgnosticBackend::Indexable::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/agnostic_backend/indexable/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, type, from: nil, **options) ⇒ Field

Returns a new instance of Field.



8
9
10
11
12
13
14
15
# File 'lib/agnostic_backend/indexable/field.rb', line 8

def initialize(value, type, from: nil, **options)
  if type == FieldType::STRUCT && from.nil?
    raise "A nested type requires the specification of a target class using the `from` argument"
  end
  @value = value.respond_to?(:call) ? value : value.to_sym
  @from = (from.is_a?(Enumerable) ? from : [from]) unless from.nil?
  @type = FieldType.new(type, **options)
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



6
7
8
# File 'lib/agnostic_backend/indexable/field.rb', line 6

def from
  @from
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/agnostic_backend/indexable/field.rb', line 6

def type
  @type
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/agnostic_backend/indexable/field.rb', line 6

def value
  @value
end

Instance Method Details

#evaluate(context:) ⇒ Object



17
18
19
20
21
# File 'lib/agnostic_backend/indexable/field.rb', line 17

def evaluate(context:)
  value.respond_to?(:call) ?
    context.instance_eval(&value) :
    context.send(value)
end