Module: Chewy::Index::Compiled::ClassMethods

Defined in:
lib/chewy/index/compiled.rb

Instance Method Summary collapse

Instance Method Details

#compiled_compose(object, crutches, context, fields) ⇒ Object



39
40
41
# File 'lib/chewy/index/compiled.rb', line 39

def compiled_compose(object, crutches, context, fields)
  send(compiled_method_name(fields), object, crutches, context)
end

#compiled_compose_available?(fields) ⇒ Boolean

Returns true when this index can use the compiled path for the given compose call. Builds the per-fields method if needed.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/chewy/index/compiled.rb', line 30

def compiled_compose_available?(fields)
  return false if witchcraft?
  return false unless root&.children&.present?

  ensure_compiled_compose_for!(fields)
rescue UNSUPPORTED
  false
end

#compiled_method_name(fields) ⇒ Object



83
84
85
86
87
88
# File 'lib/chewy/index/compiled.rb', line 83

def compiled_method_name(fields)
  key = fields_key(fields)
  return :__chewy_compose__ if key.empty?

  :"__chewy_compose__#{key}"
end

#compiled_procsObject



43
44
45
# File 'lib/chewy/index/compiled.rb', line 43

def compiled_procs
  @compiled_procs ||= []
end

#ensure_compiled_compose_for!(fields) ⇒ Object

Build (and cache) the compiled compose method for the given fields restriction. Returns true on success, false / raises on unsupported. Thread-safe: compilation under a process-wide mutex prevents two threads from interleaving appends into method bodies.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chewy/index/compiled.rb', line 53

def ensure_compiled_compose_for!(fields)
  key = fields_key(fields)
  built = (@compiled_compose_built ||= {})
  return built[key] if built.key?(key)

  COMPILE_MUTEX.synchronize do
    return built[key] if built.key?(key)

    method_name = compiled_method_name(fields)
    source      = Compiler.new(self, fields: fields, method_name: method_name).build
    class_eval(source, __FILE__, __LINE__)
    built[key] = true
  end
rescue UNSUPPORTED
  (@compiled_compose_built ||= {})[fields_key(fields)] = false
  raise
end

#invalidate_compiled_compose!Object

Clears compiled-method cache and rebuilds-on-next-call. Hooked from ‘Mapping.field` so a `field` added after the first compose is picked up rather than absent from a stale generated method. Existing singleton methods remain defined (they shadow until rebuilt) but the cache flag is dropped so the next compose rebuilds and reassigns them.



77
78
79
80
81
# File 'lib/chewy/index/compiled.rb', line 77

def invalidate_compiled_compose!
  @compiled_compose_built = nil
  @compiled_procs = nil
  @compiled_default_ready = false
end