Permalink
Browse files

Support inheritence. Issue #1

  • Loading branch information...
1 parent ec70165 commit e8fd901511cddb3d4e0b7825e98d83e26bcee991 @matthuhiggins committed Mar 23, 2012
Showing with 32 additions and 11 deletions.
  1. +1 −0 lib/saxaphone/document.rb
  2. +12 −10 lib/saxaphone/element.rb
  3. +19 −1 test/element_test.rb
@@ -26,6 +26,7 @@ def start_element(name, attributes = [])
def end_element(name)
ending_element = element_stack.pop
if element_stack.empty?
+ p "root"
@grantr
grantr Mar 23, 2012

This line sucks!

self.root_element = ending_element
else
element_stack.last.add_element(ending_element)
@@ -7,7 +7,18 @@ class Element
class ElementHandler < Struct.new(:class_name, :proc)
end
+ @element_handlers = {}
+ @stored_attributes = Set.new
+
class << self
+ attr_accessor :element_handlers
+ attr_accessor :stored_attributes
+
+ def inherited(base)
+ base.element_handlers = element_handlers.dup
+ base.stored_attributes = stored_attributes.dup
+ end
+
# A block can be passed to <tt>setup</tt>,
# which is called after the element is initialized.
#
@@ -144,25 +155,16 @@ def has_element(element_name, class_name = @@base_class_name, &block)
# Notice that the "price" attribute is not stored.
#
def store_attributes(*attribute_names)
- @stored_attributes = attribute_names.flatten.to_set
+ self.stored_attributes += attribute_names.flatten.to_set
end
def handler_for(element_name)
element_handlers[element_name] || element_handlers['*']
end
- def stored_attributes
- @stored_attributes ||= Set.new
- end
-
def parse(xml)
Saxaphone::Document.parse(xml, self)
end
-
- private
- def element_handlers
- @element_handlers ||= {}
- end
end
attr_accessor :name, :content, :attributes
View
@@ -121,7 +121,25 @@ def test_has_element_without_block
element.add_element(child_element)
# silence?
end
-
+
+ def test_attributes_inherited
+ parent = Class.new(Saxaphone::Element) do
+ has_element 'foo'
+ store_attributes 'faz'
+ end
+
+ child = Class.new(parent) do
+ has_element 'bar'
+ store_attributes 'baz'
+ end
+
+ assert_equal ['foo'].to_set, parent.element_handlers.keys.to_set
+ assert_equal ['faz'].to_set, parent.stored_attributes
+
+ assert_equal ['foo', 'bar'].to_set, child.element_handlers.keys.to_set
+ assert_equal ['faz', 'baz'].to_set, child.stored_attributes
+ end
+
private
def define_element(&block)
Class.new(Saxaphone::Element, &block).new

0 comments on commit e8fd901

Please sign in to comment.