meh / ruby-tesseract-ocr

A Ruby wrapper library to the tesseract-ocr API.
629 stars 74 forks source link

Fix class collision bug #60

Closed joshleitzel closed 8 years ago

joshleitzel commented 8 years ago

First just want to say thanks for your great library, which has made working with Tesseract much easier for me!

I ran into a small bug because I'm using this gem in a Rails project where I've got models named after document sections, including Paragraph and Line. I was baffled because when I tried to invoke those iterators, engine.paragraphs_for and engine.lines_for, I was seeing ActiveRecord errors coming back, which made no sense.

I traced this to the iterator API, where it falls back on the Element class if the constant (Paragraph or Line) isn't found. In my case, Paragraph and Line were found—it's just that they were my ActiveRecord models.

This is easily fixed by including those classes explicitly. With this simple modification all is well in my Rails project again. However, I think a better long-term solution might be to get rid of the const_get/rescue scheme altogether.

Let me know if you want me to change anything here.

meh commented 8 years ago

I have absolutely no idea why I did it like that, but since I have absolutely no idea I think the best approach would be to check Iterator.constants.include?(level.capitalize) and bail out if it's false.

joshleitzel commented 8 years ago

I actually think just removing the rescue clause and adding Line and Paragraph would be the best solution. Since you control the internal API and only %w(block paragraph line word symbol) get the methods defined, there really shouldn't be any error conditions here.

I'd be happy to make the change if you like. :)

meh commented 8 years ago

Agree, go for it :panda_face:

joshleitzel commented 8 years ago

Done!

meh commented 8 years ago

Merged, thank you for the patience :panda_face: