Provides helper functionality for ruby classes that store various database-unfriendly types as instance variables. It automatically serializes and deserializes things like classes and symbols to interact easily with both the database and your application code.
Add this line to your application's Gemfile:
gem "attribute_helpers"
And then execute:
$ bundle
Or install it yourself as:
$ gem install attribute_helpers
This gem exposes the attr_symbol
and attr_class
class methods, which when
called will wrap the related instance method to allow for better database
serialization.
require "attribute_helpers"
class Vehicle < ActiveRecord::Base
extend AttributeHelpers
attr_class :manufacturer
attr_symbol :status
end
car = Vehicle.new
car.manufacturer = Tesla # This is a class.
car.status = :parked
car.save!
car = car.reload # After a DB round-trip, typically these fields are strings.
car.manufacturer # Tesla (the class) rather than "Tesla" (the string)
car.status # :parked (the symbol) rather than "parked" (the string)
Note: while this gem was written to help with ActiveRecord objects, it has no dependencies and works great with any database backend (or none!). You can extend it into pure Ruby classes just fine!
- Fork it (https://github.com/panorama-ed/attribute_helpers/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
Make sure your changes have appropriate tests (bundle exec rspec
)
and conform to the Rubocop style specified. We use
overcommit to enforce good code.
AttributeHelpers is released under the MIT License.