Class: Moneta::Transformer

Inherits:
Proxy
  • Object
show all
Defined in:
lib/moneta/transformer.rb,
lib/moneta/transformer/config.rb,
lib/moneta/transformer/helper.rb,
lib/moneta/transformer/helper/bson.rb

Overview

Transforms keys and values (Marshal, YAML, JSON, Base64, MD5, …). You can bypass the transformer (e.g. serialization) by using the `:raw` option.

Examples:

Add `Moneta::Transformer` to proxy stack

Moneta.build do
  transformer key: [:marshal, :escape], value: [:marshal]
  adapter :File, dir: 'data'
end

Bypass serialization

store.store('key', 'value', raw: true)
store['key'] # raises an Exception
store.load('key', raw: true) # returns 'value'

store['key'] = 'value'
store.load('key', raw: true) # returns "\x04\bI\"\nvalue\x06:\x06ET"

Defined Under Namespace

Modules: Helper

Constant Summary collapse

TRANSFORMER =

Available key/value transformers

{
  # Name:   [ Type,       Load,                           Dump,                              Library        Test      ],
  bencode:  [ :serialize, '::BEncode.load(%s)',           '::BEncode.dump(%s)',              'bencode'      ],
  bert:     [ :serialize, '::BERT.decode(%s)',            '::BERT.encode(%s)',               'bert'         ],
  bson:     [ :serialize, 'Helper::BSON.load(%s)',        'Helper::BSON.dump(%s)',           'bson'         ],
  json:     [ :serialize, '::MultiJson.load(%s)',         '::MultiJson.dump(%s)',            'multi_json'   ],
  marshal:  [ :serialize, '::Marshal.load(%s)',           '::Marshal.dump(%s)',              nil            ],
  msgpack:  [ :serialize, '::MessagePack.unpack(%s)',     '::MessagePack.pack(%s)',          'msgpack'      ],
  ox:       [ :serialize, '::Ox.parse_obj(%s)',           '::Ox.dump(%s)',                   'ox'           ],
  php:      [ :serialize, '::PHP.unserialize(%s)',        '::PHP.serialize(%s)',             'php_serialize'],
  tnet:     [ :serialize, '::TNetstring.parse(%s).first', '::TNetstring.dump(%s)',           'tnetstring'   ],
  yaml:     [ :serialize, '::YAML.load(%s)',              '::YAML.dump(%s)',                 'yaml'         ],
  bzip2:    [ :compress,  'Helper.bunzip2(%s)',           'Helper.bzip2(%s)',                'rbzip2'       ],
  lz4:      [ :compress,  '::LZ4.uncompress(%s)',         '::LZ4.compress(%s)',              'lz4-ruby'     ],
  lzma:     [ :compress,  '::LZMA.decompress(%s)',        '::LZMA.compress(%s)',             'lzma'         ],
  lzo:      [ :compress,  '::LZO.decompress(%s)',         '::LZO.compress(%s)',              'lzoruby'      ],
  snappy:   [ :compress,  '::Snappy.inflate(%s)',         '::Snappy.deflate(%s)',            'snappy'       ],
  quicklz:  [ :compress,  '::QuickLZ.decompress(%s)',     '::QuickLZ.compress(%s)',          'qlzruby'      ],
  zlib:     [ :compress,  '::Zlib::Inflate.inflate(%s)',  '::Zlib::Deflate.deflate(%s)',     'zlib'         ],
  base64:   [ :encode,    "%s.unpack('m0').first",        "[%s].pack('m0')"                                 ],
  urlsafe_base64: [
    :encode,
    'Base64.urlsafe_decode64(%s)',
    'Base64.urlsafe_encode64(%s)',
    'base64'
  ],
  escape:   [ :encode,    'Helper.unescape(%s)',          'Helper.escape(%s)'                               ],
  hex:      [ :encode,    "[%s].pack('H*')",              "%s.unpack('H*').first"                           ],
  qp:       [ :encode,    "%s.unpack('M').first",         "[%s].pack('M')"                                  ],
  uuencode: [ :encode,    "%s.unpack('u').first",         "[%s].pack('u')"                                  ],
  hmac:     [
    :hmac,
    'Helper.hmacverify(%s, options[:secret] || @secret)',
    'Helper.hmacsign(%s, options[:secret] || @secret)',
    'openssl'
  ],
  prefix:   [
    :prefix,
    "%s.sub(@prefix, '')",
    '(options[:prefix] || @prefix)+%s',
    nil,
    "%s.start_with?(@prefix)"
  ],
  truncate: [ :truncate,  nil,                            'Helper.truncate(%s, @maxlen)',    'digest/md5'   ],
  md5:      [ :digest,    nil,                            '::Digest::MD5.hexdigest(%s)',     'digest/md5'   ],
  rmd160:   [ :digest,    nil,                            '::Digest::RMD160.hexdigest(%s)',  'digest/rmd160'],
  sha1:     [ :digest,    nil,                            '::Digest::SHA1.hexdigest(%s)',    'digest/sha1'  ],
  sha256:   [ :digest,    nil,                            '::Digest::SHA256.hexdigest(%s)',  'digest/sha2'  ],
  sha384:   [ :digest,    nil,                            '::Digest::SHA384.hexdigest(%s)',  'digest/sha2'  ],
  sha512:   [ :digest,    nil,                            '::Digest::SHA512.hexdigest(%s)',  'digest/sha2'  ],
  city32:   [ :digest,    nil,                            '::CityHash.hash32(%s).to_s(16)',  'cityhash'     ],
  city64:   [ :digest,    nil,                            '::CityHash.hash64(%s).to_s(16)',  'cityhash'     ],
  city128:  [ :digest,    nil,                            '::CityHash.hash128(%s).to_s(16)', 'cityhash'     ],
  spread:   [ :spread,    nil,                            'Helper.spread(%s)'                               ],
  to_s:     [ :string,    nil,                            '%s.to_s'                                         ],
  inspect:  [ :string,    nil,                            '%s.inspect'                                      ]
}.freeze
VALUE_TRANSFORMER =

