Class: Elastic::Support::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic/support/transform.rb

Instance Method Summary collapse

Constructor Details

#initialize(_transform) ⇒ Transform

Returns a new instance of Transform.



3
4
5
# File 'lib/elastic/support/transform.rb', line 3

def initialize(_transform)
  @transform = _transform
end

Instance Method Details

#apply(_value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/elastic/support/transform.rb', line 7

def apply(_value)
  case @transform
  when Symbol
    _value.public_send @transform
  when String
    _value.instance_eval(@transform)
  when Proc
    _value.instance_exec(&@transform)
  when nil
    _value
  else
    raise ArgumentError, "invalid transformation type #{@transform.class}"
  end
end

#apply_to_many(_values) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elastic/support/transform.rb', line 22

def apply_to_many(_values)
  case @transform
  when Symbol
    _values.map(&@transform)
  when String
    _values.map { |v| v.instance_eval(@transform) }
  when Proc
    _values.map { |v| v.instance_exec(&@transform) }
  when nil
    _values
  else
    raise ArgumentError, "invalid transformation type #{@transform.class}"
  end
end