ruby-on-rails – 在rake任务中包含Rails类?

ruby-on-rails – 在rake任务中包含Rails类?,第1张

概述我对Rails很新,所以如果我的问题没有最有意义,我会道歉. 我有一个名为PaymentGatewayCipher的类看起来像: require 'openssl'# Encapsulates payment gateway encryption / decryption utility functionsclass PaymentGatewayCipher class << self 我对Rails很新,所以如果我的问题没有最有意义,我会道歉.

我有一个名为PaymentGatewayCipher的类看起来像:

require 'openssl'# Encapsulates payment gateway encryption / decryption utility functionsclass PaymentGatewayCipher  class << self    def encrypt(file,options = {})      cipher = create_cipher      cipher.encrypt(cipher_key)      data = cipher.update(file.read(file))      data << cipher.final      if to_file = options[:to]        # Write it out to a different file        file.open(to_file,'wb') do |f|          f << data        end      end      data    end    # Decrypts the given file    def decrypt(file)      cipher = create_cipher      cipher.decrypt(cipher_key)      encrypted_data = file.open(file,'rb') {|io| io.read}      data = cipher.update(encrypted_data)      data << cipher.final    end    # Generates the cipher to be used for encryption/decryption    def create_cipher      OpenSSL::Cipher::Cipher.new('aes-256-cbc')    end    # Loads the cipher key used for the symmetric algorithm    def cipher_key      file.open(file.join(Rails.root,'config/mystuff/live/cipher.key'),'rb') {|io| io.read}    end  endend

我想写一个rake任务来运行它来解密文件.我试过把一个文件放在tasks / Rakefile中,看起来像:

directory "tasks"task :decrypt_test do  puts "Decypting"  pay_pal_config = PaymentGatewayCipher.decrypt('hpa1')end

然而,当我运行它时,它说无法找到Class :: Rails

救命?

解决方法 使用lib / tasks文件夹,不要忘记在任务中包含rails环境:

directory "tasks"task :decrypt_test => :environment do  puts "Decypting"  pay_pal_config = PaymentGatewayCipher.decrypt('hpa1')end
总结

以上是内存溢出为你收集整理的ruby-on-rails – 在rake任务中包含Rails类?全部内容,希望文章能够帮你解决ruby-on-rails – 在rake任务中包含Rails类?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1222003.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存