active_operation 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: 38af3fab295c23cd57429dc1f481b5aee13a8bcc
4
+ data.tar.gz: c3c4bc019d611d19a35d15ab48e6e8268e168785
5
+ SHA512:
6
+ metadata.gz: 923a98f234f74a0cd9b73a5d648b7aa062fa958043fbfd4c080841106fd9db258c96b4abc93b432f5471042226c56e086ab3a83e6dd1243ee0f9878b37ab1a70
7
+ data.tar.gz: 940a102d89f627796988ae5ea1e01ac4af5ee75fd73a764289f681c1f3f7655a56e81c41c064cbcf1af052ff391673ab011ebe360c4c0651126a91f489e981f1
@@ -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.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_operation.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Konstantin Tennhard
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,106 @@
1
+ # ActiveOperation
2
+
3
+ `ActiveOperation` is a micro-framework for modelling business processes.
4
+ It is the perfect companion for any Rails application.
5
+ The core idea behind an operation is to move code that usually would either live in a controller or a model into a dedicated object.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'active_operation', '~> 0.1.0'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install active_operation
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ To define an operation, create a new class and inherit from `ActiveOperation::Base`.
30
+ The input arguments of an operation are defined using the `input` statement.
31
+ These statements describe the arguments the initializer takes.
32
+ All arguments are accessible through reader and writer methods.
33
+ `ActiveOperation` uses `SmartProperties` to provide this feature.
34
+ More information on the available configuration options can be found in the project [README](https://github.com/t6d/smart_properties).
35
+
36
+ Every operation must implement the `#execute` method, which describes its core functionality.
37
+ Additionally, operations support the following callbacks: `before`, `around`, `after`, `succeeded`, `halted`, `error`.
38
+ The callback mechanism utilizes `ActiveSupport::Callbacks`.
39
+ Thus, everything known from Rails is applicable for operation callbacks, too.
40
+
41
+ ```ruby
42
+ # app/operations/user/signup.rb
43
+ class User::Signup < ActiveOperation::Base
44
+ input :email, accepts: String, type: :keyword, required: true
45
+ input :password, accepts: String, type: :keyword, required: true
46
+
47
+ before do
48
+ user = User.find_by(email: email)
49
+ halt user unless user.nil?
50
+ end
51
+
52
+ def execute
53
+ User.create!(email: email, password: password)
54
+ end
55
+
56
+ succeeded do
57
+ Email::SendWelcomeMail.perform(output)
58
+ end
59
+ end
60
+ ```
61
+
62
+ To execute an operation, instantiate it and invoke the `#call` method.
63
+ As a result, this method will return the operation's output, which is also available through the `#output` method.
64
+ For convenience, operations also expose a `.call` class method, which instantiates the operation, runs it and returns the its output.
65
+
66
+ Operations go through different states during their lifecycle.
67
+ After executing an operation, the state can be used for branching purposes.
68
+ In the example below, the operation's state is used to decide wether to redirect to the `user_path` or to re-render the `new` page.
69
+
70
+ ```ruby
71
+ # app/controllers/users_controller.rb
72
+ class UsersController < ApplicationController
73
+ def create
74
+ signup = User::Signup.new(**signup_params)
75
+ @user = signup.call
76
+
77
+ if signup.succeeded?
78
+ redirect_to user_path(@user)
79
+ else
80
+ flash[:error] = "Could not create user"
81
+ render :new
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def signup_params
88
+ params.permit(:email, :password)
89
+ end
90
+ end
91
+ ```
92
+
93
+ ## Development
94
+
95
+ 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.
96
+
97
+ 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).
98
+
99
+ ## Contributing
100
+
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/t6d/active_operation.
102
+
103
+
104
+ ## License
105
+
106
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_operation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_operation"
8
+ spec.version = ActiveOperation::VERSION
9
+ spec.authors = ["Konstantin Tennhard", "Sebastian Szturo"]
10
+ spec.email = ["konstantin@tennhard.net", "sebastian.szturo@gmail.com"]
11
+
12
+ spec.summary = %q{ActiveOperation is a micro-framework for modelling business processes.}
13
+ spec.homepage = "https://gitub.com/t6d/active_operation"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
22
+ spec.add_runtime_dependency "smart_properties", "~> 1.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_operation"
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
+ require "pry"
10
+ Pry.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,11 @@
1
+ require "active_support/callbacks"
2
+ require "smart_properties"
3
+
4
+ module ActiveOperation
5
+ class Error < RuntimeError; end
6
+ class AlreadyCompletedError < Error; end
7
+ end
8
+
9
+ require "active_operation/version"
10
+ require "active_operation/input"
11
+ require "active_operation/base"
@@ -0,0 +1,155 @@
1
+ class ActiveOperation::Base
2
+ include SmartProperties
3
+ include ActiveSupport::Callbacks
4
+
5
+ attr_accessor :output
6
+ attr_accessor :error
7
+
8
+ property :state, accepts: [:initialized, :halted, :succeeded, :failed], required: true, default: :initialized
9
+ protected :state=
10
+
11
+ define_callbacks :execute
12
+ define_callbacks :error, scope: [:name]
13
+ define_callbacks :succeeded, scope: [:name]
14
+ define_callbacks :halted, scope: [:name]
15
+
16
+ class << self
17
+ def call(*args)
18
+ new(*args).call
19
+ end
20
+
21
+ def inputs
22
+ []
23
+ end
24
+
25
+ protected
26
+
27
+ def input(name, type: :positional, **configuration)
28
+ property(name, type: type, **configuration)
29
+ end
30
+
31
+ def property(name, type: :keyword, **configuration)
32
+ @inputs ||= []
33
+
34
+ if type == :positional
35
+ @inputs << ActiveOperation::Input.new(type: :positional, property: super(name, required: true, **configuration))
36
+ else
37
+ @inputs << ActiveOperation::Input.new(type: :keyword, property: super(name, **configuration))
38
+ end
39
+ end
40
+
41
+ def before(*args, &callback)
42
+ set_callback(:execute, :before, *args, &callback)
43
+ end
44
+
45
+ def around(*args, &callback)
46
+ set_callback(:execute, :around, *args, &callback)
47
+ end
48
+
49
+ def after(*args, &callback)
50
+ set_callback(:execute, :after, *args, &callback)
51
+ end
52
+
53
+ def error(*args, &callback)
54
+ set_callback(:error, :after, *args, &callback)
55
+ end
56
+
57
+ def succeeded(*args, &callback)
58
+ set_callback(:succeeded, :after, *args, &callback)
59
+ end
60
+
61
+ def halted(*args, &callback)
62
+ set_callback(:halted, :after, *args, &callback)
63
+ end
64
+
65
+ private
66
+
67
+ def method_added(method)
68
+ super
69
+ protected method if method == :execute
70
+ end
71
+
72
+ def inherited(subclass)
73
+ subclass.define_singleton_method(:inputs) do
74
+ superclass.inputs + Array(@inputs)
75
+ end
76
+ end
77
+ end
78
+
79
+ around do |operation, callback|
80
+ catch(:abort) do
81
+ callback.call
82
+ end
83
+ end
84
+
85
+ def initialize(*positional_arguments, **keyword_arguments)
86
+ expected_positional_arguments = self.class.inputs.select(&:positional?)
87
+
88
+ raise ArgumentError, "wrong number of arguments" if positional_arguments.length != expected_positional_arguments.length
89
+
90
+ super(
91
+ keyword_arguments.merge(
92
+ expected_positional_arguments.zip(positional_arguments).map { |input, value| [input.name, value] }.to_h
93
+ )
94
+ )
95
+ end
96
+
97
+ def call
98
+ run_callbacks :execute do
99
+ catch(:abort) do
100
+ next if completed?
101
+ @output = execute
102
+ self.state = :succeeded
103
+ end
104
+ end
105
+
106
+ run_callbacks :halted if halted?
107
+ run_callbacks :succeeded if succeeded?
108
+
109
+ self.output
110
+ rescue => error
111
+ self.state = :failed
112
+ self.error = error
113
+ run_callbacks :error
114
+ raise
115
+ end
116
+
117
+ def output
118
+ call unless self.completed?
119
+ @output
120
+ end
121
+
122
+ def halted?
123
+ state == :halted
124
+ end
125
+
126
+ def succeeded?
127
+ state == :succeeded
128
+ end
129
+
130
+ def completed?
131
+ halted? || succeeded?
132
+ end
133
+
134
+ protected
135
+
136
+ def execute
137
+ raise NotImplementedError
138
+ end
139
+
140
+ def halt(*args)
141
+ raise ActiveOperation::AlreadyCompletedError if completed?
142
+
143
+ self.state = :halted
144
+ @output = args.length > 1 ? args : args.first
145
+ throw :abort
146
+ end
147
+
148
+ def succeed(*args)
149
+ raise ActiveOperation::AlreadyCompletedError if completed?
150
+
151
+ self.state = :succeeded
152
+ @output = args.length > 1 ? args : args.first
153
+ throw :abort
154
+ end
155
+ end
@@ -0,0 +1,18 @@
1
+ class ActiveOperation::Input
2
+ include SmartProperties
3
+
4
+ property :type, accepts: [:positional, :keyword], required: true
5
+ property :property, accepts: SmartProperties::Property, required: true
6
+
7
+ def positional?
8
+ type == :positional
9
+ end
10
+
11
+ def keyword?
12
+ type == :keyword
13
+ end
14
+
15
+ def name
16
+ property.name
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveOperation
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_operation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Tennhard
8
+ - Sebastian Szturo
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: smart_properties
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.12'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.12'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description:
99
+ email:
100
+ - konstantin@tennhard.net
101
+ - sebastian.szturo@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - active_operation.gemspec
114
+ - bin/console
115
+ - bin/setup
116
+ - lib/active_operation.rb
117
+ - lib/active_operation/base.rb
118
+ - lib/active_operation/input.rb
119
+ - lib/active_operation/version.rb
120
+ homepage: https://gitub.com/t6d/active_operation
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: ActiveOperation is a micro-framework for modelling business processes.
144
+ test_files: []