Transparent conversion for ActiveRecord.
Add this line to your application's Gemfile:
gem 'activerecord-attribute_converter'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activerecord-attribute_converter
class Plus
def initialize(x)
@x = x
end
def internalize(n)
n+@x
end
def externalize(n)
n-@x
end
end
module Rot13
module_function
def internalize(str)
str.tr('A-Za-z', 'N-ZA-Mn-za-m')
end
def externalize(str)
internalize(str)
end
end
class Book < ActiveRecord::Base
apply_converter :page, Plus.new(10)
apply_converter :title, Rot13
end
book = Book.create(author: 'Author', title: 'Title', page: 24)
p book.author # => "Author"
p book.title # => "Title"
p book.page # => 24
in_db = Book.connection.execute('SELECT * FROM books').first
p in_db['author'] # => "Author"
p in_db['title'] # => "Gvgyr"
p in_db['page'] # => 34
p Book.where(title: 'Title').map(&:title) # => ["Title"]
- Fork it ( https://github.com/eagletmt/activerecord-attribute_converter/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request