Sha256: 67a5773e42e52310ccf5d1828cadb0ccf58bbdc6041a70d883708e8dc20acc54

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

* 配置文件中的自定义配置项

  你可以在配置文件 (如默认的 =~/.arql.yaml= / =~/.arql.d/init.yaml= ) 中定义自己的配置项,然后在代码中通过 =Arql::App.config["CONF_KEY"]-= 来获取配置项的值。

  例如,假设系统对 BankAccount 表的 =account_no= 字段进行了加密,你可以在配置文件中定义加密的密钥:

  #+BEGIN_SRC yaml
    dev:
      <<: *default
      host: 127.0.0.1
      port: 3306
      username: test
      password: test123456
      database: devel 
      encrypt_key: "1234567890abcdef"
  #+END_SRC

  然后你可以在 Initialzier 代码 (=~/.arql.rb= / =~/.arql.d/init.rb=) 中读取配置项的值:

  #+BEGIN_SRC ruby
    class BankAccount
    
      def self.encrypt_account_no(account_no)
        cipher = OpenSSL::Cipher.new('AES-128-ECB')
        cipher.encrypt
        cipher.key = Arql::App.config["encrypt_key"]
        encrypted = cipher.update(account_no) + cipher.final
        encrypted.unpack('H*').first
      end
    
      def self.decrypt_account_no(encrypted_account_no)
        cipher = OpenSSL::Cipher.new('AES-128-ECB')
        cipher.decrypt
        cipher.key = Arql::App.config["encrypt_key"]
        decrypted = cipher.update([encrypted_account_no].pack('H*')) + cipher.final
        decrypted
      end
    
    
      # 从数据库查询出数据之后,自动解密 account_no 字段
      after_find do
        self.password = decrypt_account_no(self.password)
      end
    
      # 保存数据之前,自动加密 account_no 字段
      before_save do
        self.password = encrypt_account_no(self.password)
      end
    end
  #+END_SRC

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arql-0.3.31 custom-configurations-zh_CN.org
arql-0.3.30 custom-configurations-zh_CN.org