0.0
No commit activity in last 3 years
No release in over 3 years
HashGleaner allows you to declare hash structure you want to get.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 Project Readme

HashGleaner

Summary

HashGleaner allows you to declare hash structure you want to get like Strong Parameters.

Install

$ gem install hash_gleaner

Example

require 'hash_gleaner'

h = {
  :account => {
    :id => 1234,
    :email => 'xxx@xxx.xx',
    :name => 'John Doe',
    :phone => [
      {
        :id => 2345,
        :type => 'mobile',
        :number => 'xxxxxxxxxx',
      },
      {
        :id => 2346,
        :type => 'office',
        :number => 'yyyyyyyyyy',
      },
    ],
  },
}

params = HashGleaner.apply(h) do
           o :account do
             o :name
             o :phone do
               o :type
               o :number
             end
           end
         end
#=> {:account=>{:name=>"John Doe", :phone=>[{:type=>"mobile", :number=>"xxxxxxxxxx"}, {:type=>"office", :number=>"yyyyyyyyyy"}]}}

When Hash object includes HashGleaner module, you can use glean method.

class << h; include HashGleaner; end

params = h.glean do
           o :account do
             o :name
             o :phone do
               o :type
               o :number
             end
           end
         end

A hash structure can be described as Proc object.

proc = Proc.new{
  o :account do
    o :name
    o :phone do
      o :type
      o :number
    end
  end
}

params = h.glean(proc)
params = HashGleaner.apply(h, proc)

You can describe required or optional in hash structure. When the hash object misses any required keys, HashGleaner raises MissingKeysException.

proc = Proc.new{
  (required)
  o :account do
    o :name
    o :phone do
      (required)
      o :number
      o :name
    end
  end
  (optional)
  o :admin
}

params = h.glean(proc)
#=> HashGleaner::MissingKeyException: Missing required keys [:name]