workflows 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82262e54a1a6ddd11b7c05f8169f4035ff289677
4
+ data.tar.gz: 3562c0579e24c0b92dbc3d23426f2c31d840f167
5
+ SHA512:
6
+ metadata.gz: f9346dde057c147c66444331dc18570906db016f862b27b77dda6760fc635bf19a30244d18e5c8a8c87dc92f927e698cf4b2aa8232a4ead91ee300aa69feffbd
7
+ data.tar.gz: fc07d9ecf4933fc9ea77488528df903a11440a96c4e3cc35e21b1ef586aa1bf02aa48b098f97ebfc7cbe631360f98aae6be0bfb7acbdeac61b604fdcf0f876aa
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ **/*.swp
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in workflows.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Shamim
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,41 @@
1
+ # Workflows
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/workflows`. 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 'workflows'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install workflows
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. 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/sjahandideh/workflows. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](CODE_OF_CONDUCT.md) code of conduct.
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
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ ####
4
+ # minitest automations
5
+ ####
6
+ require 'rake/testtask'
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << 'test'
10
+ end
11
+
12
+ desc 'Run tests'
13
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "workflows"
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
data/bin/setup ADDED
@@ -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,34 @@
1
+ require 'workflows'
2
+
3
+ module Example::Signup
4
+ module Models
5
+
6
+ class User
7
+ attr_reader :name, :dob
8
+ attr_accessor :notified
9
+
10
+ def initialize(name, dob)
11
+ @name = name
12
+ @dob = dob
13
+ @notified = false
14
+ end
15
+
16
+ def create; true; end
17
+ def notify; true; end
18
+ end
19
+
20
+ class Admin
21
+ attr_accessor :notified
22
+
23
+ def self.get
24
+ new
25
+ end
26
+
27
+ def initialize
28
+ @notified = false
29
+ end
30
+
31
+ def notify_about(user); true; end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,41 @@
1
+ require 'workflows'
2
+ require_relative 'models'
3
+ require_relative 'step_services/create_user'
4
+ require_relative 'step_services/notify_user'
5
+ require_relative 'step_services/notify_admin'
6
+
7
+ module Example::Signup
8
+ # @example
9
+ # signup_wf = Example::Signup::SignupWorkflow.new(
10
+ # create_user: { name: 'Harry Potter', dob: '31-07-1980' },
11
+ # notify_user: {},
12
+ # notify_admin: { admin: Example::Signup::Models::Admin.get }
13
+ # )
14
+ #
15
+ # signup_wf.run
16
+ # signup_wf.status -> [:ok, :ok, :fail]
17
+ # signup_wf.output -> ["Harry Potter is successfully created.", "Harry Potter is notified", "Admin is notified"]
18
+ # signup_wf.state -> Models::User.new(name: 'Harry Potter', ...)
19
+ ###
20
+ class SignupWorkflow
21
+ include Workflows
22
+
23
+ has_flow [
24
+ {
25
+ name: 'create_user',
26
+ service: Example::Signup::CreateUserService,
27
+ args: [:name, :dob]
28
+ },
29
+ {
30
+ name: 'notify_user',
31
+ service: Example::Signup::NotifyUserService,
32
+ args: []
33
+ },
34
+ {
35
+ name: 'notify_admin',
36
+ service: Example::Signup::NotifyAdminService,
37
+ args: [:admin]
38
+ }
39
+ ]
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ require 'workflows'
2
+
3
+ require_relative '../models'
4
+
5
+ module Example::Signup
6
+
7
+ class CreateUserService
8
+ include Workflows::StepService
9
+
10
+ def run
11
+ user = Models::User.new(args.name, args.dob)
12
+ if user.create
13
+ # success
14
+ set_state(user: user)
15
+ set_output "#{user.name} is successfully created."
16
+ else
17
+ # failure
18
+ raise "Unable to create user"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ require 'workflows'
2
+
3
+ module Example::Signup
4
+
5
+ class NotifyAdminService
6
+ include Workflows::StepService
7
+
8
+ def run
9
+ if admin.notify_about(user)
10
+ # no need to reset the state
11
+ set_output "Admin is notified."
12
+ else
13
+ raise "Unable to notify admin about new user"
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def user
20
+ @user ||= get_state.user
21
+ end
22
+
23
+ def admin
24
+ @admin ||= args.admin
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ require 'workflows'
2
+
3
+ module Example::Signup
4
+
5
+ class NotifyUserService
6
+ include Workflows::StepService
7
+
8
+ def run
9
+ if user.notify
10
+ user.notified = true
11
+ set_state(user: user)
12
+ set_output "#{user.name} is notified."
13
+ else
14
+ raise "Unable to notify user"
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def user
21
+ @user ||= get_state.user
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../example'
2
+ require_relative 'signup/signup_workflow'
3
+
4
+ module Example
5
+ module Signup
6
+ end
7
+ end
data/lib/example.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Example
2
+ end
@@ -0,0 +1,55 @@
1
+ require 'ostruct'
2
+ require_relative 'step_service'
3
+
4
+ module Workflows
5
+ class Step
6
+
7
+ def self.build_all(steps = [])
8
+ steps.map do |step_hash|
9
+ new(step_hash)
10
+ end
11
+ end
12
+
13
+ attr_reader :service_obj,
14
+ :status,
15
+ :output,
16
+ :state,
17
+ :name
18
+
19
+ def initialize(args = {})
20
+ @name = args[:name]
21
+ @klass = args[:service]
22
+ @strategy = args[:strategy] || :fail
23
+ @service_args = args[:args]
24
+
25
+ @service_obj = @klass.new
26
+ end
27
+
28
+ def run
29
+ @status = :ok
30
+ service_obj.run
31
+ @output = service_obj.get_output
32
+ rescue => e
33
+ @status = :fail
34
+ @output = e.message
35
+ ensure
36
+ @state = service_obj.get_state
37
+ end
38
+
39
+ def set_args(args)
40
+ service_obj.set_args args
41
+ end
42
+
43
+ def set_state(state)
44
+ service_obj.set_state state
45
+ end
46
+
47
+ def get_state
48
+ service_obj.get_state
49
+ end
50
+
51
+ def get_output
52
+ service_obj.get_output
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,38 @@
1
+ require 'ostruct'
2
+ require 'pry'
3
+ require 'pry-byebug'
4
+
5
+ module Workflows
6
+ module StepService
7
+
8
+ ####
9
+ # Getter, Setter methods
10
+ ####
11
+
12
+ def set_args(args)
13
+ @wf_args ||=
14
+ OpenStruct.new(args)
15
+ end
16
+
17
+ def args
18
+ @wf_args
19
+ end
20
+ alias :get_args :args
21
+
22
+ def set_state(state)
23
+ @wf_state = OpenStruct.new(state)
24
+ end
25
+
26
+ def get_state
27
+ @wf_state
28
+ end
29
+
30
+ def set_output(output)
31
+ @wf_output = output
32
+ end
33
+
34
+ def get_output
35
+ @wf_output
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Workflows
2
+ VERSION = "0.1.0"
3
+ end
data/lib/workflows.rb ADDED
@@ -0,0 +1,56 @@
1
+ require_relative 'workflows/version'
2
+ require_relative 'workflows/step'
3
+
4
+ module Workflows
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def has_flow(args = [])
12
+ class_eval <<-EVAL, __FILE__, __LINE__ + 1
13
+ def wf_steps
14
+ @wf_steps ||= Step.build_all(#{args})
15
+ end
16
+ EVAL
17
+ end
18
+ end
19
+
20
+ ####
21
+ # instance methods
22
+ ####
23
+
24
+ def initialize(service_args = {})
25
+ # TODO: validate service_args
26
+ # make sure there is one for each service
27
+ # according to the wf structure defined with has_flow
28
+ wf_steps.each do |wf_step|
29
+ args = service_args.fetch(wf_step.name.to_sym)
30
+ wf_step.set_args args
31
+ end
32
+ end
33
+
34
+ def run(args = {})
35
+ previous_state = {}
36
+ wf_steps.each do |wf_step|
37
+ wf_step.set_state(previous_state)
38
+ wf_step.run
39
+ previous_state = wf_step.get_state
40
+ end
41
+
42
+ wf_steps.last.get_output
43
+ end
44
+
45
+ def state
46
+ wf_steps.last.get_state
47
+ end
48
+
49
+ def status
50
+ wf_steps.map { |wf_step| wf_step.status }
51
+ end
52
+
53
+ def output
54
+ wf_steps.map { |wf_step| wf_step.output }
55
+ end
56
+ end
data/workflows.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'workflows/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "workflows"
8
+ spec.version = Workflows::VERSION
9
+ spec.authors = ["Shamim"]
10
+ spec.email = ["shamim.jahandideh@gmail.com"]
11
+
12
+ spec.summary = %q{Organising multi-step functionalities into workflows.}
13
+ spec.description = %q{Creating a workflow class that organises and supervises micro-services.}
14
+ spec.homepage = "https://github.com/sjahandideh/workflows"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-byebug"
26
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: workflows
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shamim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-14 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: pry
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
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
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
+ description: Creating a workflow class that organises and supervises micro-services.
70
+ email:
71
+ - shamim.jahandideh@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CODE_OF_CONDUCT.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/example.rb
85
+ - lib/example/signup.rb
86
+ - lib/example/signup/models.rb
87
+ - lib/example/signup/signup_workflow.rb
88
+ - lib/example/signup/step_services/create_user.rb
89
+ - lib/example/signup/step_services/notify_admin.rb
90
+ - lib/example/signup/step_services/notify_user.rb
91
+ - lib/workflows.rb
92
+ - lib/workflows/step.rb
93
+ - lib/workflows/step_service.rb
94
+ - lib/workflows/version.rb
95
+ - workflows.gemspec
96
+ homepage: https://github.com/sjahandideh/workflows
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.5
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Organising multi-step functionalities into workflows.
120
+ test_files: []