lib/moneta/transformer.rb in moneta-0.7.0 vs lib/moneta/transformer.rb in moneta-0.7.1

- old
+ new

@@ -1,10 +1,10 @@ module Moneta # Transforms keys and values (Marshal, YAML, JSON, Base64, MD5, ...). # You can bypass the transformer (e.g. serialization) by using the `:raw` option. # - # @example Add transformer to chain + # @example Add `Moneta::Transformer` to proxy stack # Moneta.build do # transformer :key => [:marshal, :escape], :value => [:marshal] # adapter :File, :dir => 'data' # end # @@ -23,18 +23,17 @@ # Constructor # # @param [Moneta store] adapter The underlying store # @param [Hash] options - # - # Options: - # * :key - List of key transformers in the order in which they should be applied - # * :value - List of value transformers in the order in which they should be applied - # * :prefix - Prefix string for key namespacing (Used by the :prefix key transformer) - # * :secret - HMAC secret to verify values (Used by the :hmac value transformer) - # * :maxlen - Maximum key length (Used by the :truncate key transformer) - # * :quiet - Disable error message + # @return [Transformer] new Moneta transformer + # @option options [Array] :key List of key transformers in the order in which they should be applied + # @option options [Array] :value List of value transformers in the order in which they should be applied + # @option options [String] :prefix Prefix string for key namespacing (Used by the :prefix key transformer) + # @option options [String] :secret HMAC secret to verify values (Used by the :hmac value transformer) + # @option options [Integer] :maxlen Maximum key length (Used by the :truncate key transformer) + # @option options [Boolean] :quiet Disable error message 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) @@ -44,13 +43,16 @@ end private def compile(options, keys, values) - raise ArgumentError, 'Invalid key transformer chain' if KEY_TRANSFORMER !~ keys.map(&:inspect).join - raise ArgumentError, 'Invalid value transformer chain' if VALUE_TRANSFORMER !~ values.map(&:inspect).join + @key_validator ||= compile_validator(KEY_TRANSFORMER) + @value_validator ||= compile_validator(VALUE_TRANSFORMER) + raise ArgumentError, 'Invalid key transformer chain' if @key_validator !~ keys.map(&:inspect).join + raise ArgumentError, 'Invalid value transformer chain' if @value_validator !~ values.map(&:inspect).join + key = compile_transformer(keys, 'key') klass = Class.new(self) klass.class_eval <<-end_eval, __FILE__, __LINE__ def initialize(adapter, options = {}) @@ -138,12 +140,12 @@ transformer.inject(var) do |value, name| raise ArgumentError, "Unknown transformer #{name}" unless t = TRANSFORMER[name] require t[3] if t[3] code = t[i] if t[0] == :serialize && var == 'key' - "(tmp = #{value}; String === tmp ? tmp : #{code.gsub('value', 'tmp')})" + "(tmp = #{value}; String === tmp ? tmp : #{code % 'tmp'})" else - code.gsub('value', value) + code % value end end end def class_name(prefix, keys, values)