Lab42::DiggyMethods
Access (nested) hash values by means of method chaining
Usage
gem install lab42_diggy_methods
With bundler
gem 'lab42_diggy_methods'
In your code
require 'lab42/diggy_methods'
So what does it do?
Well let us speculate about it to find out:
Context: Diggy function
An extension to the Kernel as an alias to Lab42::DiggyMethods.new
Given
let(:data) { {a: 1, b: {c: 2, d: {e: 3}}} }
let(:diggy) { Diggy(**data) }
Then we can access its fields as follows
expect(diggy.a).to eq(1)
expect(diggy.b.d.__data__).to eq(e: 3)
expect(diggy.b.d.e).to eq(3)
And we can use a shortcut for key.__data__ by using key!A
expect(diggy.b.d!).to eq(e: 3)
And that works for leave nodes too of course
expect(diggy.a!).to eq(1)
And in case of missing keys
expect{ diggy.b.d.f }.to raise_error(KeyError, "key not found: :f")
If we access unknown keys we get the usual KeyError error, however we must not pass, nonhashable data
But if we pass an array
expect{ Diggy([:a]) }.to raise_error(ArgumentError)
Context: Using in ERB
In order to take advantage of this syntax we want to pass the binding of a Diggy object to ERB
Given an ERB template
require 'erb'
let(:template_text) { "<%= data.person.name %>" }
And a Lab42::DiggyMethod instance
let(:data) { Diggy(data: {person: {name: "YHS"}}) }
Then we can pass the binding to the template
expect(ERB.new(template_text).result(data.__binding__)).to eq("YHS")
LICENSE
Copyright 2022 Robert Dober [email protected]
Apache-2.0 c.f LICENSE