business_flow 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28c1c9337388a7069d9222ba08a81648fdb252e5
4
+ data.tar.gz: d041d1b3dc0d6fbc1b5558f575bcbe20d7c76bbd
5
+ SHA512:
6
+ metadata.gz: fc01941f701b0e526f5d4d57da19fc2156c99ff184fa190ce587dc2103e8e19435629728f7402697e2738897a3372bdf932a90119cfeee4ed22b33b11f2eb083
7
+ data.tar.gz: 28cd1ece2cfb61a539429ffcae5bc024ab565758a745af3b58925536d2ba7f1ea047915dae3b7c3171a7e48f2da538a1aa74482cfabfa0d5c9ed0759995e16f8
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in business_flow.gemspec
6
+ gemspec
@@ -0,0 +1,88 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ business_flow (0.1.0)
5
+ activemodel (>= 3.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.1.5)
11
+ activesupport (= 5.1.5)
12
+ activesupport (5.1.5)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (~> 0.7)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ ast (2.4.0)
18
+ axiom-types (0.1.1)
19
+ descendants_tracker (~> 0.0.4)
20
+ ice_nine (~> 0.11.0)
21
+ thread_safe (~> 0.3, >= 0.3.1)
22
+ codeclimate-engine-rb (0.4.1)
23
+ virtus (~> 1.0)
24
+ coercible (1.0.0)
25
+ descendants_tracker (~> 0.0.1)
26
+ concurrent-ruby (1.0.5)
27
+ descendants_tracker (0.0.4)
28
+ thread_safe (~> 0.3, >= 0.3.1)
29
+ diff-lcs (1.3)
30
+ equalizer (0.0.11)
31
+ i18n (0.9.5)
32
+ concurrent-ruby (~> 1.0)
33
+ ice_nine (0.11.2)
34
+ minitest (5.11.3)
35
+ parallel (1.12.1)
36
+ parser (2.5.0.3)
37
+ ast (~> 2.4.0)
38
+ powerpack (0.1.1)
39
+ rainbow (3.0.0)
40
+ rake (10.5.0)
41
+ reek (4.8.0)
42
+ codeclimate-engine-rb (~> 0.4.0)
43
+ parser (>= 2.5.0.0, < 2.6)
44
+ rainbow (~> 3.0)
45
+ rspec (3.7.0)
46
+ rspec-core (~> 3.7.0)
47
+ rspec-expectations (~> 3.7.0)
48
+ rspec-mocks (~> 3.7.0)
49
+ rspec-core (3.7.1)
50
+ rspec-support (~> 3.7.0)
51
+ rspec-expectations (3.7.0)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.7.0)
54
+ rspec-mocks (3.7.0)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.7.0)
57
+ rspec-support (3.7.1)
58
+ rubocop (0.53.0)
59
+ parallel (~> 1.10)
60
+ parser (>= 2.5)
61
+ powerpack (~> 0.1)
62
+ rainbow (>= 2.2.2, < 4.0)
63
+ ruby-progressbar (~> 1.7)
64
+ unicode-display_width (~> 1.0, >= 1.0.1)
65
+ ruby-progressbar (1.9.0)
66
+ thread_safe (0.3.6)
67
+ tzinfo (1.2.5)
68
+ thread_safe (~> 0.1)
69
+ unicode-display_width (1.3.0)
70
+ virtus (1.0.5)
71
+ axiom-types (~> 0.1)
72
+ coercible (~> 1.0)
73
+ descendants_tracker (~> 0.0, >= 0.0.3)
74
+ equalizer (~> 0.0, >= 0.0.9)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ bundler (~> 1.16)
81
+ business_flow!
82
+ rake (~> 10.0)
83
+ reek (~> 4.8)
84
+ rspec (~> 3.0)
85
+ rubocop (~> 0.53)
86
+
87
+ BUNDLED WITH
88
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alex Scarborough
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,39 @@
1
+ # BusinessFlow
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/business_flow`. 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 'business_flow'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install business_flow
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]/business_flow.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'business_flow'
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(__FILE__)
@@ -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,30 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'business_flow/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'business_flow'
8
+ spec.version = BusinessFlow::VERSION
9
+ spec.authors = ['Alex Scarborough']
10
+ spec.email = ['alex@teak.io']
11
+
12
+ spec.summary = 'General purpose management of service object flows'
13
+ spec.homepage = 'https://teak.io'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'activemodel', '>= 3.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'reek', '~> 4.8'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rubocop', '~> 0.53'
30
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_model'
2
+ require 'business_flow/version'
3
+ require 'business_flow/not_nil_validator'
4
+ require 'business_flow/step'
5
+ require 'business_flow/base'
6
+
7
+ module BusienssFlow
8
+ end
@@ -0,0 +1,165 @@
1
+ # Magic!
2
+ module BusinessFlow
3
+ # More magic!
4
+ # Look at use of class attrs
5
+ # call! to raise errors
6
+ # Hooks for cross cutting concerns
7
+ # Figure out how much we can freeze
8
+ # Check output slots even if they're not going through our defined setter
9
+ # Conditional steps?
10
+ # ActiveSupport notifiers
11
+ class Base
12
+ include ActiveModel::Validations
13
+
14
+ class << self
15
+ alias invariant validates
16
+ end
17
+
18
+ class_attribute :step_queue, :requirements
19
+ attr_reader :parameter_object
20
+ private :parameter_object
21
+
22
+ def self.add_requirement(fields)
23
+ @requirements ||= []
24
+ @requirements.concat(fields)
25
+ fields.each do |field|
26
+ validates_with NotNilValidator, attributes: [field]
27
+ end
28
+ end
29
+
30
+ def self.needs(*fields)
31
+ add_requirement(fields)
32
+ wants(*fields)
33
+ end
34
+
35
+ def self.wants(*fields)
36
+ fields.each { |field| add_field(field) }
37
+ end
38
+
39
+ def self.provides(*fields)
40
+ attr_reader(*fields)
41
+ end
42
+
43
+ def self.expects(field, options = {})
44
+ validates field, options.merge(on: field)
45
+ end
46
+
47
+ def self.add_field(field)
48
+ define_method field do
49
+ if parameter_object.is_a?(Hash) && parameter_object.key?(field)
50
+ parameter_object[field]
51
+ else
52
+ parameter_object.public_send(field)
53
+ end
54
+ end
55
+ end
56
+
57
+ def self.steps(*step_queue)
58
+ self.step_queue = step_queue.map do |step|
59
+ if !step.is_a?(Step)
60
+ Step.new(step, {}, {}, self)
61
+ else
62
+ step
63
+ end
64
+ end
65
+ end
66
+
67
+ def self.step(klass, inputs = {}, outputs = {})
68
+ create_fields_for_step_outputs(outputs)
69
+ Step.new(klass, inputs, outputs, self)
70
+ end
71
+
72
+ def self.create_fields_for_step_outputs(outputs)
73
+ outputs.values.select { |field| field.is_a?(Symbol) }.map do |field|
74
+ attr_reader field
75
+ create_setter(field)
76
+ end
77
+ end
78
+
79
+ def self.create_setter(field)
80
+ define_method "#{field}=" do |new_value|
81
+ instance_variable_set("@#{field}", new_value)
82
+ validate(field)
83
+ new_value
84
+ end
85
+ end
86
+
87
+ def self.call(parameter_object)
88
+ new(parameter_object).tap(&:call)
89
+ end
90
+
91
+ def initialize(parameter_object)
92
+ @parameter_object = parameter_object
93
+ end
94
+
95
+ def call
96
+ return if invalid?
97
+ process_steps
98
+ end
99
+
100
+ def merge_errors_into(other_errors)
101
+ errors.each do |attribute, message|
102
+ attribute = "#{self.class.name.underscore}.#{attribute}"
103
+ (other_errors[attribute] << message).uniq!
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def process_steps
110
+ steps.each do |step_name|
111
+ process_step(step_name)
112
+ break if errors.any?
113
+ end
114
+ end
115
+
116
+ def process_step(step)
117
+ input_object = marshall_input_object(step.inputs)
118
+ result = step.dispatch(self, input_object)
119
+ marshall_outputs(result, step.outputs) if result.present?
120
+ end
121
+
122
+ def marshall_input_object(input_object)
123
+ return self if input_object.blank?
124
+ Hash[input_object.map do |input_name, input_value|
125
+ [
126
+ input_name,
127
+ process_input(input_value)
128
+ ]
129
+ end
130
+ ]
131
+ end
132
+
133
+ def marshall_outputs(result, output_object)
134
+ result.merge_errors_into(errors)
135
+ return if errors.any? || output_object.blank?
136
+ output_object.each do |(output_name, output_setter)|
137
+ output = result.public_send(output_name)
138
+ process_output(output, output_setter)
139
+ break if errors.any?
140
+ end
141
+ end
142
+
143
+ def process_input(input_value)
144
+ case input_value
145
+ when Symbol
146
+ send(input_value)
147
+ when Proc
148
+ instance_exec(&input_value)
149
+ end
150
+ end
151
+
152
+ def process_output(output, output_setter)
153
+ case output_setter
154
+ when Symbol
155
+ send("#{output_setter}=", output)
156
+ when Proc
157
+ instance_exec(output, &output_setter)
158
+ end
159
+ end
160
+
161
+ def steps
162
+ self.class.step_queue || []
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,11 @@
1
+ module BusinessFlow
2
+ # Validate that a given value is not nil, but allow blank/empty values.
3
+ class NotNilValidator < ActiveModel::EachValidator
4
+ # :reek:NilCheck :reek:UtilityFunction
5
+ # Dear reek -- I didn't decided the ActiveModel Validator API, I just
6
+ # have to live with it.
7
+ def validate_each(record, attribute, value)
8
+ record.errors[attribute] << 'must not be nil' if value.nil?
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ module BusinessFlow
2
+ class Step
3
+ attr_reader :klass, :inputs, :outputs
4
+ def initialize(klass, inputs, outputs, flow_klass)
5
+ @klass = klass
6
+ @inputs = inputs
7
+ @outputs = outputs
8
+ @flow_klass = flow_klass
9
+ end
10
+
11
+ # @klass can be a symbol or class
12
+ def dispatch(flow, inputs)
13
+ callable.call(flow, inputs)
14
+ end
15
+
16
+ private
17
+
18
+ def callable
19
+ @callable ||=
20
+ if @klass.is_a?(Symbol) &&
21
+ @flow_klass.method_defined?(@klass) ||
22
+ @flow_klass.private_method_defined?(@klass)
23
+ proc { |flow, _| flow.send(@klass) && nil }
24
+ else
25
+ @klass = lookup_klass ||
26
+ raise(NameError, "undefined constant #{@klass}")
27
+ proc { |_, inputs| @klass.call(inputs) }
28
+ end
29
+ end
30
+
31
+ def lookup_klass
32
+ return @klass unless @klass.is_a?(Symbol)
33
+ constant_name = @klass.to_s.camelcase
34
+ @flow_klass.parents.each do |parent|
35
+ begin
36
+ break parent.const_get(constant_name)
37
+ rescue NameError
38
+ next
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module BusinessFlow
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: business_flow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Scarborough
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: reek
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.53'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.53'
97
+ description:
98
+ email:
99
+ - alex@teak.io
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - business_flow.gemspec
115
+ - lib/business_flow.rb
116
+ - lib/business_flow/base.rb
117
+ - lib/business_flow/not_nil_validator.rb
118
+ - lib/business_flow/step.rb
119
+ - lib/business_flow/version.rb
120
+ homepage: https://teak.io
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.6.11
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: General purpose management of service object flows
144
+ test_files: []