Giter Club home page Giter Club logo

hashifiable's Introduction

Hashifiable Build Status Code Climate Support via Gratipay

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'

class User < Struct.new(:id, :name, :state, :private_data, :more_private_data)
  extend Hashifiable
  hashify :id, :name, :state
end

user = 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.

class User < ActiveRecord::Base

  has_many :hobbies

  extend Hashifiable
  hashify :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:

class User < ActiveRecord::Base
  has_many :activity_logs

  extend Hashifiable
  hashify :id, :name, :activity

  def activity
  {
    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'
    }
  }
  end
end

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

hashifiable's People

Contributors

pote avatar elcuervo avatar

Watchers

Raymond avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.