0.0
No commit activity in last 3 years
No release in over 3 years
Lets you manipulate data as it's being set on an object without a lot of setter method boilerplate
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Transformable

Lets you manipulate data as it's being set on an object without a lot of setter method boilerplate

Installation

Add this line to your application's Gemfile:

gem 'transformable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install transformable

Usage

Use the clean method on your objects to create a setter method that manipulates inputs:

class User < ActiveRecord::Base
  include Transformable
  clean(:email) {|e| e.downcase}
end

> u = User.new(email: "foo@BAR.com")
> u.email
 => "foo@bar.com"

By default, nil values won't be passed through to the block. Use the skip_nil option if you want them:

class User < ActiveRecord::Base
  include Transformable
  clean(:email, :skip_nil => false) {|e| e || "example@foobar.com"}
end

> u = User.new(email: "email@email.com")
> u.email = nil
> u.email
 => "example@foobar.com"

I use regexes for removing forbidden characters:

class User < ActiveRecord::Base
  include Transformable
  clean(:email) {|e| e.gsub(/\s/, "")} # remove spaces
end

> u = User.new(email: "f o o @ b a r . c o m")
> u.email
 => "foo@bar.com"

Transformable works on pure ruby objects as well as ActiveRecord objects!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request