Skip to content

yogeshjain999/trailblazer-activity-dsl-linear

 
 

Repository files navigation

Activity-DSL-Linear

The trailblazer-activity-dsl-linear gem brings the popular step DSL around the activity gem. It allows to create classes that you might mostly know as operations - service objects that execute your business logic in a certain order, depending on how you harness the step DSL.

Please find the full documentation on the Trailblazer website.

Example

The activity-dsl-linear gem provides three default patterns to model activities: Path, Railway and FastTrack. Here's an example of what a railway activity could look like, along with some more complex connections (you can read more about Railway strategy in the docs).

require "trailblazer-activity-dsl-linear"

class Memo::Update < Trailblazer::Activity::Railway
  # Use the DSL to describe the layout of the activity.
  step :find_model
  step :validate, Output(:failure) => End(:validation_error)
  step :save
  fail :log_error

  # Here comes your business logic.
  def find_model(ctx, id:, **)
    ctx[:model] = Memo.find_by(id: id)
  end

  def validate(ctx, params:, **)
    return true if params[:body].is_a?(String) && params[:body].size > 10
    ctx[:errors] = "body not long enough"
    false
  end

  def save(ctx, model:, params:, **)
    model.update_attributes(params)
  end

  def log_error(ctx, params:, **)
    ctx[:log] = "Some idiot wrote #{params.inspect}"
  end
end

Visually, this would translate to the following circuit.

You can run the activity by invoking its call method.

ctx = { id: 1, params: { body: "Awesome!" } }

signal, (ctx, *) = Update.( [ctx, {}] )

pp ctx #=>
{:id=>1,
 :params=>{:body=>"Awesome!"},
 :model=>#<Memo body=nil>,
 :errors=>"body not long enough"}

pp signal #=> #<struct Trailblazer::Activity::End semantic=:validation_error>

With Activity, modeling business processes turns out to be ridiculously simple: You define what should happen and when, and Trailblazer makes sure that it happens.

Features

  • Activities can model any process with arbitrary flow and connections.
  • Nesting and compositions are allowed and encouraged (via Trailblazer's dsl-linear gem).
  • Different step interfaces, manual processing of DSL options, etc is all possible.
  • Steps can be any kind of callable objects.
  • Tracing! (via Trailblazer's developer gem)

Operation

Trailblazer's Operation internally uses an activity to model the processes.

Workflow

Activities can be formed into bigger compounds and using workflow, you can build long-running processes such as a moderated blog post or a parcel delivery. Also, you don't have to use the DSL but can use the editorinstead(cool for more complex, long-running flows). Here comes a sample screenshot.

License

© Copyright 2018, Trailblazer GmbH

Licensed under the LGPLv3 license. We also offer a commercial-friendly license.

About

Simple DSL to compile Trailblazer activities.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%