actions 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: 85a2c1a6c481e689e03e4b574e0dec8edd153f43
4
+ data.tar.gz: 7677c7d9e149e4c8df950ef5da1ae33f9f45671a
5
+ SHA512:
6
+ metadata.gz: 26bc1ef1eb0ac9accc9047368255bd338c1f7816d115c471c210328e0779890ca4544ae53ce22bf554b3759f047dc94a43d8e0e8818e14bc42f89ac6993c5c4b
7
+ data.tar.gz: 023d9da4eee4f7293535ae18b209406cc6d0e2cba0db03921eee934b70f19ef2b94d48096bd6f6e9a8485092bd887cfdcae119dd968328198b14563f30c589bd
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in actions.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Joao Carlos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Actions
2
+
3
+ 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/actions`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'actions'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install actions
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/actions.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'actions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "actions"
8
+ spec.version = Actions::VERSION
9
+ spec.authors = ["Joao Carlos", "Lauri Jutila"]
10
+ spec.email = ["mail@joao-carlos.com", "ruby@laurijutila.com"]
11
+
12
+ spec.summary = %q{Action!}
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.13"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "simplecov"
35
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "actions"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ require "actions/action"
2
+ require "actions/group"
3
+
4
+ module Actions
5
+ def self.storage_adapter=(storage_adapter)
6
+ @storage_adapter = storage_adapter
7
+ end
8
+
9
+ def self.storage_adapter
10
+ @storage_adapter
11
+ end
12
+ end
@@ -0,0 +1,62 @@
1
+ require "actions/context"
2
+ require "actions/safe_context"
3
+ require "actions/action/error_handling"
4
+ require "actions/action/storage"
5
+ require "json"
6
+
7
+ module Actions
8
+ module Action
9
+
10
+ def self.included(base)
11
+ base.class_eval do
12
+ prepend Storage
13
+ prepend ErrorHandling
14
+ extend ClassMethods
15
+ attr_reader :context
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def call(context={})
22
+ new(context).call
23
+ end
24
+
25
+ def input(attribute, type: Object, required: false, mutable: false)
26
+ self.inputs[attribute.to_s] = { type: type, mutable: mutable, required: required }
27
+ end
28
+
29
+ def output(attribute, type: Object)
30
+ self.outputs[attribute.to_s] = { type: type }
31
+ end
32
+
33
+ def inputs
34
+ @inputs ||= {}
35
+ end
36
+
37
+ def outputs
38
+ @outputs ||= {}
39
+ end
40
+
41
+ end
42
+
43
+ # FIXME: Handle context failures more gracefully, i.e. don't initialize a new
44
+ # action if one in the group failed previously.
45
+ def initialize(context = {})
46
+ @context = SafeContext.new(context)
47
+ if @context.success? && (self.class.inputs.any? || self.class.outputs.any?)
48
+ @context.action = self
49
+ self.class.inputs.each do |name, options|
50
+ @context.input!(name, type: options[:type], mutable: options[:mutable], required: options[:required])
51
+ end
52
+ self.class.outputs.each do |name, options|
53
+ @context.output!(name, type: options[:type])
54
+ end
55
+ else
56
+ @context = Context.build(context)
57
+ @context.action = self
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ require "actions/errors"
2
+
3
+ module Actions
4
+ module ErrorHandling
5
+
6
+ def call
7
+ super
8
+ context.called!(self)
9
+ context
10
+ rescue Actions::Errors::Failure
11
+ context.rollback!
12
+ return context
13
+ rescue
14
+ context.rollback!
15
+ raise
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,47 @@
1
+ module Actions
2
+ module Storage
3
+
4
+ def initialize(context = {})
5
+ super
6
+ rescue Actions::Errors::ValidationFailed => error
7
+ context = JSON.load(JSON.dump(error.context.to_h))
8
+ store(:validation_failed, context, { message: context.message })
9
+ end
10
+
11
+ def call
12
+ store_around(:started, :done, :failed) do
13
+ super
14
+ end
15
+ end
16
+
17
+ def rollback
18
+ store_around(:rollback_started, :rollback_succeeded, :rollback_failed) do
19
+ super
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def store(status, input, output)
26
+ Actions.storage_adapter.store(
27
+ name: self.class.name,
28
+ context_id: self.context.id,
29
+ input: input,
30
+ output: output,
31
+ status: status
32
+ )
33
+ end
34
+
35
+ def store_around(initial, success, failure, &block)
36
+ input_context = JSON.load(JSON.dump(context.to_h))
37
+ begin
38
+ store(initial, input_context, nil)
39
+ block.call
40
+ store(success, input_context, context.to_h)
41
+ rescue
42
+ store(failure, input_context, context.to_h)
43
+ raise
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,100 @@
1
+ require "ostruct"
2
+ require "securerandom"
3
+ require "actions/errors"
4
+
5
+ module Actions
6
+ class Context < BasicObject
7
+
8
+ attr_reader :id
9
+ attr_accessor :action
10
+
11
+ def self.build(context={})
12
+ self === context ? context : new(context)
13
+ end
14
+
15
+ def initialize(initial={})
16
+ @id = ::SecureRandom.uuid
17
+ @hash = {}
18
+ @failure = false
19
+ _update(initial)
20
+ end
21
+
22
+ def success?
23
+ !failure?
24
+ end
25
+
26
+ def failure?
27
+ @failure
28
+ end
29
+
30
+ def fail!(context={})
31
+ _update(context)
32
+ @failure = true
33
+ ::Kernel.raise ::Actions::Errors::Failure.new(self, self)
34
+ end
35
+
36
+ def defined?(attribute)
37
+ @hash.has_key?(attribute.to_s)
38
+ end
39
+
40
+ def called!(action)
41
+ _called << action
42
+ end
43
+
44
+ def rollback!
45
+ return false if @rolled_back
46
+ _called.reverse_each(&:rollback)
47
+ @rolled_back = true
48
+ end
49
+
50
+ def method_missing(name, *args)
51
+ mname = name.id2name
52
+ len = args.length
53
+ if mname.chomp!("=")
54
+ if len != 1
55
+ ::Kernel.raise ::ArgumentError, "wrong number of arguments (#{len} for 1)", ::Kernel.caller(1)
56
+ end
57
+ @hash[mname] = args[0]
58
+ elsif len == 0
59
+ @hash[mname]
60
+ else
61
+ ::Kernel.raise ::NoMethodError, "undefined method `#{mname}' for #{self}", ::Kernel.caller(1)
62
+ end
63
+ end
64
+
65
+ def respond_to?(*args)
66
+ true
67
+ end
68
+
69
+ def class
70
+ (class << self; self end).superclass
71
+ end
72
+
73
+ def inspect
74
+ status = success? ? "success" : "failure"
75
+ attributes = to_h.map { |k,v| "#{k}=#{v.inspect}" }.join(", ")
76
+ "#{self.class.name}[#{status}](#{attributes})"
77
+ end
78
+
79
+ def to_s
80
+ inspect
81
+ end
82
+
83
+ def to_h
84
+ @hash
85
+ end
86
+
87
+ private
88
+
89
+ def _update(context)
90
+ context.each do |key, value|
91
+ __send__("#{key}=", value)
92
+ end
93
+ end
94
+
95
+ def _called
96
+ @called ||= []
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,21 @@
1
+ module Actions
2
+ module Errors
3
+ class Error < StandardError
4
+ attr_reader :context
5
+
6
+ def initialize(message, context)
7
+ super(message)
8
+ @context = context
9
+ end
10
+ end
11
+
12
+ class Failure < Error
13
+ end
14
+
15
+ class ValidationFailed < Error
16
+ end
17
+
18
+ class InvalidType < Error
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ require "actions/action"
2
+
3
+ module Actions
4
+ module Group
5
+
6
+ def self.included(base)
7
+ base.class_eval do
8
+ include Action
9
+ extend ClassMethods
10
+ include InstanceMethods
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ def actions(*actions)
17
+ @actions ||= []
18
+ if actions.length > 0
19
+ @actions = actions.freeze
20
+ else
21
+ @actions
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ module InstanceMethods
28
+
29
+ def call
30
+ self.class.actions.each { |action| action.call(context) }
31
+ context
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,59 @@
1
+ require "set"
2
+ require "forwardable"
3
+ require "actions/errors"
4
+
5
+ module Actions
6
+ class SafeContext
7
+ extend Forwardable
8
+
9
+ def initialize(context = {})
10
+ @context = Context.build(context)
11
+ end
12
+
13
+ def input!(name, type: Object, required: false, mutable: false)
14
+ attribute!(name, type: type, readonly: !mutable)
15
+ validate!(name, type: type, required: required)
16
+ end
17
+
18
+ def output!(name, type: Object)
19
+ attribute!(name, type: type, readonly: false)
20
+ end
21
+
22
+ private
23
+
24
+ def attribute!(name, type: Object, readonly: false)
25
+ define_singleton_method(name) do
26
+ @context.__send__(name)
27
+ end
28
+
29
+ unless readonly
30
+ define_singleton_method("#{name}=") do |value|
31
+ unless value.is_a?(type)
32
+ raise Errors::InvalidType.new("#{name} allows values of type #{type}, received #{value.class}", self)
33
+ end
34
+
35
+ @context.__send__("#{name}=", value)
36
+ end
37
+ end
38
+ end
39
+
40
+ def validate!(attribute, type: Object, required: false)
41
+ value = @context.__send__(attribute)
42
+
43
+ if required
44
+ if value.nil?
45
+ raise Errors::ValidationFailed.new("expected #{attribute} not to be nil", self)
46
+ end
47
+ end
48
+
49
+ if @context.defined?(attribute)
50
+ unless value.is_a?(type)
51
+ raise Errors::ValidationFailed.new("expected #{attribute} to be of type #{type}, is #{value.class}", self)
52
+ end
53
+ end
54
+ end
55
+
56
+ def_delegators :@context, :success?, :failure?, :fail!, :called!,
57
+ :rollback!, :id, :to_h, :to_s, :defined?, :inspect, :action, :action=
58
+ end
59
+ end
@@ -0,0 +1,42 @@
1
+ module Actions
2
+ module StorageAdapter
3
+ class ActiveRecord
4
+
5
+ class Record < ::ActiveRecord::Base
6
+ serialize :input, JSON
7
+ serialize :output, JSON
8
+
9
+ enum status: {
10
+ started: 0,
11
+ done: 1,
12
+ failed: 2,
13
+ rollback_started: 3,
14
+ rollback_succeeded: 4,
15
+ rollback_failed: 5,
16
+ validation_failed: 6
17
+ }
18
+ end
19
+
20
+ def initialize(table_name)
21
+ @table_name = table_name
22
+ @record_class = Class.new(Record)
23
+ @record_class.table_name = table_name
24
+ end
25
+
26
+ def store(name:, context_id:, input:, output:, status:)
27
+ @record_class.create!(
28
+ name: name,
29
+ context_id: context_id,
30
+ input: input,
31
+ output: output,
32
+ status: status
33
+ )
34
+ end
35
+
36
+ def records
37
+ @record_class
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,8 @@
1
+ module Actions
2
+ module StorageAdapter
3
+ class NullAdapter
4
+ def store(*args)
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Actions
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joao Carlos
8
+ - Lauri Jutila
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-01-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.13'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.13'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: simplecov
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description:
71
+ email:
72
+ - mail@joao-carlos.com
73
+ - ruby@laurijutila.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - actions.gemspec
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/actions.rb
89
+ - lib/actions/action.rb
90
+ - lib/actions/action/error_handling.rb
91
+ - lib/actions/action/storage.rb
92
+ - lib/actions/context.rb
93
+ - lib/actions/errors.rb
94
+ - lib/actions/group.rb
95
+ - lib/actions/safe_context.rb
96
+ - lib/actions/storage_adapter/active_record.rb
97
+ - lib/actions/storage_adapter/null_adapter.rb
98
+ - lib/actions/version.rb
99
+ homepage:
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ allowed_push_host: https://rubygems.org
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.5.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Action!
124
+ test_files: []
125
+ has_rdoc: