lib/happymapper/item.rb in nokogiri-happymapper-0.6.0 vs lib/happymapper/item.rb in nokogiri-happymapper-0.7.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + module HappyMapper class Item attr_accessor :name, :type, :tag, :options, :namespace # options: @@ -9,16 +11,16 @@ # default # :parser => Symbol Class method to use for type coercion. # :raw => Boolean Use raw node value (inc. tags) when parsing. # :single => Boolean False if object should be collection, True for single object # :tag => String Element name if it doesn't match the specified name. - def initialize(name, type, o={}) + def initialize(name, type, options = {}) self.name = name.to_s self.type = type - #self.tag = o.delete(:tag) || name.to_s - self.tag = o[:tag] || name.to_s - self.options = { :single => true }.merge(o.merge(:name => self.name)) + # 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 end def constant @@ -29,31 +31,29 @@ # @param [XMLNode] node the xml node that is being parsed # @param [String] namespace the name of the namespace # @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? 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) + process_node_with_default_parser(node, namespaces: xpath_options) end - end def xpath(namespace = self.namespace) xpath = '' xpath += './/' if options[:deep] xpath += "#{namespace}:" if namespace xpath += tag - #puts "xpath: #{xpath}" + # puts "xpath: #{xpath}" xpath end def method_name @method_name ||= name.tr('-', '_') @@ -70,23 +70,22 @@ # def typecast(value) typecaster(value).apply(value) end - private # @return [Boolean] true if the type defined for the item is defined in the # list of support types. def suported_type_registered? - SupportedTypes.types.map {|caster| caster.type }.include?(constant) + SupportedTypes.types.map(&:type).include?(constant) end # @return [#apply] the typecaster object that will be able to convert # the value into a value with the correct type. def typecaster(value) - SupportedTypes.types.find { |caster| caster.apply?(value,constant) } + SupportedTypes.types.find { |caster| caster.apply?(value, constant) } end # # Processes a Nokogiri::XML::Node as a supported type # @@ -111,25 +110,25 @@ def custom_parser_defined? options[:parser] end def process_node_with_custom_parser(node) - if node.respond_to?(:content) && !options[:raw] - value = node.content - else - value = node.to_s - end + value = if node.respond_to?(:content) && !options[:raw] + node.content + else + node.to_s + end begin constant.send(options[:parser].to_sym, value) - rescue + rescue StandardError nil end end - def process_node_with_default_parser(node,parse_options) - constant.parse(node,options.merge(parse_options)) + def process_node_with_default_parser(node, parse_options) + constant.parse(node, options.merge(parse_options)) end # # Convert any String defined types into their constant version so that # the method #parse or the custom defined parser method would be used. @@ -153,8 +152,7 @@ constant.const_missing(name) end end constant end - end end