Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

command_chain

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

command_chain

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

CommandChain

Command-chain is an algorithm based on the command patterns that allows commands to be chained. It is used to divide large actions into smaller ones and automatically undo them when something goes wrong.

Each Chain consists of a series of sequential commands. So, given a sequence A→B→C→D→E, the Chain executes A, then B, then C, and so on... If one fails, the Chain will undo every command in reverse order when they are undoable. So if D fails, then the Chain undo C, then undo B, and so on.

The main reason why this gem was created is to show how using lambdas can produce a cleaner code than using service objects.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add command_chain

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install command_chain

Usage

To use the CommandChain, first, you must create the lambdas/procs responsible for the 'do' and 'undo' actions. Second, make the Chain with an Array of Arrays. If a command does not need an undo action, use an Array with just an element or nil as the second element.

do1 = lambda {|obj| puts "first execution"}
undo1 = lambda {|obj| puts "third execution"}

do2 = lambda {|obj| puts "second execution"}

do3 = lambda {|obj| raise CommandChain::Error.new("error")}
undo3 = lambda {|obj| puts "will not execute"}

do4 = lambda {|obj| puts "will not execute"}
undo4 = lambda {|obj| puts "will not execute"}

CommandChain::Chain.new(
  [
    [do1, undo1],
    [do2, nil],
    [do3, undo3],
    [do4, undo4],
  ]
).execute

There are two ways that a Command can receive variables; one is implicit because the commands are closures, and the second is through a variable that the closure gets as an argument.

add_taxes = lambda do |order|
  order.tax = 0.1
end

calculate_total = lambda do |order|
  order.total_price = order.base_price + order.base_price * order.tax
end

Order = Struct.new('Order', :base_price, :tax, :total_price)
order = Order.new
order.base_price = 120.0

CommandChain::Chain.new(
  [
    [add_taxes, nil],
    [calculate_total, nil]
  ]
).execute(order)

puts order.total_price # => 132.0

Development

After checking out the repo, run rake test 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. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://bitbucket.org/fredlinhares/command_chain.

FAQs

Package last updated on 26 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc