Iteraptor
This small mixin allows the deep iteration / mapping of Enumerables instances.
Adopted to be used with hashes/arrays. It is not intended to be used with large objects.
Usage
require 'iteraptor'
Iteraptor is intended to be used for iteration of complex nested structures.
The yielder is being called with two parameters: “current key” and “current value.”
The key is an index (converted to string for convenience) of an element for any
Enumerable save for Hash.
Nested Enumerables are called with a compound key, represented as a “breadcrumb,”
which is a path to current key, joined with Iteraptor::DELIMITER constant. The
latter is just a dot in current release.
Features
cada(sp.each) iterates through all the levels of the nestedEnumerable, yieldingparent, elementtuple; parent is returned as a delimiter-joined stringmapa(sp.map) iterates all the elements, yieldingparent, (key, value); the mapper should return either[key, value]array ornilto remove this element;- NB this method always maps to
Hash, to map toArrayuseplana_mapa - NB this method will raise if the returned value is neither
[key, value]tuple nornil
- NB this method always maps to
plana_mapaiterates yieldingkey, value, maps to the yielded value, whatever it is;nils are not treated in some special wayaplanar(sp.flatten) the analogue ofArray#flatten, but flattens the deep enumerable intoHashinstancerecoger(sp.harvest,collect) the opposite toaplanar, it builds the nested structure out of flattened hashsegar(sp.yield), aliasescoger(sp.select) allows to filter and collect elelementsrechazar(sp.reject) allows to filter out and collect elelements.
Words are cheap, show me the code
Iteration
Iteraptor#cada iterates all the Enumerable elements, recursively. As it meets
the Enumerable, it yields it and then iterates items through.
Mapping
Mapper function should return a pair [k, v] or nil when called from hash,
or just a value when called from an array. E. g., deep hash filtering:
This is not quite convenient, but I currently have no idea how to help
the consumer to decide what to return, besides analyzing the arguments,
received by code block. That is because internally both Hash and Array are
iterated as Enumerables.
Examples
Find and report all empty values:
Filter keys, that meet a condition:
In the example below we yield all keys, that matches the regexp given as parameter.
Change all empty values in a hash to 'N/A':
Flatten the deeply nested hash:
Installation
Add this line to your application's Gemfile:
gem 'iteraptor'
And then execute:
$ bundle
Or install it yourself as:
$ gem install iteraptor
Changelog
0.6.0— experimental support forfull_parent: trueparam0.5.0—rechazarandescoger0.4.0—aplanarandplana_mapa
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/iteraptor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.