soulcutter/saxerator

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

voke opened this issue · 4 comments

voke commented

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)

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

voke commented

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

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

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