Skip to content

Repository files navigation

Hashifiable Build StatusCode Climate

With Hashifiable you can specify a line with the methods that will be called to create a hash representation of your object. Simple and straightforward.

How it works

require'hashifiable'classUser < Struct.new(:id,:name,:state,:private_data,:more_private_data)extendHashifiablehashify:id,:name,:stateenduser=User.new(1,'pote','active','real credit card number','super secret token')user.to_h# Yes, #to_hash works too, but be good and use to_h. :)#=> {:id=>1, :name=>"pote", :state=>"active"}

As simple as that, I find most gems with similar functionality to simply do too much, Hashifiable provides a minimum interface to solve the problem of object representation (mostly to be used in APIs) without too much fuzz.

Passing Procs and lambdas

You can also declare a Proc or a lambda instead of just specifying a method name.

classUser < ActiveRecord::Basehas_many:hobbiesextendHashifiablehashify:id,:name,:hobbies=>Proc.new{hobbies.map(:&to_hash)},:right_now=>lambda{Time.now}end

Complex structures

If you want to define whatever complex structure to be included you can simply define a method in your object that returns said structure and include it by name in the hashify statement. Like so:

classUser < ActiveRecord::Basehas_many:activity_logsextendHashifiablehashify:id,:name,:activitydefactivity{logs: self.activity_logs.map(&:to_h),appointments: self.appointments.map(&:to_h),random: {i_am_a_key: 'And I am a value',bacon: 'cats'}}endend

Installation

$ gem install hashifiable

Development setup

$ bundle install
$ rspec

Thanks

  • To @soveran for an alternative and prettier implementation than the one I had came up with originally.
  • To @tizoc for great constructive criticism and pointing me towards instance_exec, of which I didn't know about. :)
  • To all contributors

About

Dead simple hash representation declarations.

Resources

Stars

60 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages