lib/happymapper/item.rb in nokogiri-happymapper-0.9.0 vs lib/happymapper/item.rb in nokogiri-happymapper-0.10.0
- old
+ new
@@ -18,11 +18,11 @@
self.type = type
# self.tag = options.delete(:tag) || name.to_s
self.tag = options[:tag] || name.to_s
self.options = { single: true }.merge(options.merge(name: self.name))
- @xml_type = self.class.to_s.split('::').last.downcase
+ @xml_type = self.class.to_s.split("::").last.downcase
end
def constant
@constant ||= constantize(type)
end
@@ -33,32 +33,32 @@
# @param [Hash] xpath_options additional xpath options
#
def from_xml_node(node, namespace, xpath_options)
namespace = options[:namespace] if options.key?(:namespace)
- if suported_type_registered?
+ if custom_parser_defined?
+ find(node, namespace, xpath_options) { |n| process_node_with_custom_parser(n) }
+ elsif suported_type_registered?
find(node, namespace, xpath_options) { |n| process_node_as_supported_type(n) }
elsif constant == XmlContent
find(node, namespace, xpath_options) { |n| process_node_as_xml_content(n) }
- elsif custom_parser_defined?
- find(node, namespace, xpath_options) { |n| process_node_with_custom_parser(n) }
else
process_node_with_default_parser(node, namespaces: xpath_options)
end
end
def xpath(namespace = self.namespace)
- xpath = ''
- xpath += './/' if options[:deep]
+ xpath = ""
+ xpath += ".//" if options[:deep]
xpath += "#{namespace}:" if namespace
xpath += tag
# puts "xpath: #{xpath}"
xpath
end
def method_name
- @method_name ||= name.tr('-', '_')
+ @method_name ||= name.tr("-", "_")
end
#
# Convert the value into the correct type.
#
@@ -116,17 +116,23 @@
node.content
else
node.to_s
end
- begin
- constant.send(options[:parser].to_sym, value)
- rescue StandardError
- nil
- end
+ custom_parser = create_custom_parser(options[:parser])
+
+ custom_parser.call(value)
end
+ def create_custom_parser(parser)
+ return parser if parser.respond_to?(:call)
+
+ proc { |value|
+ constant.send(parser.to_sym, value)
+ }
+ end
+
def process_node_with_default_parser(node, parse_options)
constant.parse(node, options.merge(parse_options))
end
#
@@ -140,10 +146,10 @@
def constantize(type)
type.is_a?(String) ? convert_string_to_constant(type) : type
end
def convert_string_to_constant(type)
- names = type.split('::')
+ names = type.split("::")
constant = Object
names.each do |name|
constant =
if constant.const_defined?(name)
constant.const_get(name)