Module: ActiveModelCachers::Hook::Depdenencies

Defined in:
lib/active_model_cachers/hook/dependencies.rb

Instance Method Summary collapse

Instance Method Details

#load_hooksObject



15
16
17
# File 'lib/active_model_cachers/hook/dependencies.rb', line 15

def load_hooks
  @load_hooks ||= Hash.new{|h, k| h[k] = [] }
end

#new_constants_inObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_model_cachers/hook/dependencies.rb', line 19

def new_constants_in(*)
  new_constants = super.each do |const_name|
    hooks = load_hooks[const_name]
    need_compact = false
    hooks.each_with_index do |hook, idx|
      if (hook[:times] -= 1) < 0
        hooks[idx] = nil
        need_compact = true
        next
      end
      const_name.constantize.instance_exec(&hook[:block])
    end
    hooks.compact! if need_compact
  end
  return new_constants
end

#onload(const_name, times: 1, &block) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/active_model_cachers/hook/dependencies.rb', line 6

def onload(const_name, times: 1, &block)
  const = const_name if not const_name.is_a?(String)
  if const or Module.const_defined?(const_name)
    (const || const_name.constantize).instance_exec(&block)
  else
    load_hooks[const_name].push(block: block, times: times)
  end
end