TransformTree
TransformTree provides an API for buildng trees of closures and executing those closures on provided input. This allows users to output all possible combinations of their desired transformations. A small library of useful transformations is included.
Until 1.0, minor versions contain breaking changes while patch versions are backwards compatible with respect to the API.
Installation
Add this line to your application's Gemfile:
gem 'transform_tree'
And then execute:
$ bundle
Or install it yourself as:
$ gem install transform_tree
Usage
tree = TransformTree::Root.new
upcase = ->(o) { o.upcase }
downcase = ->(o) { o.downcase }
append_s = ->(o) { "#{o}s" }
append_exc = ->(o) { "#{o}!" }
tree.add_transform(upcase, downcase, TransformTree::Transforms::null)
tree.add_transform(append_s, append_exc, TransformTree::Transforms::null)
tree.execute('Woof')
=> ["WOOFs", "WOOF!", "WOOF", "woofs", "woof!", "woof", "Woofs", "Woof!", "Woof"]
For full code examples, see integration specs in /spec/integrations
.
Provided transforms
The TransformTree::Transforms
module provides some useful transformations. ret
can be used to initially split your tree into distinct values, which are to be operated on later, e.g.
closures = superlatives.map {|w| TransformTree::Transforms::ret(w)}
tree.add_transforms(*closures)
If you prefer, you can build the tree without using Transforms::ret
and instead simply make multiple calls to #execute
, passing an object to process each time.
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
.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ffleming/transform_tree.
License
The gem is available as open source under the terms of the MIT License.