lib/cache_flow.rb in laserlemon-cache_flow-0.1.1 vs lib/cache_flow.rb in laserlemon-cache_flow-0.1.2
- old
+ new
@@ -28,30 +28,46 @@
end
end
end
module InstanceMethods
- def to_xml_with_cache_flow(options = {})
- with_cache_flow(:xml, options) do
- to_xml_without_cache_flow(options)
+ def to_xml_with_cache_flow(options = {}, &block)
+ if cache_xml?(options, &block)
+ key = xml_cache_key(options.merge!(:cache => false))
+ value = to_xml_without_cache_flow(options, &block)
+ Rails.cache.fetch(key){ value.respond_to?(:dup) ? value.dup : value }
+ else
+ to_xml_without_cache_flow(options, &block)
end
end
- def to_json_with_cache_flow(options = {})
- with_cache_flow(:json, options) do
- to_json_without_cache_flow(options)
+ def to_json_with_cache_flow(options = {}, &block)
+ if cache_json?(options)
+ key = json_cache_key(options.merge!(:cache => false))
+ value = to_json_without_cache_flow(options, &block)
+ Rails.cache.fetch(key){ value.respond_to?(:dup) ? value.dup : value }
+ else
+ to_json_without_cache_flow(options, &block)
end
end
private
- def with_cache_flow(extension, options, &block)
- if options.has_key?(:cache) ? options.delete(:cache) : self.class.cache_serialization
- options.merge!(:cache => false)
- key, value = send("#{extension}_cache_key", options), yield
- Rails.cache.fetch(key){ value.respond_to?(:dup) ? value.dup : value }
- else
- yield
+ def cache_xml?(options, &block)
+ case
+ when block_given?, options.has_key?(:procs) then false
+ else cache_serialization?(options)
+ end
+ end
+
+ def cache_json?(options)
+ cache_serialization?(options)
+ end
+
+ def cache_serialization?(options)
+ case
+ when options.has_key?(:cache) then options.delete(:cache)
+ else self.class.cache_serialization
end
end
def xml_cache_key(options)
level = options.has_key?(:builder) ? options[:builder].instance_eval{ @level } : nil