0.0
No release in over 3 years
Low commit activity in last 3 years
A simple Hash to JSON serializer with some helpers to improve JSON model columns.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 4.2.6
~> 1.11
>= 4.6.5, ~> 4.6
>= 1.2.0, ~> 1.2
~> 10.0
~> 3.0
~> 0.39.0
~> 0.11.2
~> 1.3.11
~> 0.8.7.6
 Project Readme

HashSerializer

Build Status Code Climate Test Coverage Issue Count

A simple Hash to JSON serializer with some helpers to improve JSON model columns.

Back Story

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.

Installation

Add this line to your application's Gemfile:

gem 'hash_serializer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_serializer

Usage

HashSerializer::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.

HashSerializer::Helpers

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

Development

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.