transacted 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 75093ead0368285253cb0a567febf04c6c468aea
4
+ data.tar.gz: e4dc24955206da96276e84a8ce9cdf0d91be572c
5
+ SHA512:
6
+ metadata.gz: 1c859ff7247e59571f206594ea3a4178ac598d0537e6f44d2d33ba5dccd261ab8544f7306593a62b41bba83b6bc75a1944a36eef9f3adcdf1e7b458782f4789d
7
+ data.tar.gz: 6536c9de83b26c52b5516e38646e8f5db08a9b7e2bcef37a1b441d5a68e2d7cb7493a81c0fce6f8598e428a41276da096174b568bb2c1b66708b3dd8e78c6bb8
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.rspec
11
+ .last_run.json
12
+ .resultset.json
13
+ .resultset.json.lock
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transacted.gemspec
4
+ gemspec
@@ -0,0 +1,68 @@
1
+ # Transacted
2
+
3
+ A library to make writing transactional code easier.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'transacted'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install transacted
20
+
21
+ ## Usage
22
+
23
+ Require it in your file:
24
+
25
+ require 'transacted'
26
+
27
+ Then you can initialize `Action`s:
28
+
29
+ action_options = {
30
+ up: -> {puts "Up !"},
31
+ down: -> {puts "Down !"}
32
+ }
33
+ action = Transacted::Action.new action_options
34
+
35
+ You can then create a `Transaction` out of `Action`s :
36
+
37
+ first_action_options = {
38
+ up: -> {puts "First up !"},
39
+ down: -> {puts "First down !"}
40
+ }
41
+ first_action = Transacted::Action.new first_action_options
42
+
43
+ second_action_options = {
44
+ up: -> {puts "Second up !"},
45
+ down: -> {puts "Second down !"}
46
+ }
47
+ second_action = Transacted::Action.new second_action_options
48
+
49
+ transaction = Transacted::Transaction.new [first_action, second_action]
50
+
51
+ And execute it =
52
+
53
+ transaction_status = transaction.execute
54
+ # First up !
55
+ # Second up !
56
+
57
+ `transaction_status` would be either
58
+ - `:execution_success` : if all the actions are executed successfully
59
+ - `:rollback_success` : if an action has failed and the rollback was made successfully
60
+ - `:rollback_failure` : if an action has failed during execution, and another failure was encountered during rollback. The transactionality is lost in such cases since the rollback was not successful.
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/[my-github-username]/transacted/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "transacted"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "transacted/version"
2
+ require "transacted/action"
3
+ require "transacted/transaction"
4
+
5
+ module Transacted
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,26 @@
1
+ module Transacted
2
+ class Action
3
+ def initialize options
4
+ raise "Must be initialized with procs" if not are_procs? [options[:up], options[:down]]
5
+ @up = options[:up]
6
+ @down = options[:down]
7
+ end
8
+
9
+ def up
10
+ return @up.call
11
+ end
12
+
13
+ def down
14
+ return @down.call
15
+ end
16
+
17
+ def valid?
18
+ are_procs? [@up,@down]
19
+ end
20
+
21
+ def are_procs? proc_or_procs
22
+ procs = [*proc_or_procs]
23
+ procs.all? { |proc| proc.is_a?(Proc)}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ module Transacted
2
+ class Transaction
3
+ def initialize actions
4
+ raise "Actions must be of type Transacted::Action" if not are_actions? actions
5
+ @actions = actions
6
+ end
7
+
8
+ def are_actions? action_or_actions
9
+ [*action_or_actions].all? {|action| action.is_a? Transacted::Action }
10
+ end
11
+
12
+ def valid?
13
+ are_actions? @actions
14
+ end
15
+
16
+ def execute
17
+ @executed_actions = []
18
+ @actions_left = @actions.clone
19
+ direction = :up
20
+ status = nil
21
+ while direction != nil do
22
+ if direction == :up
23
+
24
+ if @actions_left.count == 0
25
+ direction == nil
26
+ return :execution_success
27
+ end
28
+
29
+ action = @actions_left.shift
30
+
31
+ begin
32
+ action_value = action.up
33
+ @executed_actions.push action
34
+ direction = :down if action_value == false
35
+ rescue Exception => e
36
+ direction = :down
37
+ end
38
+
39
+ elsif direction == :down
40
+
41
+ if @executed_actions.count == 0
42
+ direction == nil
43
+ return :rollback_success
44
+ end
45
+
46
+ action = @executed_actions.pop
47
+
48
+ begin
49
+ action_value = action.down
50
+ return :rollback_failure if action_value == false
51
+ rescue
52
+ return :rollback_failure
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Transacted
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'transacted/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transacted"
8
+ spec.version = Transacted::VERSION
9
+ spec.authors = ["Omar Kamali"]
10
+ spec.email = ["okamali@productivemobile.com"]
11
+
12
+ spec.summary = %q{A library for generic transactional actions.}
13
+ spec.homepage = "http://www.productivemobile.com"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
22
+ end
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "simplecov"
27
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transacted
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Omar Kamali
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - okamali@productivemobile.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/transacted.rb
70
+ - lib/transacted/action.rb
71
+ - lib/transacted/transaction.rb
72
+ - lib/transacted/version.rb
73
+ - transacted.gemspec
74
+ homepage: http://www.productivemobile.com
75
+ licenses: []
76
+ metadata:
77
+ allowed_push_host: 'TODO: Set to ''http://mygemserver.com'' to prevent pushes to
78
+ rubygems.org, or delete to allow pushes to any server.'
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.14
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A library for generic transactional actions.
99
+ test_files: []