Module: BreezyTemplate::CacheExtension

Included in:
BreezyTemplate
Defined in:
lib/breezy_template/cache_extension.rb

Instance Method Summary collapse

Instance Method Details

#_breezy_set_cache(key, value) ⇒ Object



95
96
97
# File 'lib/breezy_template/cache_extension.rb', line 95

def _breezy_set_cache(key, value)
  "cache[\"#{key}\"]=#{_dump(value)};"
end

#_cache(key = nil, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/breezy_template/cache_extension.rb', line 109

def _cache(key=nil, options={})
  perform_caching = @context.respond_to?(:controller) && @context.controller.perform_caching

  if !perform_caching || key.nil?
    return yield self
  end

  parent_js = @js
  key = _cache_key(key, options)
  @js = []

  blank_or_value = begin
    ::Rails.cache.fetch(key, options) do
      result = yield self
      if result != _blank
        @js << _breezy_set_cache(key, result)
        @js.join
      else
        _blank
      end
    end
  ensure
    @js = parent_js
  end

  if blank_or_value == _blank
    _blank
  else
    v = blank_or_value
    @js.push(v)
    ::BreezyTemplate::Var.new("cache[\"#{key}\"]")
  end
end

#_cache_key(key, options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/breezy_template/cache_extension.rb', line 99

def _cache_key(key, options={})
  key = _fragment_name_with_digest(key, options)
  key = url_for(key).split('://', 2).last if ::Hash === key
  key = ::ActiveSupport::Cache.expand_cache_key(key, :breezy)

  ::Digest::MD5.hexdigest(key.to_s).tap do |digest|
    _logger.try :debug, "Cache key :#{key} was digested to #{digest}"
  end
end

#_cache_options(options) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/breezy_template/cache_extension.rb', line 83

def _cache_options(options)
  return nil if !options || options.blank?
  options = [*options[:cache]]
  key, options = options

  return [key, options]
end

#_cache_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/breezy_template/cache_extension.rb', line 79

def _cache_options?(options)
  !!options[:cache]
end

#_extended_options?(value) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/breezy_template/cache_extension.rb', line 91

def _extended_options?(value)
  _cache_options?(value) || super
end

#_fragment_name_with_digest(key, options) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/breezy_template/cache_extension.rb', line 143

def _fragment_name_with_digest(key, options)
  if options && options[:_partial]
    partial = options[:_partial]
    [key, _partial_digest(partial)]
  else
    if @context.respond_to?(:cache_fragment_name)
      # Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
      # should be used instead.
      @context.cache_fragment_name(key, options)
    elsif @context.respond_to?(:fragment_name_with_digest)
      # Backwards compatibility for period of time when fragment_name_with_digest was made public.
      @context.fragment_name_with_digest(key)
    else
      key
    end
  end
end

#_mapping_element(element, opts = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/breezy_template/cache_extension.rb', line 64

def _mapping_element(element, opts={})
  if _cache_options?(opts)
    key, options = _cache_options(opts)
    if ::Proc === key
      key = key.call(element)
    end

    _cache(key, options) {
      _scope { yield element }
    }
  else
    super
  end
end

#_normalize_with_cache_options(options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/breezy_template/cache_extension.rb', line 14

def _normalize_with_cache_options(options)
  return options unless options.key? :cache

  options = options.dup
  options[:cache] = [*options[:cache]]

  if options[:cache].size == 1
    options[:cache].push({})
  end

  options
end

#_result(value, *args) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/breezy_template/cache_extension.rb', line 5

def _result(value, *args)
  options = _cache_options(args[0])
  if options
    _cache(*options) { super }
  else
    super
  end
end

#array!(collection = [], *attributes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/breezy_template/cache_extension.rb', line 44

def array!(collection=[], *attributes)
  has_option = attributes.first.is_a? ::Hash
  return super if !has_option

  options = attributes.first.dup
  options = _normalize_with_cache_options(options)

  if options[:partial] && options[:cache]
    partial_name = [*options[:partial]][0]
    cache_options = options[:cache][1]
    cache_options[:_partial] = partial_name
  end

  if ::Kernel.block_given?
    super(collection, options, ::Proc.new)
  else
    super(collection, options)
  end
end

#set!(key, value = BLANK, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/breezy_template/cache_extension.rb', line 27

def set!(key, value = BLANK, *args)
  options = args.first || {}
  options = _normalize_with_cache_options(options)

  if options[:partial] && options[:cache]
    partial_name = [*options[:partial]][0]
    cache_options = options[:cache][1]
    cache_options[:_partial] = partial_name
  end

  if ::Kernel.block_given?
    super(key, value, options, ::Proc.new)
  else
    super(key, value, options)
  end
end