forked from jnunemaker/happymapper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Confusion/refactorings
Refactorings
- Loading branch information
Showing
8 changed files
with
86 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters