class NRB::BeerXML::Parser
Attributes
builder[R]
perform_validations[R]
reader[R]
Public Class Methods
new(builder: Builder.new, reader: Nokogiri::XML, perform_validations: true)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 28 def initialize(builder: Builder.new, reader: Nokogiri::XML, perform_validations: true) @perform_validations = perform_validations @builder = builder @reader = reader end
Public Instance Methods
parse(entry)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 35 def parse(entry) doc = case entry when IO, StringIO parse_xml entry when String parse_path(file: entry) else raise ArgumentError "Don't know how to parse a #{entry.class}" end parse_node doc.root end
Private Instance Methods
assign_attribute(object, node)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 49 def assign_attribute(object, node) return unless object return unless attribute_node?(node) meth = guess_attribute_assignment_method node if object.respond_to? meth object.send meth, attribute_value(node) end end
assign_child_to_parent(parent, child)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 59 def assign_child_to_parent(parent, child) meth = guess_child_assignment_method parent, child parent.respond_to?(meth) && parent.send(meth, child) end
attribute_node?(node)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 65 def attribute_node?(node) node.children.size == 1 && node.children.first.text? end
attribute_value(node)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 70 def attribute_value(node) value = node.children.first.text.strip return value.to_i if value =~ /^-?[0-9]+$/ return value.to_f if value =~ /^-?[0-9.]+$/ return false if value == "FALSE" return true if value == "TRUE" value end
guess_attribute_assignment_method(node)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 80 def guess_attribute_assignment_method(node) node.name.downcase + '=' end
guess_child_assignment_method(parent, child)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 85 def guess_child_assignment_method(parent, child) # Smelly, needs refactor return :<< if parent.is_a? RecordSet name = if child.is_a? RecordSet "#{child.record_type.to_s}s" else child.class.name.split(/::/).last end underscore(name) + '=' end
guess_class_name(string)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 97 def guess_class_name(string) camelize string.downcase end
parse_node(node,parent=nil)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 102 def parse_node(node,parent=nil) return unless node if attribute_node?(node) assign_attribute parent, node else obj = builder.build guess_class_name(node.name) node.children.each do |child| parse_node child, obj end assign_child_to_parent parent, obj validate obj if perform_validations obj end end
parse_path(file: nil)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 125 def parse_path(file: nil) f = File.open(file) doc = parse_xml(f) f.close doc end
parse_xml(stream)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 133 def parse_xml(stream) reader.parse(stream) do |config| config.noblanks end end
validate(obj)
click to toggle source
# File lib/nrb/beerxml/parser.rb, line 140 def validate(obj) return unless obj.respond_to?(:valid?) return if obj.valid? raise InvalidRecordError.new(obj, obj.errors.messages) end