Allowed value transformers (Read it like a regular expression!)

'serialize? compress? hmac? encode?'.freeze
KEY_TRANSFORMER =

Allowed key transformers (Read it like a regular expression!)

'(serialize | string)? prefix? ((encode? truncate?) | (digest spread?))?'.freeze
LOAD_KEY_TRANSFORMER =

Key transformers that can be “loaded” (e.g. reversed) and can be used by the key enumeration feature

'serialize? prefix? encode?'.freeze
TEST_KEY_TRANSFORMER =

Key transformers that can be “tested for success” with a dumped key and can be used by the key enumeration feature

'serialize? prefix? encode?'.freeze

Instance Attribute Summary

Attributes inherited from Proxy

#adapter

Class Method Summary collapse

Methods inherited from Proxy

#clear, #close, #create, #delete, #each_key, #features, features_mask, #fetch_values, #increment, #initialize, #key?, #load, #merge!, not_supports, #slice, #store, #values_at

Methods included from Defaults

#[], #[]=, #close, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #increment, #key?, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

This class inherits a constructor from Moneta::Proxy

Class Method Details

.new(adapter, options = {}) ⇒ Transformer

Returns new Moneta transformer.

Parameters:

  • adapter (Moneta store)

    The underlying store

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :key (Array)

    List of key transformers in the order in which they should be applied

  • :value (Array)

    List of value transformers in the order in which they should be applied

  • :prefix (String)

    Prefix string for key namespacing (Used by the :prefix key transformer)

  • :secret (String)

    HMAC secret to verify values (Used by the :hmac value transformer)

  • :maxlen (Integer)

    Maximum key length (Used by the :truncate key transformer)

Returns:

Raises:

  • (ArgumentError)

32
33
34
35
36
37
38
39
40
# File 'lib/moneta/transformer.rb', line 32

def new(adapter, options = {})
  keys = [options[:key]].flatten.compact
  values = [options[:value]].flatten.compact
  raise ArgumentError, 'Option :key or :value is required' if keys.empty? && values.empty?
  options[:prefix] ||= '' if keys.include?(:prefix)
  name = class_name(keys, values)
  const_set(name, compile(keys, values)) unless const_defined?(name)
  const_get(name).original_new(adapter, options)
end

.original_newObject


22
# File 'lib/moneta/transformer.rb', line 22

alias original_new new