MyApp/lib/ directory: How to deal with it?

The Guide says about /lib directory:
lib/ Extended modules for your application.

Somewhere I read that I could place my custom email validator
class there. But how to use it then? How to require it?

Now I do this way in my “create” method:

File MyApp/lib/myEmailValidator.rb

class EmailValidator
def self.validate(email) …
end

In my “create” methon

def create
require “myEmailValidator”
@result = EmailValidator.validate(params[:email_from_form])
end

It works, but maybe is there a better way to work with it?

Wins L. wrote in post #1105843:

In my “create” methon

def create
require “myEmailValidator”
@result = EmailValidator.validate(params[:email_from_form])
end

It works, but maybe is there a better way to work with it?

Isn’t it generally accepted practice to put require statements at the
top of the file? It seem odd to me to put them inside a method as you’ve
done here. Maybe I’m just used to treating require like import from
other languages.

You may find this helpful:

http://reefpoints.dockyard.com/ruby/2012/02/14/love-your-lib-directory.html

what i usually do is adding autoload_path.

in config/application.rb, i append

config.autoload_paths += %W(#{Rails.root}/lib/validators)

then i put generic validators such as EmailValidator into lib/validators
directory

after that, all i need is

class SomeModel < ActiveRecord::Base
validates :email, :email => true
end

2013/4/16 Wins L. [email protected]