如何在rake任务中引入Rails类以实现Ruby on Rails功能?

2026-04-11 17:261阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计323个文字,预计阅读时间需要2分钟。

如何在rake任务中引入Rails类以实现Ruby on Rails功能?

我对Rails比较陌生,所以如果我的问题没有最直接的意义,我会道歉。我有一个名为PaymentGatewayCipher的类,看起来是这样的:

rubyrequire 'openssl'

我对Rails很新,所以如果我的问题没有最有意义,我会道歉.

如何在rake任务中引入Rails类以实现Ruby on Rails功能?

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

require 'openssl' # Encapsulates payment gateway encryption / decryption utility functions class 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 end end

我想写一个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

本文共计323个文字,预计阅读时间需要2分钟。

如何在rake任务中引入Rails类以实现Ruby on Rails功能?

我对Rails比较陌生,所以如果我的问题没有最直接的意义,我会道歉。我有一个名为PaymentGatewayCipher的类,看起来是这样的:

rubyrequire 'openssl'

我对Rails很新,所以如果我的问题没有最有意义,我会道歉.

如何在rake任务中引入Rails类以实现Ruby on Rails功能?

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

require 'openssl' # Encapsulates payment gateway encryption / decryption utility functions class 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 end end

我想写一个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