GithubHelp home page GithubHelp logo

vshmyhlo / tranrax Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 14 KB

Transaction monad in Ruby

Home Page: https://github.com/v-shmyhlo/tranrax

License: MIT License

Ruby 98.43% Shell 1.57%
ruby functional-programming

tranrax's Introduction

TODO: Cleanup mess in README.md

Tranrax

Build Status

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file lib/tranrax. To experiment with that code, run bin/console for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

Installation

Add this line to your application's Gemfile:

gem 'tranrax'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tranrax

Usage

More examples in "examples" foldes

Write to a file

# write_file returns Transaction which writes to a file
# and restores original content (or removes file) in case of failure
write_file = lambda do |file_name, contents|
  Tranrax::Transaction.new do
    file_exists = File.exist?(file_name)

    operations =
      if file_exists
        tmpfile_name = SecureRandom.hex
        FileUtils.mv(file_name, tmpfile_name)

        [
          -> { FileUtils.mv(tmpfile_name, file_name) }, # rollback logic
          -> { FileUtils.rm(tmpfile_name) } # postcommit logic (cleanup on success)
        ]
      else
        [
          -> { FileUtils.rm(file_name) } # only rollback, no postcommit
        ]
      end

    File.write(file_name, contents)

    # returns result of operation ("whatever" in this case), rollback operation and postcommit operation
    ['whatever', *operations]
  end
end

# nothing is done at this point
transaction = write_file.call('test_1.txt', 'hello world')

# run transaction
transaction.transact do |_result|
  # at this point file is written and you can observe changes
  # result == "whatever"

  sleep 3
  raise 'Damn :('
end
# rollback takes places, original file restored
# you can compose transactions
transaction = write_file
  .call('test_1.txt', 'hello world')
  .map { |result| "Result of first operation is \"#{result}\"" }
  .bind { |result| write_file.call('test_2.txt', result) }

# everything is rolled back
transaction.transact do
  sleep 3
  raise 'Damn :('
end

Repo example

User = Struct.new(:id, :name)

class UserRepo
  def initialize
    @records = []
  end

  def all
    @records
  end

  def create(record)
    @records << record
    record
  end

  def delete(id)
    @records = @records.reject { |record| record.id == id }
    nil
  end
end
create_user = lambda do |repo, user|
  Tranrax::Transaction.new do
    result = repo.create(user)

    [result, -> { repo.delete(result.id) }]
  end
end

repo = UserRepo.new
repo.create(User.new(1, 'John'))

transaction = create_user.call(repo, User.new(2, 'Joe'))

transaction.transact do |result|
  puts result
  # => #<struct User id=2, name="Joe">
  puts repo.all.count
  # => 2

  raise 'Damn :('
end

puts repo.all.count
# => 1

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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 tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/v-shmyhlo/tranrax.

License

The gem is available as open source under the terms of the MIT License.

tranrax's People

Contributors

vshmyhlo avatar

Watchers

 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.