Module: Merb::Cache::CacheMixin::ClassMethods

Defined in:
lib/merb-cache/merb_ext/controller.rb

Instance Method Summary collapse

Instance Method Details

#build_request(path, params = {}, env = {}) ⇒ Object



66
67
68
69
70
# File 'lib/merb-cache/merb_ext/controller.rb', line 66

def build_request(path, params = {}, env = {})
  path, params, env = nil, path, params if path.is_a? Hash

  Merb::Cache::CacheRequest.new(path, params, env)
end

#build_url(*args) ⇒ Object



72
73
74
# File 'lib/merb-cache/merb_ext/controller.rb', line 72

def build_url(*args)
  Merb::Router.url(*args)
end

#cache(*actions) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/merb-cache/merb_ext/controller.rb', line 12

def cache(*actions)
  if actions.last.is_a? Hash
    cache_action(*actions)
  else
    actions.each {|a| cache_action(*a)}
  end
end

#cache!(conditions = {}) ⇒ Object



7
8
9
10
# File 'lib/merb-cache/merb_ext/controller.rb', line 7

def cache!(conditions = {})
  before(:_cache_before, conditions.only(:if, :unless).merge(:with => conditions))
  after(:_cache_after, conditions.only(:if, :unless).merge(:with => conditions))
end

#cache_action(action, conditions = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/merb-cache/merb_ext/controller.rb', line 20

def cache_action(action, conditions = {})
  before("_cache_#{action}_before", conditions.only(:if, :unless).merge(:with => [conditions], :only => action))
  after("_cache_#{action}_after", conditions.only(:if, :unless).merge(:with => [conditions], :only => action))
  alias_method "_cache_#{action}_before", :_cache_before
  alias_method "_cache_#{action}_after",  :_cache_after
end

#eager_cache(trigger_action, target = trigger_action, conditions = {}, &blk) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/merb-cache/merb_ext/controller.rb', line 27

def eager_cache(trigger_action, target = trigger_action, conditions = {}, &blk)
  target, conditions = trigger_action, target if target.is_a? Hash

  if target.is_a? Array
    target_controller, target_action = *target
  else
    target_controller, target_action = self, target
  end

  after("_eager_cache_#{trigger_action}_to_#{target_controller.name.snake_case}__#{target_action}_after", conditions.only(:if, :unless).merge(:with => [target_controller, target_action, conditions, blk], :only => trigger_action))
  alias_method "_eager_cache_#{trigger_action}_to_#{target_controller.name.snake_case}__#{target_action}_after", :_eager_cache_after
end

#eager_dispatch(action, params = {}, env = {}, blk = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/merb-cache/merb_ext/controller.rb', line 40

def eager_dispatch(action, params = {}, env = {}, blk = nil)
  kontroller = if blk.nil?
    new(Merb::Request.new(env))
  else
    result = case blk.arity
      when 0  then  blk[]
      when 1  then  blk[params]
      else          blk[*[params, env]]
    end

    case result
    when NilClass         then new(Merb::Request.new(env))
    when Hash, Mash       then new(Merb::Request.new(result))
    when Merb::Request    then new(result)
    when Merb::Controller then result
    else raise ArgumentError, "Block to eager_cache must return nil, the env Hash, a Request object, or a Controller object"
    end
  end

  kontroller.force_cache!

  kontroller._dispatch(action)

  kontroller
end