Skip to content

Commit

Permalink
Merge pull request #25 from Confusion/refactorings
Browse files Browse the repository at this point in the history
Refactorings
  • Loading branch information
dam5s committed Dec 27, 2012
2 parents df40b76 + 2f3ad8f commit 56878f0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 127 deletions.
14 changes: 6 additions & 8 deletions lib/happymapper.rb
Expand Up @@ -10,8 +10,8 @@ module HappyMapper
DEFAULT_NS = "happymapper" DEFAULT_NS = "happymapper"


def self.included(base) def self.included(base)
base.instance_variable_set("@attributes", {}) base.instance_variable_set("@attributes", [])
base.instance_variable_set("@elements", {}) base.instance_variable_set("@elements", [])
base.instance_variable_set("@registered_namespaces", {}) base.instance_variable_set("@registered_namespaces", {})
base.instance_variable_set("@wrapper_anonymous_classes", {}) base.instance_variable_set("@wrapper_anonymous_classes", {})


Expand All @@ -37,8 +37,7 @@ module ClassMethods
# #
def attribute(name, type, options={}) def attribute(name, type, options={})
attribute = Attribute.new(name, type, options) attribute = Attribute.new(name, type, options)
@attributes[to_s] ||= [] @attributes << attribute
@attributes[to_s] << attribute
attr_accessor attribute.method_name.intern attr_accessor attribute.method_name.intern
end end


Expand All @@ -49,7 +48,7 @@ def attribute(name, type, options={})
# an empty array is returned when there have been no attributes defined. # an empty array is returned when there have been no attributes defined.
# #
def attributes def attributes
@attributes[to_s] || [] @attributes
end end


# #
Expand Down Expand Up @@ -93,8 +92,7 @@ def register_namespace(namespace, ns)
# #
def element(name, type, options={}) def element(name, type, options={})
element = Element.new(name, type, options) element = Element.new(name, type, options)
@elements[to_s] ||= [] @elements << element
@elements[to_s] << element
attr_accessor element.method_name.intern attr_accessor element.method_name.intern
end end


Expand All @@ -106,7 +104,7 @@ def element(name, type, options={})
# defined. # defined.
# #
def elements def elements
@elements[to_s] || [] @elements
end end


# #
Expand Down
22 changes: 20 additions & 2 deletions lib/happymapper/attribute.rb
@@ -1,3 +1,21 @@
module HappyMapper module HappyMapper
class Attribute < Item; end class Attribute < Item
end 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
52 changes: 51 additions & 1 deletion lib/happymapper/element.rb
@@ -1,3 +1,53 @@
module HappyMapper 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 end
81 changes: 0 additions & 81 deletions lib/happymapper/item.rb
Expand Up @@ -93,18 +93,6 @@ def primitive?
Types.include?(constant) Types.include?(constant)
end end


def element?
@xml_type == 'element'
end

def attribute?
@xml_type == 'attribute'
end

def text_node?
@xml_type == 'textnode'
end

def method_name def method_name
@method_name ||= name.tr('-', '_') @method_name ||= name.tr('-', '_')
end end
Expand Down Expand Up @@ -177,75 +165,6 @@ def constantize(type)
type type
end end
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 private methods
end end
end end
7 changes: 6 additions & 1 deletion lib/happymapper/text_node.rb
@@ -1,3 +1,8 @@
module HappyMapper 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 end
13 changes: 3 additions & 10 deletions spec/happymapper_attribute_spec.rb
Expand Up @@ -5,17 +5,10 @@
before do before do
@attr = HappyMapper::Attribute.new(:foo, String) @attr = HappyMapper::Attribute.new(:foo, String)
end 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 it 'should accept :default as an option' do
@attr.text_node?.should be_false attr = described_class.new(:foo, String, :default => 'foobar')
attr.default.should == 'foobar'
end end
end end
end end
12 changes: 0 additions & 12 deletions spec/happymapper_element_spec.rb
Expand Up @@ -5,17 +5,5 @@
before do before do
@attr = HappyMapper::Element.new(:foo, String) @attr = HappyMapper::Element.new(:foo, String)
end 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
end end
12 changes: 0 additions & 12 deletions spec/happymapper_text_node_spec.rb
Expand Up @@ -5,17 +5,5 @@
before do before do
@attr = HappyMapper::TextNode.new(:foo, String) @attr = HappyMapper::TextNode.new(:foo, String)
end 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
end end

0 comments on commit 56878f0

Please sign in to comment.