Class: Chewy::Index::Crutch::Crutches

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/index/crutch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, collection, context = {}) ⇒ Crutches

Returns a new instance of Crutches.



14
15
16
17
18
19
# File 'lib/chewy/index/crutch.rb', line 14

def initialize(index, collection, context = {})
  @index = index
  @collection = collection
  @context = context
  @crutches_instances = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



21
22
23
24
25
# File 'lib/chewy/index/crutch.rb', line 21

def method_missing(name, *, **)
  return self[name] if @index._crutches.key?(name)

  super
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/chewy/index/crutch.rb', line 12

def context
  @context
end

Instance Method Details

#[](name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/chewy/index/crutch.rb', line 31

def [](name)
  @crutches_instances[name] ||= begin
    block = @index._crutches[:"#{name}"]
    if block.arity > 1 || block.arity < -1
      block.call(@collection, @context)
    else
      block.call(@collection)
    end
  end
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/chewy/index/crutch.rb', line 27

def respond_to_missing?(name, include_private = false)
  @index._crutches.key?(name) || super
end