Module: BreezyTemplate::CacheExtension
- Included in:
- BreezyTemplate
- Defined in:
- lib/breezy_template/cache_extension.rb
Instance Method Summary collapse
- #_breezy_set_cache(key, value) ⇒ Object
- #_cache(key = nil, options = {}) ⇒ Object
- #_cache_key(key, options = {}) ⇒ Object
- #_cache_options(options) ⇒ Object
- #_cache_options?(options) ⇒ Boolean
- #_extended_options?(value) ⇒ Boolean
- #_fragment_name_with_digest(key, options) ⇒ Object
- #_mapping_element(element, opts = {}) ⇒ Object
- #_normalize_with_cache_options(options) ⇒ Object
- #_result(value, *args) ⇒ Object
- #array!(collection = [], *attributes) ⇒ Object
- #set!(key, value = BLANK, *args) ⇒ Object
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, ={}) 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, ) @js = [] blank_or_value = begin ::Rails.cache.fetch(key, ) 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, ={}) key = _fragment_name_with_digest(key, ) key = url_for(key).split('://', 2).last if ::Hash === key key = ::ActiveSupport::Cache.(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 () return nil if ! || .blank? = [*[:cache]] key, = return [key, ] end |
#_cache_options?(options) ⇒ Boolean
79 80 81 |
# File 'lib/breezy_template/cache_extension.rb', line 79 def () !![:cache] end |
#_extended_options?(value) ⇒ Boolean
91 92 93 |
# File 'lib/breezy_template/cache_extension.rb', line 91 def (value) (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, ) if && [:_partial] partial = [:_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, ) 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 (opts) key, = (opts) if ::Proc === key key = key.call(element) end _cache(key, ) { _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 () return unless .key? :cache = .dup [:cache] = [*[:cache]] if [:cache].size == 1 [:cache].push({}) end 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) = (args[0]) if _cache(*) { 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 = attributes.first.dup = () if [:partial] && [:cache] partial_name = [*[:partial]][0] = [:cache][1] [:_partial] = partial_name end if ::Kernel.block_given? super(collection, , ::Proc.new) else super(collection, ) 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) = args.first || {} = () if [:partial] && [:cache] partial_name = [*[:partial]][0] = [:cache][1] [:_partial] = partial_name end if ::Kernel.block_given? super(key, value, , ::Proc.new) else super(key, value, ) end end |