kind_dom 0.9.7 → 0.9.8

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 (2) hide show
  1. data/lib/kind_dom/base.rb +40 -19
  2. metadata +2 -2
data/lib/kind_dom/base.rb CHANGED
@@ -33,11 +33,14 @@ module KindDom
33
33
  class Base
34
34
 
35
35
  attr_reader :dom
36
+ attr_accessor :find_cache
36
37
 
37
38
  # A new KindDom object may be created from raw XML [string] data, or
38
39
  # an already instantiated KindDom::Base, XML::Node or XML::Document.
39
40
  #
40
- def initialize(xml_in=nil)
41
+ # Caches intermediate found values, before yielding to the optional block.
42
+ # Disable the cache with opt `:find_cache => false` or set #find_cache to false
43
+ def initialize(xml_in=nil, opts={})
41
44
  unless xml_in.nil?
42
45
  case
43
46
  when xml_in.kind_of?(KindDom::Base)
@@ -50,6 +53,7 @@ module KindDom
50
53
  @dom = parser.parse
51
54
  end
52
55
  end
56
+ @find_cache = {} if opts[:find_cache].nil? ? true : opts[:find_cache]
53
57
  rescue XML::Parser::ParseError
54
58
  end
55
59
 
@@ -62,17 +66,19 @@ module KindDom
62
66
  # (or default) before returning.
63
67
  #
64
68
  def content_for(xpath='.', default=nil) # :yields: found_content
65
- node = case @dom.class.to_s
66
- when 'XML::Document' then
67
- @dom.root.find_first(xpath)
68
- else # 'XML::Node'
69
- @dom.find_first(xpath)
70
- end
71
- content = case node.class.to_s
72
- when 'XML::Attr' then
73
- node.value
74
- else
75
- node.content
69
+ content = cache "content_for(#{xpath})" do
70
+ node = case @dom.class.to_s
71
+ when 'XML::Document' then
72
+ @dom.root.find_first(xpath)
73
+ else # 'XML::Node'
74
+ @dom.find_first(xpath)
75
+ end
76
+ case node.class.to_s
77
+ when 'XML::Attr' then
78
+ node.value
79
+ else
80
+ node.content
81
+ end
76
82
  end
77
83
  rescue NoMethodError
78
84
  ensure
@@ -92,7 +98,9 @@ module KindDom
92
98
  # (or default) before returning.
93
99
  #
94
100
  def collection_of(xpath, default=nil) # :yields: found_collection
95
- c = @dom.find(xpath).collect {|n| self.class.new(n) }
101
+ c = cache "collection_of(#{xpath})" do
102
+ @dom.find(xpath).collect {|n| self.class.new(n) }
103
+ end
96
104
  rescue NoMethodError
97
105
  ensure
98
106
  c = c.blank?||c.size<1 ? default : c
@@ -111,11 +119,13 @@ module KindDom
111
119
  # (or default) before returning.
112
120
  #
113
121
  def first_of(xpath, default=nil) # :yields: found_node
114
- n = case @dom.class.to_s
115
- when 'XML::Document' then
116
- @dom.root.find_first(xpath)
117
- else # 'XML::Node'
118
- @dom.find_first(xpath)
122
+ n = cache "first_of(#{xpath})" do
123
+ case @dom.class.to_s
124
+ when 'XML::Document' then
125
+ @dom.root.find_first(xpath)
126
+ else # 'XML::Node'
127
+ @dom.find_first(xpath)
128
+ end
119
129
  end
120
130
  rescue NoMethodError
121
131
  ensure
@@ -126,6 +136,17 @@ module KindDom
126
136
  return n
127
137
  end
128
138
  end
129
-
139
+
140
+ private
141
+
142
+ def cache(key)
143
+ if @find_cache and @find_cache.has_key?(key)
144
+ x = @find_cache[key]
145
+ else
146
+ x = yield
147
+ @find_cache[key] = x if @find_cache
148
+ end
149
+ return x
150
+ end
130
151
  end
131
152
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kind_dom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mars Hall
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-05 00:00:00 -07:00
12
+ date: 2008-08-20 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency