0

In My lib folder, I have a file image_helper_exception.rb. The path is lib/dibs_exception/image_exception/image_helper_exception.rb

In this file I have all my Image Exceptions defined

module DibsException
  module ImageException
     class ImageHelperException < Exception; end
     class InvalidEntityException < Exception; end
     class InvalidImageTypeException < Exception; end
     class InvalidImageVersionException < Exception; end
     class InvalidImageUrlException < Exception; end
     class ImageNotFoundException < Exception; end
  end
end

I raise applications elsewhere in my application when certain things are missing. The path to the file image_resize.rb where it is being used is helpers/image_resize.rb

The image_resize.rb looks something like this.

module Helpers
  class ImageResize
    require 'RMagick'
    require 'aws/s3'
    require 'rvg/rvg'
    require 'mechanize'
    include Magick

    def initialize(abc)
       if abc
         raise DibsException::ImageException::InvalidEntityException.new("Entity is invalid.")
         #The above line raises a error 
         #NameError: uninitialized constant DibsException::ImageException::InvalidEntityException
       else
         raise DibsException::ImageException::ImageHelperException.new("ImageHelperException.")
         #The above line does not raise a error 
        end
    end

end

I would like to add lib gets loaded in application.rb.

I am not able to understand why(Helpers::ImageResize.new(true)) is raising this error : NameError: uninitialized constant DibsException::ImageException::InvalidEntityException

But this works fine as expected : Helpers::ImageResize.new(false)

Why is only the first class loaded. Can some one help ? require does not work.

Egalitarian
  • 2,168
  • 7
  • 24
  • 33
  • Ruby does not support method overloading -> http://stackoverflow.com/questions/9373104/why-does-ruby-not-support-method-overloading – usha Nov 15 '13 at 20:18
  • I removed it. Overloading is not the issue here. Accessing the exception is. – Egalitarian Nov 15 '13 at 20:24

1 Answers1

1

Ruby does not support function overloading. second initialize overwrite the first one while parsing. soo only the second function exists.

This should do the magic.

def initialize(xyz=nil)
       raise DibsException::ImageException::InvalidEntityException.new("Entity is invalid.") if(xyz.blank?)
       raise DibsException::ImageException::ImageHelperException.new("ImageHelperException.")
       #This line DOES NOT raise error like above. It works fine.
end
HariHaraSudhan
  • 1,330
  • 3
  • 15
  • 24
  • lib folder is autoloaded, so you need to create classes based on file name. for if you ask for ImageHelperException, it looks for file named, image_helper_exception.rb, same way it might look fir invalid_entity_exception.rb when you call InvalidEntityException. – HariHaraSudhan Nov 15 '13 at 20:33
  • Yes, that is a solution. I solved it using load('lib/dibs_exception/image_exception/image_helper_exception.rb'). Do you have some alternative ? – Egalitarian Nov 15 '13 at 20:47