Code Monkey home page Code Monkey logo

lou's Introduction

Lou

Build Status Code Climate

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 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:

require 'lou'

class HashTransformer
  extend Lou::Transformer

  # optional
  revert_on RuntimeError

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

  step up { |x|
    x.flatten
  }.down { |x|
    Hash[*x]
  }
end

Then you can use it like this:

result = HashTransformer.apply(an_old_key: 'this is old')
# [:an_old_key, "this is old", :a_new_key, "this is new"]
original = HashTransformer.revert(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 revert method. Note that for each step, the input is the result of the previous step.

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

Transformers can reuse other transformers as steps. In fact, any object that defines an apply method and a revert method can be used as a step.

class FakeTransformer
  def self.apply(x)
    x + ["this is special"]
  end

  def self.revert(x)
    x.pop
    x
  end
end

class CompositeTransformer
  extend Lou::Transformer

  step HashTransformer
  step FakeTransformer
end

result = CompositeTransformer.apply(an_old_key: 'this is old')
# [:an_old_key, "this is old", :a_new_key, "this is new", "this is special"]
original = CompositeTransformer.revert(result)
# {:an_old_key=>"this is old"}

Credits

Lou was originally inspired by Hash Mapper by Ismael Celis to be a way of transforming hashes, however, it evolved into a general purpose pipeline for arbitrary blocks of code.

Related

lou's People

Contributors

iainbeeston avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.