flows 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 79e8e96fb6de7ee4b138c11b64d430e1937f906093f86cb990e1c4c24a3eab3d
4
+ data.tar.gz: 3f22f6b60a7d872d779189371f1f5a274296dc302dc5d93b5aba31a83a0a9925
5
+ SHA512:
6
+ metadata.gz: 8c3cd416cd84f853d6690b94fda9d721422d5b0f4934bf02036c2294aaa589eb362e3fd95422e333d53b636762080641ee6d18ec40df537b4c6890898b202546
7
+ data.tar.gz: b3cb5df724b36cd50edeb107dc5604e965f06f3032b24aed2e734c5a86224f2ed6d15e2bceb977ef8ce8c66debf0bf0736821ef827e1dbe4b1db62d7ce9db40b
data/.gitignore ADDED
@@ -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
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-performance
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.6
7
+
8
+ LineLength:
9
+ Max: 120
10
+
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - 'spec/**/*'
14
+
15
+ Style/FrozenStringLiteralComment:
16
+ EnforcedStyle: never
17
+
18
+ Metrics/ParameterLists:
19
+ CountKeywordArgs: false
20
+
21
+ Style/WhileUntilModifier:
22
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.2
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ - 2.5.5
8
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rvkolesnev@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flows.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ flows (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ codecov (0.1.14)
11
+ json
12
+ simplecov
13
+ url
14
+ coderay (1.1.2)
15
+ diff-lcs (1.3)
16
+ docile (1.3.1)
17
+ jaro_winkler (1.5.2)
18
+ json (2.2.0)
19
+ method_source (0.9.2)
20
+ parallel (1.17.0)
21
+ parser (2.6.2.0)
22
+ ast (~> 2.4.0)
23
+ pry (0.12.2)
24
+ coderay (~> 1.1.0)
25
+ method_source (~> 0.9.0)
26
+ psych (3.1.0)
27
+ rainbow (3.0.0)
28
+ rake (10.5.0)
29
+ rspec (3.8.0)
30
+ rspec-core (~> 3.8.0)
31
+ rspec-expectations (~> 3.8.0)
32
+ rspec-mocks (~> 3.8.0)
33
+ rspec-core (3.8.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-expectations (3.8.2)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-mocks (3.8.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-support (3.8.0)
42
+ rubocop (0.67.1)
43
+ jaro_winkler (~> 1.5.1)
44
+ parallel (~> 1.10)
45
+ parser (>= 2.5, != 2.5.1.1)
46
+ psych (>= 3.1.0)
47
+ rainbow (>= 2.2.2, < 4.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 1.6)
50
+ rubocop-performance (1.0.0)
51
+ rubocop (>= 0.58.0)
52
+ rubocop-rspec (1.32.0)
53
+ rubocop (>= 0.60.0)
54
+ ruby-progressbar (1.10.0)
55
+ simplecov (0.16.1)
56
+ docile (~> 1.1)
57
+ json (>= 1.8, < 3)
58
+ simplecov-html (~> 0.10.0)
59
+ simplecov-html (0.10.2)
60
+ unicode-display_width (1.5.0)
61
+ url (0.3.2)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ bundler (~> 2.0)
68
+ codecov
69
+ flows!
70
+ pry
71
+ rake (~> 10.0)
72
+ rspec (~> 3.0)
73
+ rubocop
74
+ rubocop-performance
75
+ rubocop-rspec
76
+ simplecov
77
+
78
+ BUNDLED WITH
79
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Roman Kolesnev
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.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Flows
2
+
3
+ [![Build Status](https://travis-ci.com/ffloyd/flows.svg?branch=master)](https://travis-ci.com/ffloyd/flows)
4
+ [![codecov](https://codecov.io/gh/ffloyd/flows/branch/master/graph/badge.svg)](https://codecov.io/gh/ffloyd/flows)
5
+
6
+ _TODO_
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'flows'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install flows
23
+
24
+ ## Usage
25
+
26
+ _TODO_
27
+
28
+ ## Development
29
+
30
+ 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.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`.
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ffloyd/flows. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Flows project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ffloyd/flows/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ require 'rubocop/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ RuboCop::RakeTask.new(:rubocop)
8
+
9
+ task default: %i[rubocop spec]
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'flows'
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
data/bin/setup ADDED
@@ -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
data/flows.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'flows/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'flows'
7
+ spec.version = Flows::VERSION
8
+ spec.authors = ['Roman Kolesnev']
9
+ spec.email = ['rvkolesnev@gmail.com']
10
+
11
+ spec.summary = 'Ruby framework for building FSM-like data flows.'
12
+ spec.homepage = 'https://github.com/ffloyd/flows'
13
+ spec.license = 'MIT'
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 2.0'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+
28
+ spec.add_development_dependency 'rubocop'
29
+ spec.add_development_dependency 'rubocop-performance'
30
+ spec.add_development_dependency 'rubocop-rspec'
31
+
32
+ spec.add_development_dependency 'pry'
33
+
34
+ spec.add_development_dependency 'codecov'
35
+ spec.add_development_dependency 'simplecov'
36
+ end
data/lib/flows.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Flows
2
+ class Error < StandardError; end
3
+ end
4
+
5
+ require 'flows/version'
6
+
7
+ require 'flows/router'
8
+ require 'flows/node'
9
+ require 'flows/flow'
10
+
11
+ require 'flows/result'
12
+ require 'flows/operation'
data/lib/flows/flow.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Flows
2
+ # Simple sequentional flow
3
+ class Flow
4
+ def initialize(start_node:, nodes:)
5
+ @start_node = start_node
6
+ @nodes = Hash[
7
+ nodes.map { |node| [node.name, node] }
8
+ ]
9
+ end
10
+
11
+ def call(input, context:)
12
+ current_node_name = @start_node
13
+
14
+ while current_node_name != :term
15
+ input, current_node_name = @nodes[current_node_name].call(input, context: context)
16
+ end
17
+
18
+ input
19
+ end
20
+ end
21
+ end
data/lib/flows/node.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Flows
2
+ # Representation of FSM node.
3
+ class Node
4
+ attr_reader :name, :meta
5
+
6
+ def initialize(name:, body:, router:, meta: {}, preprocessor: nil, postprocessor: nil)
7
+ @name = name
8
+ @body = body
9
+ @router = router
10
+
11
+ @meta = meta.freeze
12
+
13
+ @preprocessor = preprocessor
14
+ @postprocessor = postprocessor
15
+ end
16
+
17
+ def call(input, context:)
18
+ input = @preprocessor.call(input, context, @meta) if @preprocessor
19
+ output = @body.call(input)
20
+ output = @postprocessor.call(output, context, @meta) if @postprocessor
21
+
22
+ route = @router.call(output, context: context, meta: @meta)
23
+
24
+ [output, route]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'operation/errors'
2
+
3
+ require_relative 'operation/dsl'
4
+ require_relative 'operation/builder'
5
+ require_relative 'operation/executor'
6
+
7
+ module Flows
8
+ # Operaion DSL
9
+ module Operation
10
+ def self.included(mod)
11
+ mod.instance_variable_set(:@steps, [])
12
+ mod.extend ::Flows::Operation::DSL
13
+ end
14
+
15
+ include ::Flows::Result::Helpers
16
+
17
+ def initialize
18
+ _flows_do_checks
19
+
20
+ flow = ::Flows::Operation::Builder.new(
21
+ steps: self.class.steps,
22
+ method_source: self
23
+ ).call
24
+
25
+ @_flows_executor = ::Flows::Operation::Executor.new(
26
+ flow: flow,
27
+ success_shapes: self.class.success_shapes,
28
+ failure_shapes: self.class.failure_shapes
29
+ )
30
+ end
31
+
32
+ def call(**params)
33
+ @_flows_executor.call(**params)
34
+ end
35
+
36
+ private
37
+
38
+ def _flows_do_checks
39
+ raise NoStepsError if self.class.steps.empty?
40
+ raise NoSuccessShapeError, self if self.class.success_shapes.nil?
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,73 @@
1
+ module Flows
2
+ module Operation
3
+ # Flow builder
4
+ class Builder
5
+ def initialize(steps:, method_source:)
6
+ @method_source = method_source
7
+ @steps = steps
8
+ end
9
+
10
+ def call
11
+ resolve_wiring!
12
+ resolve_bodies!
13
+
14
+ nodes = build_nodes
15
+ Flows::Flow.new(start_node: nodes.first.name, nodes: nodes)
16
+ end
17
+
18
+ private
19
+
20
+ def resolve_wiring!
21
+ @steps = @steps.map.with_index do |step, index|
22
+ step.merge(
23
+ next_step: @steps.dig(index + 1, :name) || :term
24
+ )
25
+ end
26
+ end
27
+
28
+ def resolve_bodies!
29
+ @steps.map! do |step|
30
+ unless @method_source.respond_to?(step[:name])
31
+ raise ::Flows::Operation::NoStepImplementationError, step[:name]
32
+ end
33
+
34
+ step.merge(
35
+ body: @method_source.method(step[:name])
36
+ )
37
+ end
38
+ end
39
+
40
+ def build_nodes
41
+ @nodes = @steps.map do |step|
42
+ Flows::Node.new(
43
+ name: step[:name],
44
+ body: step[:body],
45
+ preprocessor: method(:node_preprocessor),
46
+ postprocessor: method(:node_postprocessor),
47
+ router: make_router(step)
48
+ )
49
+ end
50
+ end
51
+
52
+ def node_preprocessor(_input, context, _meta)
53
+ context[:data]
54
+ end
55
+
56
+ def node_postprocessor(output, context, _meta)
57
+ output_data = output.ok? ? output.unwrap : output.error
58
+ context[:data].merge!(output_data)
59
+
60
+ output
61
+ end
62
+
63
+ def make_router(step_definition)
64
+ routes = step_definition[:custom_routes]
65
+
66
+ routes[Flows::Result::Ok] ||= step_definition[:next_step]
67
+ routes[Flows::Result::Err] ||= :term
68
+
69
+ Flows::Router.new(routes)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,33 @@
1
+ module Flows
2
+ module Operation
3
+ # DSL methods for operation
4
+ module DSL
5
+ attr_reader :steps, :success_shapes, :failure_shapes
6
+
7
+ include Flows::Result::Helpers
8
+
9
+ def step(name, custom_routes = {})
10
+ @steps << {
11
+ name: name,
12
+ custom_routes: custom_routes
13
+ }
14
+ end
15
+
16
+ def success(*keys, **code_keys_map)
17
+ @success_shapes = if keys.empty?
18
+ code_keys_map
19
+ else
20
+ { success: keys }
21
+ end
22
+ end
23
+
24
+ def failure(*keys, **code_keys_map)
25
+ @failure_shapes = if keys.empty?
26
+ code_keys_map
27
+ else
28
+ { failure: keys }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,65 @@
1
+ module Flows
2
+ module Operation
3
+ # rubocop:disable Style/Documentation
4
+ class NoSuccessShapeError < ::Flows::Error
5
+ def message
6
+ 'Missing success output shapes'
7
+ end
8
+ end
9
+
10
+ class NoFailureShapeError < ::Flows::Error
11
+ def message
12
+ 'Missing failure output shape'
13
+ end
14
+ end
15
+
16
+ class NoStepsError < ::Flows::Error
17
+ def message
18
+ 'No steps defined'
19
+ end
20
+ end
21
+
22
+ class NoStepImplementationError < ::Flows::Error
23
+ def initialize(step_name)
24
+ @step_name = step_name
25
+ end
26
+
27
+ def message
28
+ "Missing step implementation for #{@step_name}"
29
+ end
30
+ end
31
+
32
+ class MissingOutputError < ::Flows::Error
33
+ def initialize(required_keys, actual_keys)
34
+ @missing_keys = required_keys - actual_keys
35
+ end
36
+
37
+ def message
38
+ "Missing keys in output: #{@missing_keys.join(', ')}"
39
+ end
40
+ end
41
+
42
+ class UnexpectedSuccessStatusError < ::Flows::Error
43
+ def initialize(actual_status, supported_statuses)
44
+ @actual_status = actual_status.inspect
45
+ @supported_statuses = supported_statuses.map(&:inspect).join(', ')
46
+ end
47
+
48
+ def message
49
+ "Unexpeted success result status: `#{@actual_status}`, supported statuses: `#{@supported_statuses}`"
50
+ end
51
+ end
52
+
53
+ class UnexpectedFailureStatusError < ::Flows::Error
54
+ def initialize(actual_status, supported_statuses)
55
+ @actual_status = actual_status.inspect
56
+ @supported_statuses = supported_statuses.map(&:inspect).join(', ')
57
+ end
58
+
59
+ def message
60
+ "Unexpeted failure result status: `#{@actual_status}`, supported statuses: `#{@supported_statuses}`"
61
+ end
62
+ end
63
+ # rubocop:enable Style/Documentation
64
+ end
65
+ end
@@ -0,0 +1,59 @@
1
+ module Flows
2
+ module Operation
3
+ # Runner for operation steps
4
+ class Executor
5
+ include ::Flows::Result::Helpers
6
+
7
+ def initialize(flow:, success_shapes:, failure_shapes:)
8
+ @flow = flow
9
+
10
+ @success_shapes = success_shapes
11
+ @failure_shapes = failure_shapes
12
+ end
13
+
14
+ def call(**params)
15
+ context = { data: params }
16
+ last_result = @flow.call(nil, context: context)
17
+
18
+ build_result(last_result, context)
19
+ end
20
+
21
+ private
22
+
23
+ def build_result(last_result, context)
24
+ status = last_result.status
25
+
26
+ case last_result
27
+ when Flows::Result::Ok then build_success_result(status, context)
28
+ when Flows::Result::Err then build_failure_result(status, context)
29
+ end
30
+ end
31
+
32
+ def build_success_result(status, context)
33
+ shape = @success_shapes[status]
34
+ raise ::Flows::Operation::UnexpectedSuccessStatusError.new(status, @success_shapes.keys) if shape.nil?
35
+
36
+ data = extract_data(context[:data], shape)
37
+
38
+ ok(status, data)
39
+ end
40
+
41
+ def build_failure_result(status, context)
42
+ raise ::Flows::Operation::NoFailureShapeError if @failure_shapes.nil?
43
+
44
+ shape = @failure_shapes[status]
45
+ raise ::Flows::Operation::UnexpectedFailureStatusError.new(status, @failure_shapes.keys) if shape.nil?
46
+
47
+ data = extract_data(context[:data], shape)
48
+
49
+ err(status, data)
50
+ end
51
+
52
+ def extract_data(output, keys)
53
+ raise ::Flows::Operation::MissingOutputError.new(keys, output.keys) unless keys.all? { |key| output.key?(key) }
54
+
55
+ output.slice(*keys)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,18 @@
1
+ module Flows
2
+ # Result object with context
3
+ class Result
4
+ attr_reader :status, :meta
5
+
6
+ def initialize(data, status:, meta: {})
7
+ @data = data
8
+ @status = status
9
+ @meta = meta
10
+
11
+ raise 'Use Flows::Result::Ok or Flows::Result::Err for build result objects' if self.class == Result
12
+ end
13
+ end
14
+ end
15
+
16
+ require_relative 'result/ok'
17
+ require_relative 'result/err'
18
+ require_relative 'result/helpers'
@@ -0,0 +1,28 @@
1
+ module Flows
2
+ class Result
3
+ # Wrapper for failure results
4
+ class Err < Result
5
+ class UnwrapError < Flows::Error; end
6
+
7
+ def initialize(data, status: :failure, meta: {})
8
+ super
9
+ end
10
+
11
+ def ok?
12
+ false
13
+ end
14
+
15
+ def err?
16
+ true
17
+ end
18
+
19
+ def unwrap
20
+ raise UnwrapError
21
+ end
22
+
23
+ def error
24
+ @data
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ module Flows
2
+ class Result
3
+ # Shortcuts for building result objects
4
+ module Helpers
5
+ private
6
+
7
+ def ok(status = :success, **data)
8
+ Flows::Result::Ok.new(data, status: status)
9
+ end
10
+
11
+ def err(status = :failure, **data)
12
+ Flows::Result::Err.new(data, status: status)
13
+ end
14
+
15
+ def match_ok(status = nil)
16
+ if status
17
+ lambda do |result|
18
+ result.is_a?(Flows::Result::Ok) &&
19
+ result.status == status
20
+ end
21
+ else
22
+ Flows::Result::Ok
23
+ end
24
+ end
25
+
26
+ def match_err(status = nil)
27
+ if status
28
+ lambda do |result|
29
+ result.is_a?(Flows::Result::Err) &&
30
+ result.status == status
31
+ end
32
+ else
33
+ Flows::Result::Err
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,28 @@
1
+ module Flows
2
+ class Result
3
+ # Wrapper for successful results
4
+ class Ok < Result
5
+ class NoErrorError < Flows::Error; end
6
+
7
+ def initialize(data, status: :success, meta: {})
8
+ super
9
+ end
10
+
11
+ def ok?
12
+ true
13
+ end
14
+
15
+ def err?
16
+ false
17
+ end
18
+
19
+ def unwrap
20
+ @data
21
+ end
22
+
23
+ def error
24
+ raise NoErrorError
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ module Flows
2
+ # Node router: defines predicate rules to calculate next node.
3
+ class Router
4
+ class Error < Flows::Error; end
5
+ class NoRouteError < Error; end
6
+
7
+ DEFAULT_PREPROCESSOR = ->(output, _context, _meta) { output }
8
+
9
+ def initialize(route_hash, preprocessor: DEFAULT_PREPROCESSOR)
10
+ @route_def = route_hash
11
+ @preprocessor = preprocessor
12
+ end
13
+
14
+ def call(output, context:, meta:)
15
+ data = @preprocessor.call(output, context, meta)
16
+
17
+ matched_entry = @route_def.find do |predicate, _|
18
+ predicate === data # rubocop:disable Style/CaseEquality
19
+ end
20
+
21
+ raise NoRouteError, "no route found found for output: #{output.inspect}" unless matched_entry
22
+
23
+ matched_entry[1]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Flows
2
+ VERSION = '0.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flows
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Roman Kolesnev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-27 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: codecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - rvkolesnev@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".ruby-version"
150
+ - ".travis.yml"
151
+ - CODE_OF_CONDUCT.md
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - flows.gemspec
160
+ - lib/flows.rb
161
+ - lib/flows/flow.rb
162
+ - lib/flows/node.rb
163
+ - lib/flows/operation.rb
164
+ - lib/flows/operation/builder.rb
165
+ - lib/flows/operation/dsl.rb
166
+ - lib/flows/operation/errors.rb
167
+ - lib/flows/operation/executor.rb
168
+ - lib/flows/result.rb
169
+ - lib/flows/result/err.rb
170
+ - lib/flows/result/helpers.rb
171
+ - lib/flows/result/ok.rb
172
+ - lib/flows/router.rb
173
+ - lib/flows/version.rb
174
+ homepage: https://github.com/ffloyd/flows
175
+ licenses:
176
+ - MIT
177
+ metadata: {}
178
+ post_install_message:
179
+ rdoc_options: []
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubygems_version: 3.0.3
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: Ruby framework for building FSM-like data flows.
197
+ test_files: []