Project

choc_mool

0.0
No commit activity in last 3 years
No release in over 3 years
Deep dive hash accessor
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
>= 2.11
 Project Readme

ChocMool

Build Status Code Climate

Wraps a hash, allowing easy access to deeply nested values.

Installation

gem 'choc_mool'

Usage

hash = {one: {two: {three: :four}}}
data = ChocMool.new(hash)

data.fetch(:one, :two, :three)
# => :four

using a regex

hash = {one: 'two, three'}
data = ChocMool.new(hash)

data.using(/^(.*),/).fetch(:one)
# => 'two'

using dot notation

hash = {one: {two: {three: :four}}}
data = ChocMool.new(hash)

data.fetch('one.two.three')
# => :four

data.fetch(:one, 'two.three')
# => :four

accessing attributes

A common use for ChocMool is to easily access deeply nested data returned from APIs. The API data is converted to hash from XML or JSON then wrapped by ChocMool.

XML cannot be represented directly by a hash, thus libraries that convert the XML -> hash can expose this data.

ChocMool supports accessing these values when they are accessed via #attributes on the corresponding key (as provided by Nori).

xml = "<one two='three'>four</one>"
hash = Nori.new.parse(xml)
data = ChocMool.new(hash)

data.fetch(:one, '@two')
# => 'three'

data.fetch(:one)
# => 'four'

accessing arrays

hash = {one: [:two, :three]}
data = ChocMool.new(hash)

data.fetch(:one, 1)
# => :three

data.fetch_each(:one) { |value| puts value }
# => :two
# => :three

data.fetch_each_with_index(:one) { |value, index| puts "#{index}: #{value}" }
# => 0: :two
# => 1: :three

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Links

Copyright

Copyright (c) 2013-2014 Mark Gangl. See LICENSE for details.