Coder Social home page Coder Social logo

hashifiable's Introduction

Hashifiable

A simple way to specify the hash/json representation of your object

Build Status

TL;DR

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_hash
#=> {:id=>1, :name=>"pote", :state=>"active"}

user.to_json
#=> "{\"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

Note:

I would recommend that every item in the hashify call to be hashes, arrays, strings, symbols and the like. Hashifiable also adds a #to_json method to your object and for that to work all the values should be json-serializable.

If you want to define whatever complex structure to be included you can simply define a method in your object that returns said structure as a hash/array/whatever 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_hash),
    appointments: self.appointments.map(&:to_hash),
    random: {
      i_am_a_key: 'And I am a value',
      bacon:  'cats'
    }
  }
  end
end

Installation

$ gem install hashify

Development setup

$ bundle install
$ rspec

hashifiable's People

Contributors

pote avatar

Watchers

Nicolás Sanguinetti avatar James Cloos 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.