lib/cache_flow.rb in laserlemon-cache_flow-0.1.2 vs lib/cache_flow.rb in laserlemon-cache_flow-0.1.3
- old
+ new
@@ -30,30 +30,39 @@
end
module InstanceMethods
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 }
+ serializer = ActiveRecord::XmlSerializer.new(self, options)
+ builder = serializer.builder
+ key = xml_cache_key(serializer.options)
+ fetch = Rails.cache.fetch(key) do
+ target = builder.target!
+ builder.instance_eval{ @target = '' }
+ value = block_given? ? serializer.to_s(&block) : serializer.to_s
+ builder.instance_eval{ @target = target }
+ value
+ end
+ builder.instance_eval{ @target << fetch }
else
to_xml_without_cache_flow(options, &block)
end
end
- def to_json_with_cache_flow(options = {}, &block)
+ def to_json_with_cache_flow(options = {})
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 }
+ key = json_cache_key(options)
+ Rails.cache.fetch(key) do
+ value = to_json_without_cache_flow(options)
+ value.duplicable? ? value.dup : value
+ end
else
- to_json_without_cache_flow(options, &block)
+ to_json_without_cache_flow(options)
end
end
private
-
def cache_xml?(options, &block)
case
when block_given?, options.has_key?(:procs) then false
else cache_serialization?(options)
end
@@ -64,23 +73,23 @@
end
def cache_serialization?(options)
case
when options.has_key?(:cache) then options.delete(:cache)
- else self.class.cache_serialization
+ else cache_serialization
end
end
def xml_cache_key(options)
level = options.has_key?(:builder) ? options[:builder].instance_eval{ @level } : nil
- serialized_cache_key(:xml, options.except(:builder).merge(:level => level))
+ serialization_cache_key(:xml, options.except(:builder).merge(:level => level))
end
def json_cache_key(options)
- serialized_cache_key(:json, options)
+ serialization_cache_key(:json, options.merge(:include_root_in_json => include_root_in_json))
end
- def serialized_cache_key(extension, options)
+ def serialization_cache_key(extension, options)
query = options.except(:cache).reject{|k,v| v.nil? }.to_query
["#{cache_key}.#{extension}", query].delete_if(&:blank?).join('?')
end
end
end