Ruby on rails 包括或扩展gem中的类/模块(例如Desive)
我已经编写了一个小模块lib/encryption/encryption.rbRuby on rails 包括或扩展gem中的类/模块(例如Desive),ruby-on-rails,module,devise,Ruby On Rails,Module,Devise,我已经编写了一个小模块lib/encryption/encryption.rb module Encryption def self.encrypt(value) ... end def self.decrypt(value) ... end end 我想在Desive的这两个文件中使用/访问此模块,即: token\u authenticable.rb 可验证的.rb 我已经通过创建2个新文件并将它们放入/config/initilaizers(复制其中的原
module Encryption
def self.encrypt(value)
...
end
def self.decrypt(value)
...
end
end
我想在Desive的这两个文件中使用/访问此模块,即:
require 'devise/strategies/token_authenticatable'
require './lib/encryption/encryption.rb' #TRIED THIS, BUT DOES NOT WORK
module Devise
module Models
# The TokenAuthenticatable module is responsible for generating an authentication token and
# validating the authenticity of the same while signing in.
...
我的修改工作正常,但如何访问这些文件中的lib/Encryption.rb模块?
这种修改方法是最佳实践吗?
如果没有,正确的方法是什么?在一个类中包装两个方法,例如MyEncryptionAlgo。创建该类的对象
obj = Encryption::MyEncryptionAlgo.new
使用此对象访问这两种方法
obj.encrypt(value)
obj.decrypt(value)
如果您的application.rb中有此项:
config.autoload_paths += %W(#{config.root}/lib)
然后将自动加载“/lib”。意思是你可以打电话
require 'encryption/encryption'
它应该可以工作。谢谢您的帮助,但是我如何在/config/initializers/*.rb文件中包含这个类的模块呢?(见编辑后的问题)谢谢,这很有效!我已经用
config.autoload_path+=Dir[“{config.root}/lib/**/”]将/lib添加到config.autoload_路径中。