Sha256: d4a988f592541bee09ff77eb417ff24530cc1523be28959f47bc73b0b8965fa2

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require 'active_support/core_ext/string'
if ActiveSupport::VERSION::STRING.match(/3\.\d+\.\d+/)
  require 'sparrow/core_ext/hash'
end

require 'sparrow/strategies/key_transformation/underscore_key'
require 'sparrow/strategies/key_transformation/camelize_key'

module Sparrow
  module Strategies
    class TransformParams
      attr_accessor :key_transformation_strategy

      def initialize(key_transformation_strategy_buzzword)
        key_transformation_strategy = create_key_transformation_strategy(
            key_transformation_strategy_buzzword)
        self.key_transformation_strategy = key_transformation_strategy
      end

      def transform(collection_or_hash)
        case collection_or_hash
        when Array
          collection_or_hash.map { |element| transform(element) }
        when Hash
          collection_or_hash.deep_transform_keys do |key|
            key_transformation_strategy.transform_key(key)
          end
        end
      end

      def create_key_transformation_strategy(key_transformation_strategy_buzzword)
        class_name = "#{key_transformation_strategy_buzzword.to_s}_key".camelize
        strategy_class = "Sparrow::Strategies::#{class_name}".constantize
        strategy_class.new
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cp-sparrow-0.0.12 lib/sparrow/strategies/transform_params.rb
cp-sparrow-0.0.11 lib/sparrow/strategies/transform_params.rb