A simple Hash to JSON serializer with some helpers to improve JSON model columns.
HashSerializer
was birthed from the need of converting a Hash
to and from JSON for ActiveRecord JSON columns, specifically tested on Postgres. The need grew to monitoring Hash
changes in Rails models and creating dynamic methods for accessing the hash keys as if they were model properties. The need to prefix the columns came apparent when Stripe data was used in a hash and it contained keys like id
and object
.
Add this line to your application's Gemfile:
gem 'hash_serializer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hash_serializer
In a model with a JSON column (ex - billing_fields
), use HashSerializer::Serializer
serialize a Hash to JSON.
class Customer < ActiveRecord::Base
serialize :billing_fields, HashSerializer::Serializer
end
$ customer = Customer.new(billing_fields: { name: 'John C. Bland II' })
$ customer.billing_fields[:name]
=> John C. Bland II
This leaves the column open to any structure you want so it is great for development while equally terrible for data integrity.
You can utilize the HashSerializer::Helpers
to add JSON key validation.
class Customer < ActiveRecord::Base
include HashSerializer::Helpers
serialize :billing_fields, HashSerializer::Serializer
validate :validate_billing_fields
def validate_billing_fields
invalid_keys = validate_hash_serializer :billing_hash, %w(name)
errors.add(:billing_fields, 'has invalid keys: #{invalid_fields.join(', ')}') unless invalid_keys.blank?
end
end
$ customer = Customer.new(billing_fields: { name: 'John C. Bland II' })
$ customer.billing_fields[:dumb_stuff] = true
$ customer.valid?
=> false
Some JSON keys may conflict with names on the including model. You can also generate custom methods for each key for direct access to the hash without using hash syntax. It also allows for determining if a value has changed.
class Customer < ActiveRecord::Base
include HashSerializer::Helpers
serialize :billing_fields, HashSerializer::Serializer
hash_accessor_with_prefix :billing_fields, 'billing', %w(name)
end
$ customer = Customer.new(billing_fields: { name: 'John C. Bland II' })
$ customer.billing_name
=> 'John C. Bland II'
$ customer.billing_name = 'John Bland III'
$ customer.billing_name
=> 'John Bland III'
$ customer.billing_fields[:name]
=> John Bland III
$ customer.billing_name_changed?
=> true
$ customer.billing_fields_changed?
=> true
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/johncblandii/hash_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.