laserlemon-cache_flow 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/cache_flow.gemspec +2 -2
  3. data/lib/cache_flow.rb +22 -13
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/cache_flow.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cache_flow}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["laserlemon"]
12
- s.date = %q{2009-08-29}
12
+ s.date = %q{2009-08-30}
13
13
  s.description = %q{Easily cache ActiveRecord's to_xml and to_json outputs}
14
14
  s.email = %q{steve@laserlemon.com}
15
15
  s.extra_rdoc_files = [
data/lib/cache_flow.rb CHANGED
@@ -32,26 +32,35 @@ module LaserLemon
32
32
  module InstanceMethods
33
33
  def to_xml_with_cache_flow(options = {}, &block)
34
34
  if cache_xml?(options, &block)
35
- key = xml_cache_key(options.merge!(:cache => false))
36
- value = to_xml_without_cache_flow(options, &block)
37
- Rails.cache.fetch(key){ value.respond_to?(:dup) ? value.dup : value }
35
+ serializer = ActiveRecord::XmlSerializer.new(self, options)
36
+ builder = serializer.builder
37
+ key = xml_cache_key(serializer.options)
38
+ fetch = Rails.cache.fetch(key) do
39
+ target = builder.target!
40
+ builder.instance_eval{ @target = '' }
41
+ value = block_given? ? serializer.to_s(&block) : serializer.to_s
42
+ builder.instance_eval{ @target = target }
43
+ value
44
+ end
45
+ builder.instance_eval{ @target << fetch }
38
46
  else
39
47
  to_xml_without_cache_flow(options, &block)
40
48
  end
41
49
  end
42
50
 
43
- def to_json_with_cache_flow(options = {}, &block)
51
+ def to_json_with_cache_flow(options = {})
44
52
  if cache_json?(options)
45
- key = json_cache_key(options.merge!(:cache => false))
46
- value = to_json_without_cache_flow(options, &block)
47
- Rails.cache.fetch(key){ value.respond_to?(:dup) ? value.dup : value }
53
+ key = json_cache_key(options)
54
+ Rails.cache.fetch(key) do
55
+ value = to_json_without_cache_flow(options)
56
+ value.duplicable? ? value.dup : value
57
+ end
48
58
  else
49
- to_json_without_cache_flow(options, &block)
59
+ to_json_without_cache_flow(options)
50
60
  end
51
61
  end
52
62
 
53
63
  private
54
-
55
64
  def cache_xml?(options, &block)
56
65
  case
57
66
  when block_given?, options.has_key?(:procs) then false
@@ -66,20 +75,20 @@ module LaserLemon
66
75
  def cache_serialization?(options)
67
76
  case
68
77
  when options.has_key?(:cache) then options.delete(:cache)
69
- else self.class.cache_serialization
78
+ else cache_serialization
70
79
  end
71
80
  end
72
81
 
73
82
  def xml_cache_key(options)
74
83
  level = options.has_key?(:builder) ? options[:builder].instance_eval{ @level } : nil
75
- serialized_cache_key(:xml, options.except(:builder).merge(:level => level))
84
+ serialization_cache_key(:xml, options.except(:builder).merge(:level => level))
76
85
  end
77
86
 
78
87
  def json_cache_key(options)
79
- serialized_cache_key(:json, options)
88
+ serialization_cache_key(:json, options.merge(:include_root_in_json => include_root_in_json))
80
89
  end
81
90
 
82
- def serialized_cache_key(extension, options)
91
+ def serialization_cache_key(extension, options)
83
92
  query = options.except(:cache).reject{|k,v| v.nil? }.to_query
84
93
  ["#{cache_key}.#{extension}", query].delete_if(&:blank?).join('?')
85
94
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: laserlemon-cache_flow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - laserlemon
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-29 00:00:00 -07:00
12
+ date: 2009-08-30 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency