Sha256: 131d71332eaff58f223b720639f92af3062fca7f28c36ef0c0798a9deb3c1d15

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

Lou
===

[![Build Status](https://travis-ci.org/iainbeeston/lou.svg?branch=master)](https://travis-ci.org/iainbeeston/lou)
[![Code Climate](https://codeclimate.com/github/iainbeeston/lou/badges/gpa.svg)](https://codeclimate.com/github/iainbeeston/lou)

Lou lets you define a pipeline of reversible transformations, that you can apply to any ruby object. It assumes nothing about your business logic or the objects that you're using. For example, you might want to define a pipeline of [ImageMagick](http://www.imagemagick.org) operations on an image, or a sequence of API calls. You could even use Lou as a replacement for ActiveRecord migrations.

Usage
-----

You can define transformations in their own class like this:

~~~ruby
require 'lou'

class HashTransformer
  extend Lou::Transformer

  # optional
  reverse_on RuntimeError

  step.up do |x|
    x.merge(a_new_key: 'this is new')
  end.down do |x|
   x.delete(:a_new_key)
   x
  end

  step.up do |x|
    x.flatten
  end.down do |x|
    Hash[*x]
  end
end
~~~

Then you can use it like this:

~~~ruby
result = HashTransformer.apply(an_old_key: 'this is old')
# [:an_old_key, "this is old", :a_new_key, "this is new"]
original = HashTransformer.reverse(result)
# {:an_old_key=>"this is old"}
~~~

The steps are applied in the order that they're defined, when the ~apply~ method is called, with each step receiving the result of the previous one. The process can be reversed using the ~reverse~ method. Note that for each step, the input is the result of the previous step.

If ~reverse_on~ is defined, then any completed steps will be reversed if the exception specified is raised.

Transformers inherit the steps of their parent class, so it's possible to reuse steps by using inheritance.

Credits
-------

Lou is heavily inspired by [Hash Mapper](http://github.com/ismasan) by [Ismael Celis](http://github.com/ismasan).

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lou-0.2.1 README.md