saxaphone 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,14 +9,17 @@ module Saxaphone
9
9
 
10
10
  @element_handlers = {}
11
11
  @stored_attributes = Set.new
12
+ @attribute_aliases = {}
12
13
 
13
14
  class << self
14
15
  attr_accessor :element_handlers
15
16
  attr_accessor :stored_attributes
17
+ attr_accessor :attribute_aliases
16
18
 
17
19
  def inherited(base)
18
- base.element_handlers = element_handlers.dup
19
- base.stored_attributes = stored_attributes.dup
20
+ base.element_handlers = element_handlers.dup
21
+ base.stored_attributes = stored_attributes.dup
22
+ base.attribute_aliases = attribute_aliases.dup
20
23
  end
21
24
 
22
25
  # A block can be passed to <tt>setup</tt>,
@@ -87,10 +90,12 @@ module Saxaphone
87
90
  #
88
91
  # element.attributes # => {"dollars" => "4.33"}
89
92
  def element_attribute(element_name, options = {})
90
- converted_name = options.delete(:as)
93
+ if as = options.delete(:as)
94
+ attribute_aliases[element_name] = as
95
+ end
91
96
 
92
97
  has_element(element_name) do |element|
93
- attributes[converted_name || element.name] = element.content
98
+ assign_attribute_value(element.name, element.content)
94
99
  end
95
100
  end
96
101
 
@@ -158,6 +163,29 @@ module Saxaphone
158
163
  self.stored_attributes += attribute_names.flatten.to_set
159
164
  end
160
165
 
166
+ # Define a white listed attribute that is extracted
167
+ # from the XML element and stored in the attribute hash:
168
+ #
169
+ # WidgetElement < Saxaphone::Element
170
+ # store_attribute 'href', as: 'uri'
171
+ # end
172
+ #
173
+ # element = WidgetElement.parse %{
174
+ # <widget href="http://www.example.com" color="red" price="3.21">
175
+ # ...
176
+ # </widget>
177
+ # }
178
+ #
179
+ # element.attributes # => {"uri" => "http://www.example.com"}
180
+ #
181
+ def store_attribute(attribute_name, options = {})
182
+ if as = options.delete(:as)
183
+ attribute_aliases[attribute_name] = as
184
+ end
185
+
186
+ self.stored_attributes << attribute_name
187
+ end
188
+
161
189
  def handler_for(element_name)
162
190
  element_handlers[element_name] || element_handlers['*']
163
191
  end
@@ -171,7 +199,11 @@ module Saxaphone
171
199
  def initialize(name = '', content = '', attribute_array = [])
172
200
  self.name = name
173
201
  self.content = content
174
- self.attributes = Hash[attribute_array.select { |(key, value)| self.class.stored_attributes.include?(key) }]
202
+ self.attributes = {}
203
+ attribute_array.each do |key, value|
204
+ assign_attribute_value(key, value) if self.class.stored_attributes.include?(key)
205
+ end
206
+ # self.attributes = Hash[attribute_array.select { |(key, value)| self.class.stored_attributes.include?(key) }]
175
207
  setup
176
208
  end
177
209
 
@@ -193,6 +225,10 @@ module Saxaphone
193
225
 
194
226
  end
195
227
 
228
+ def assign_attribute_value(key, value)
229
+ attributes[self.class.attribute_aliases[key] || key] = value
230
+ end
231
+
196
232
  def append_content(string)
197
233
  content << string
198
234
  end
data/test/element_test.rb CHANGED
@@ -16,7 +16,27 @@ class Saxaphone::ElementTest < MiniTest::Spec
16
16
  assert_equal 'hola', element.setup_attr
17
17
  end
18
18
 
19
- def test_initialize_with_attributes
19
+ def test_accessors_inheritence
20
+ parent = Class.new(Saxaphone::Element) do
21
+ has_element 'foo'
22
+ store_attributes 'faz'
23
+ end
24
+
25
+ child = Class.new(parent) do
26
+ has_element 'bar'
27
+ store_attribute 'baz', as: 'moo'
28
+ end
29
+
30
+ assert_equal ['foo'].to_set, parent.element_handlers.keys.to_set
31
+ assert_equal ['faz'].to_set, parent.stored_attributes
32
+ assert_equal({}, parent.attribute_aliases)
33
+
34
+ assert_equal ['foo', 'bar'].to_set, child.element_handlers.keys.to_set
35
+ assert_equal ['faz', 'baz'].to_set, child.stored_attributes
36
+ assert_equal({'baz' => 'moo'}, child.attribute_aliases)
37
+ end
38
+
39
+ def test_store_attributes
20
40
  element = Class.new(Saxaphone::Element) do
21
41
  store_attributes 'a', 'm'
22
42
  end.new('foo', 'bar', [['a', 'b'], ['m', 'n'], ['x', 'y']])
@@ -26,6 +46,14 @@ class Saxaphone::ElementTest < MiniTest::Spec
26
46
  assert_equal({'a' => 'b', 'm' => 'n'}, element.attributes)
27
47
  end
28
48
 
49
+ def test_store_attribute
50
+ element = Class.new(Saxaphone::Element) do
51
+ store_attribute 'm', as: 'n'
52
+ end.new('foo', 'bar', [['a', 'b'], ['m', 'o']])
53
+
54
+ assert_equal({'n' => 'o'}, element.attributes)
55
+ end
56
+
29
57
  def test_element_attributes
30
58
  element = define_element do
31
59
  element_attributes %w(foo bar)
@@ -122,24 +150,6 @@ class Saxaphone::ElementTest < MiniTest::Spec
122
150
  # silence?
123
151
  end
124
152
 
125
- def test_attributes_inherited
126
- parent = Class.new(Saxaphone::Element) do
127
- has_element 'foo'
128
- store_attributes 'faz'
129
- end
130
-
131
- child = Class.new(parent) do
132
- has_element 'bar'
133
- store_attributes 'baz'
134
- end
135
-
136
- assert_equal ['foo'].to_set, parent.element_handlers.keys.to_set
137
- assert_equal ['faz'].to_set, parent.stored_attributes
138
-
139
- assert_equal ['foo', 'bar'].to_set, child.element_handlers.keys.to_set
140
- assert_equal ['faz', 'baz'].to_set, child.stored_attributes
141
- end
142
-
143
153
  private
144
154
  def define_element(&block)
145
155
  Class.new(Saxaphone::Element, &block).new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saxaphone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-23 00:00:00.000000000 Z
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70336418925760 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70336418925760
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  description: Use objects
26
31
  email: developer@matthewhiggins.com
27
32
  executables: []
@@ -59,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
64
  version: 1.3.5
60
65
  requirements: []
61
66
  rubyforge_project:
62
- rubygems_version: 1.8.15
67
+ rubygems_version: 1.8.24
63
68
  signing_key:
64
69
  specification_version: 3
65
70
  summary: Object Oriented SAX Parsing