Module: Chewy::Type::Observe::Helpers

Included in:
Chewy::Type::Observe, Sequel::Plugins::ChewyObserve
Defined in:
lib/chewy/type/observe.rb

Instance Method Summary collapse

Instance Method Details

#extract_callback_options!(args) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/chewy/type/observe.rb', line 32

def extract_callback_options!(args)
  options = args.extract_options!
  options.each_key.with_object({}) { |key, hash|
    hash[key] = options.delete(key) if [:if, :unless].include?(key)
  }.tap {
    args.push(options) unless options.empty?
  }
end

#update_proc(type_name, *args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chewy/type/observe.rb', line 7

def update_proc(type_name, *args, &block)
  options = args.extract_options!
  method = args.first

  Proc.new do
    backreference = if method && method.to_s == 'self'
      self
    elsif method
      send(method)
    else
      instance_eval(&block)
    end

    reference = if type_name.is_a?(Proc)
      type_name.arity == 0 ?
        instance_exec(&type_name) :
        type_name.call(self)
    else
      type_name
    end

    Chewy.derive_type(reference).update_index(backreference, options)
  end
end