ProductPromotion
Logo

Ruby

made by https://0x3d.site

GitHub - apneadiving/waterfall: A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow!
A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow! - apneadiving/waterfall
Visit Site

GitHub - apneadiving/waterfall: A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow!

GitHub - apneadiving/waterfall: A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow!

Code Climate Test Coverage Build Status Gem Version

Goal

Chain ruby commands, and treat them like a flow, which provides a new approach to application control flow.

When logic is complicated, waterfalls show their true power and let you write intention revealing code. Above all they excel at chaining services.

Material

Upcoming book about failure management patterns, leveraging the gem: The Unhappy path

General presentation blog post there: Chain services objects like a boss.

Reach me @apneadiving

Overview

A waterfall object has its own flow of commands, you can chain your commands and if something wrong happens, you dam the flow which bypasses the rest of the commands.

Here is a basic representation:

  • green, the flow goes on, chain by chain
  • red its bypassed and only on_dam blocks are executed.

Waterfall Principle

Example

class FetchUser
  include Waterfall

  def initialize(user_id)
    @user_id = user_id
  end

  def call
    chain { @response = HTTParty.get("https://jsonplaceholder.typicode.com/users/#{@user_id}") }
    when_falsy { @response.success? }
      .dam { "Error status #{@response.code}" }
    chain(:user) { @response.body }
  end
end

and call / chain:

Flow.new
    .chain(user1: :user) { FetchUser.new(1) }
    .chain(user2: :user) { FetchUser.new(2) }
    .chain  {|outflow| puts(outflow.user1, outflow.user2)  } # report success
    .on_dam {|error, context| puts(error, context) }         # report error

Which works like:

Waterfall Logo

Installation

For installation, in your gemfile:

gem 'waterfall'

then bundle as usual.

Waterfall mixin

Outputs

Each waterfall has its own outflow and error_pool.

outflow is an Openstruct so you can get/set its property like a hash or like a standard object.

Wiki

Wiki contains many details, please check appropriate pages:

Koans (!)

You can try and exercise your understanding of Waterfall using the Koans here

Illustration of chaining

Doing

Flow.new
    .chain(foo: :bar) { Flow.new.chain(:bar){ 1 } }

is the same as doing:

Flow.new
    .chain do |outflow, parent_waterfall|
      unless parent_waterfall.dammed?
        child = Wf.new.chain(:bar){ 1 }
        if child.dammed?
          parent_waterfall.dam(child.error_pool)
        else
          parent_waterfall.ouflow.foo = child.outflow.bar
        end
      end
    end

Hopefully you better get the chaining power this way.

Syntactic sugar

Given:

class MyWaterfall
  include Waterfall
  def call
    self.chain { 1 }
  end
end

You may have noticed that I usually write:

Flow.new
    .chain { MyWaterfall.new }

instead of

Flow.new
    .chain { MyWaterfall.new.call }

Both are not really the same:

  • the only source of information for the gem is the return value of the block
  • if it returns a Waterfall, it will apply chaining logic. If ever the waterfall was not executed yet, it will trigger call, hence the convention.
  • if you call your waterfall object inside the block, the return value would be whatever your call method returns. So the gem doesnt know there was a waterfall involved and cannot apply chaining logic... unless you ensure self is always returned, which is cumbersome, so it's better to avoid this

Syntax advice

# this is valid
self
  .chain { Service1.new }
  .chain { Service2.new }

# this is equivalent
self.chain { Service1.new }
self.chain { Service2.new }

# this is equivalent too
chain { Service1.new }
chain { Service2.new }

# this is invalid Ruby due to the extra line
self
  .chain { Service1.new }

  .chain { Service2.new }

Tips

Error pool

For the error_pool, its up to you. But using Rails, I usually include ActiveModel::Validations in my services.

Thus you:

  • have a standard way to deal with errors
  • can deal with multiple errors
  • support I18n out of the box
  • can use your model errors out of the box

Conditional Flow

In a service, there is one and single flow, so if you need conditionals to branch off, you can do:

self.chain { Service1.new }

if foo?
  self.chain { Service2.new }
else
  self.chain { Service3.new }
end

Halting chain

Sometimes you have a flow and you need a return value. You can use halt_chain, which is executed whether or not the flow is dammed. It returns what the block returns. As a consequence, it cannot be chained anymore, so it must be the last command:

self.halt_chain do |outflow, error_pool|
  if error_pool
    # what you want to return on error
  else
    # what you want to return from the outflow
  end
end

Rails and transactions

I'm used to wrap every single object involving database interactions within transactions, so it can be rolled back on error. Here is my usual setup:

module Waterfall
  extend ActiveSupport::Concern

  class Rollback < StandardError; end

  def with_transaction(&block)
    ActiveRecord::Base.transaction(requires_new: true) do
      yield
      on_dam do
        raise Waterfall::Rollback
      end
    end
  rescue Waterfall::Rollback
    self
  end
end

And to use it:

class AuthenticateUser
  include Waterfall
  include ActiveModel::Validations

  validates :user, presence: true
  attr_reader :user

  def initialize(email, password)
    @email, @password = email, password
  end

  def call
    with_transaction do
      chain { @user = User.authenticate(@email, @password) }
      when_falsy { valid? }
        .dam { errors }
      chain(:user) { user }
    end
  end
end

The huge benefit is that if you call services from services, everything will be rolled back.

Undo

If you get to dam a flow, this would trigger the reverse_flow method in all Services previously executed.

reverse_flow is not executed on the service which just failed, consider the on_dam hook in this case.

Take this as a hook to undo whatever you need to undo if things go wrong. Yet, you probably do not need to bother about databases inserts: this is the purpose of with_transaction.

FYI

Flow is just an alias for the Wf class, so just use the one you prefer :)

Examples / Presentations

Thanks

Huge thanks to robhorrigan for the help during infinite naming brainstorming.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory