Transformers Build Status

Transformers is an extension to hash to allow various transformations using simple DSL. It also has optional rails extension to dry up frequently applied transformations on controller params.

Install

    gem install transformers

OR Add dependency in Gemfile for projects using bundler

    gem 'transformers'

Available Transformations

Register the converter

    Transformers.register(:name, Transformers::Name)

Usage

    hash.convert :first_name, :last_name, :as => :name, :to => :name

Rails Extensions

To Include rails extensions add the below line in a config/initializer/transformers.rb. You can register the custom transformers in the same file.

    require 'transformers/rails'

This adds transform_params extension to action controller. You can define the transformations for any action as,

    transform_params :search, :only => :index do
      convert :first_name, :last_name, :as => :name, :to => :name
    end

This will add a before_filter in the controller to apply transformations on params[:search]. If the first argument is omitted, the transformations will be applied directly on the params. The options for transform_params are same as before_filter options.

Rspec Matcher

This gem has a rspec matcher for testing the custom converters. To use this, add the below line in the spec_helper

    require 'rspec/transformers'

The converter spec can be written as

    describe Transformers::Boolean do
      it { should convert('true').to(true)}
      it { should convert('false').to(false)}
    end