Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #25 from Confusion/refactorings

Refactorings
  • Loading branch information...
commit 56878f0944372053762108b110977dca91df2409 2 parents df40b76 + 2f3ad8f
@dam5s authored
View
14 lib/happymapper.rb
@@ -10,8 +10,8 @@ module HappyMapper
DEFAULT_NS = "happymapper"
def self.included(base)
- base.instance_variable_set("@attributes", {})
- base.instance_variable_set("@elements", {})
+ base.instance_variable_set("@attributes", [])
+ base.instance_variable_set("@elements", [])
base.instance_variable_set("@registered_namespaces", {})
base.instance_variable_set("@wrapper_anonymous_classes", {})
@@ -37,8 +37,7 @@ module ClassMethods
#
def attribute(name, type, options={})
attribute = Attribute.new(name, type, options)
- @attributes[to_s] ||= []
- @attributes[to_s] << attribute
+ @attributes << attribute
attr_accessor attribute.method_name.intern
end
@@ -49,7 +48,7 @@ def attribute(name, type, options={})
# an empty array is returned when there have been no attributes defined.
#
def attributes
- @attributes[to_s] || []
+ @attributes
end
#
@@ -93,8 +92,7 @@ def register_namespace(namespace, ns)
#
def element(name, type, options={})
element = Element.new(name, type, options)
- @elements[to_s] ||= []
- @elements[to_s] << element
+ @elements << element
attr_accessor element.method_name.intern
end
@@ -106,7 +104,7 @@ def element(name, type, options={})
# defined.
#
def elements
- @elements[to_s] || []
+ @elements
end
#
View
22 lib/happymapper/attribute.rb
@@ -1,3 +1,21 @@
module HappyMapper
- class Attribute < Item; end
-end
+ class Attribute < Item
+ attr_accessor :default
+
+ # @see Item#initialize
+ # Additional options:
+ # :default => Object The default value for this
+ def initialize(name, type, o={})
+ super
+ self.default = o[:default]
+ end
+
+ def find(node, namespace, xpath_options)
+ if options[:xpath]
+ yield(node.xpath(options[:xpath],xpath_options))
+ else
+ yield(node[tag])
+ end
+ end
+ end
+end
View
52 lib/happymapper/element.rb
@@ -1,3 +1,53 @@
module HappyMapper
- class Element < Item; end
+ class Element < Item
+
+ def find(node, namespace, xpath_options)
+ if self.namespace && xpath_options["xmlns:#{self.namespace}"]
+ # from the class definition
+ namespace = self.namespace
+ elsif options[:namespace] && xpath_options["xmlns:#{options[:namespace]}"]
+ namespace = options[:namespace]
+ end
+
+ if options[:single]
+ if options[:xpath]
+ result = node.xpath(options[:xpath], xpath_options)
+ else
+ result = node.xpath(xpath(namespace), xpath_options)
+ end
+
+ if result
+ value = yield(result.first)
+ handle_attributes_option(result, value, xpath_options)
+ value
+ end
+ else
+ target_path = options[:xpath] ? options[:xpath] : xpath(namespace)
+ node.xpath(target_path, xpath_options).collect do |result|
+ value = yield(result)
+ handle_attributes_option(result, value, xpath_options)
+ value
+ end
+ end
+ end
+
+ def handle_attributes_option(result, value, xpath_options)
+ if options[:attributes].is_a?(Hash)
+ result = result.first unless result.respond_to?(:attribute_nodes)
+
+ result.attribute_nodes.each do |xml_attribute|
+ if attribute_options = options[:attributes][xml_attribute.name.to_sym]
+ attribute_value = Attribute.new(xml_attribute.name.to_sym, *attribute_options).from_xml_node(result, namespace, xpath_options)
+
+ result.instance_eval <<-EOV
+ def value.#{xml_attribute.name}
+ #{attribute_value.inspect}
+ end
+ EOV
+ end # if attributes_options
+ end # attribute_nodes.each
+ end # if options[:attributes]
+ end # def handle...
+
+ end
end
View
81 lib/happymapper/item.rb
@@ -93,18 +93,6 @@ def primitive?
Types.include?(constant)
end
- def element?
- @xml_type == 'element'
- end
-
- def attribute?
- @xml_type == 'attribute'
- end
-
- def text_node?
- @xml_type == 'textnode'
- end
-
def method_name
@method_name ||= name.tr('-', '_')
end
@@ -177,75 +165,6 @@ def constantize(type)
type
end
end
-
-
- def find(node, namespace, xpath_options, &block)
- if self.namespace && xpath_options["xmlns:#{self.namespace}"]
- # from the class definition
- namespace = self.namespace
- elsif options[:namespace] && xpath_options["xmlns:#{options[:namespace]}"]
- namespace = options[:namespace]
- end
-
- if element?
- if options[:single]
-
- result = nil
-
- if options[:xpath]
- result = node.xpath(options[:xpath], xpath_options)
- else
- result = node.xpath(xpath(namespace), xpath_options)
- end
-
- if result
- value = options[:single] ? yield(result.first) : result.map {|r| yield r }
- handle_attributes_option(result, value, xpath_options)
-
- value
- end
- else
-
- target_path = options[:xpath] ? options[:xpath] : xpath(namespace)
-
- results = node.xpath(target_path, xpath_options).collect do |result|
- value = yield(result)
- handle_attributes_option(result, value, xpath_options)
- value
- end
- results
- end
- elsif attribute?
-
- if options[:xpath]
- yield(node.xpath(options[:xpath],xpath_options))
- else
- yield(node[tag])
- end
-
- else # text node
- yield(node.children.detect{|c| c.text?})
- end
- end
-
- def handle_attributes_option(result, value, xpath_options)
- if options[:attributes].is_a?(Hash)
- result = result.first unless result.respond_to?(:attribute_nodes)
-
- result.attribute_nodes.each do |xml_attribute|
- if attribute_options = options[:attributes][xml_attribute.name.to_sym]
- attribute_value = Attribute.new(xml_attribute.name.to_sym, *attribute_options).from_xml_node(result, namespace, xpath_options)
-
- result.instance_eval <<-EOV
- def value.#{xml_attribute.name}
- #{attribute_value.inspect}
- end
- EOV
- end # if attributes_options
- end # attribute_nodes.each
- end # if options[:attributes]
- end # def handle...
-
# end private methods
end
end
View
7 lib/happymapper/text_node.rb
@@ -1,3 +1,8 @@
module HappyMapper
- class TextNode < Item; end
+ class TextNode < Item
+
+ def find(node, namespace, xpath_options)
+ yield(node.children.detect{|c| c.text?})
+ end
+ end
end
View
13 spec/happymapper_attribute_spec.rb
@@ -5,17 +5,10 @@
before do
@attr = HappyMapper::Attribute.new(:foo, String)
end
-
- it 'should know that it is an attribute' do
- @attr.attribute?.should be_true
- end
-
- it 'should know that it is NOT an element' do
- @attr.element?.should be_false
- end
- it 'should know that it is NOT a text node' do
- @attr.text_node?.should be_false
+ it 'should accept :default as an option' do
+ attr = described_class.new(:foo, String, :default => 'foobar')
+ attr.default.should == 'foobar'
end
end
end
View
12 spec/happymapper_element_spec.rb
@@ -5,17 +5,5 @@
before do
@attr = HappyMapper::Element.new(:foo, String)
end
-
- it 'should know that it is an element' do
- @attr.element?.should be_true
- end
-
- it 'should know that it is NOT an attribute' do
- @attr.attribute?.should be_false
- end
-
- it 'should know that it is NOT a text node' do
- @attr.text_node?.should be_false
- end
end
end
View
12 spec/happymapper_text_node_spec.rb
@@ -5,17 +5,5 @@
before do
@attr = HappyMapper::TextNode.new(:foo, String)
end
-
- it 'should know that it is a text node' do
- @attr.text_node?.should be_true
- end
-
- it 'should know that it is NOT an element' do
- @attr.element?.should be_false
- end
-
- it 'should know that it is NOT an attribute' do
- @attr.attribute?.should be_false
- end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.