soulcutter / saxerator

A SAX-based XML parser for parsing large files into manageable chunks
MIT License
128 stars 19 forks source link

can't convert Saxerator::Builder::StringElement to Array #62

Open voke opened 6 years ago

voke commented 6 years ago

According to the README:

You can treat objects consistently as arrays using Ruby's built-in array conversion method in the form Array(element_or_array)

Snippet:


require 'saxerator'

p RUBY_VERSION # => "2.3.1"
p Saxerator::VERSION # => "0.9.8"

xml = <<-XML
<root>
  <product>
    <items>
      <item>foo</item>
      <item>bar</item>
    </items>
  </product>
  <product>
    <items>
      <item>lonely</item>
    </items>
  </product>
</root>
XML

parser = Saxerator.parser(xml)

parser.for_tag("product").each do |node|
  p Array(node['items']['item'])
end

gives

`Array': can't convert Saxerator::Builder::StringElement to Array (Saxerator::Builder::StringElement#to_a gives Saxerator::Builder::ArrayElement) (TypeError)
fanantoxa commented 6 years ago

@soulcutter I've checked is really a bug. Could you check when find a time please?

voke commented 6 years ago

I think the problem is this commit which changed the behaviour.

# string_element.rb
def to_a
  ArrayElement.new(self, name)
end

should probably be

def to_a
  ArrayElement.new([self], name)
end
fanantoxa commented 6 years ago

@voke Already tested it and it's doesn't work. I thing Array() expect and exactly Array object.

soulcutter commented 6 years ago

I think this behavior changed when we stopped extending core ruby classes, using delegation instead.