chain_of_responsibility 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: cf3c8c0e99685e1f360c4994b7863d0b54829eab
4
+ data.tar.gz: ad928480a019247e3dbe2d66ef4c4b6592abc180
5
+ SHA512:
6
+ metadata.gz: b2edddc4044ffb15dbe2b2f118b7de786bcdab1eb58143608946cdb3c956d8ffefb1be60967424f52b2d33e6d0eaafb02215fc9e0e6a2061219b32044ad03149
7
+ data.tar.gz: 35b07a2cb18d44e46c29e7c2e5140b7f8bcfae3976635ea6173efc85a77d7b097ac02a2e638010824e9f380d18e8ba8a5baa897130a7ea8d7e1c9383eabe08e0
@@ -0,0 +1,33 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ - image: circleci/ruby:2.3
10
+
11
+ steps:
12
+ - checkout
13
+
14
+ - run:
15
+ name: Setup Code Climate test-reporter
16
+ command: |
17
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
18
+ chmod +x ./cc-test-reporter
19
+
20
+ - run:
21
+ name: Install dependencies
22
+ command: bundle install --path vendor/bundle
23
+
24
+ - run:
25
+ name: Run tests
26
+ command: |
27
+ ./cc-test-reporter before-build
28
+ bundle exec rspec --format progress --color
29
+ ./cc-test-reporter after-build --coverage-input-type simplecov --exit-code $?
30
+
31
+ - run:
32
+ name: Run linter
33
+ command: bundle exec rubocop
data/.gitignore ADDED
@@ -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
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ inherit_gem:
2
+ sanelint:
3
+ - config/default.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.3
7
+
8
+ Style/Documentation:
9
+ Enabled: false
10
+
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - spec/**/*
14
+
15
+ Rails/Delegate:
16
+ Enabled: false
17
+
18
+ RSpec/NestedGroups:
19
+ Max: 4
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at kacper.pucek@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in chain_of_responsibility.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 KacperPucek
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,132 @@
1
+ # chain_of_responsibility
2
+
3
+ [![CircleCI](https://circleci.com/gh/KacperPucek/chain_of_responsibility.svg?style=shield)](https://circleci.com/gh/KacperPucek/chain_of_responsibility)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/b9b001acc0dfd25d0c19/maintainability)](https://codeclimate.com/github/KacperPucek/chain_of_responsibility/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/b9b001acc0dfd25d0c19/test_coverage)](https://codeclimate.com/github/KacperPucek/chain_of_responsibility/test_coverage)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "chain_of_responsibility"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install chain_of_responsibility
22
+
23
+ ## About
24
+
25
+ Some time ago I've stumbled upon pretty nice [article](http://rubyblog.pro/2017/11/chain-of-responsibility-ruby) written by *Sergii Makagon* about a chain of
26
+ responsibility pattern which I highly encourage you to read.
27
+
28
+ The main idea is that we can change nasty `if/else` statements to a chain of
29
+ handlers that can decide whether they are applicable or not. Handlers
30
+ are specified in the certain order and when we get a match we resolve using an appropriate handler.
31
+
32
+ This `gem` provides a tiny wrapper to make defining `chains` nicer. It's pretty minimal so feel free to simply copy the source code and adjust it to your needs if you wish.
33
+
34
+ ## Usage
35
+
36
+ A chain is composed of handlers defined in a specific order which are called one by one until
37
+ there's a handler that can handle the request. If none of the handlers can be used, error will
38
+ be raised.
39
+
40
+ ### Handlers
41
+
42
+ Handlers should inherit from `ChainOfResponsibility::Handler` and define `applicable?` and `resolve` methods that can take an arbitrary number of arguments (but both the same).
43
+
44
+ Example:
45
+
46
+ ```ruby
47
+ module Handlers
48
+ class WelcomeEmailHandler < ChainOfResponsibility::Handler
49
+ def applicable?(user)
50
+ user.registered? && user.confirmed?
51
+ end
52
+
53
+ def resolve(user)
54
+ # send email
55
+ end
56
+ end
57
+ end
58
+ ```
59
+
60
+ ### Chain
61
+
62
+ Once you have your specific handlers defined, you can easily compose them.
63
+
64
+ Example
65
+
66
+ ```ruby
67
+ module Handlers
68
+ class Passing < ChainOfResponsibility::Handler
69
+ def applicable?
70
+ true
71
+ end
72
+
73
+ def resolve
74
+ :ok
75
+ end
76
+ end
77
+ end
78
+
79
+ module Handlers
80
+ class Failing < ChainOfResponsibility::Handler
81
+ def applicable?
82
+ false
83
+ end
84
+
85
+ def resolve
86
+ # won't be reached
87
+ end
88
+ end
89
+ end
90
+
91
+ class PassingTransaction
92
+ include ChainOfResponsibility
93
+
94
+ def_handler Handlers::Failing
95
+ def_handler Handlers::Passing
96
+
97
+ def call
98
+ chain.call
99
+ end
100
+ end
101
+
102
+ PassingTransaction.new.call # => :ok
103
+
104
+ class FailingTransation
105
+ include ChainOfResponsibility
106
+
107
+ def_handler Handlers::Failing
108
+
109
+ def call
110
+ chain.call
111
+ end
112
+ end
113
+
114
+ FailingTransation.new.call # => ChainOfResponsibility::NoAppropriateHandlerFoundError
115
+ ```
116
+
117
+
118
+ ## Development
119
+
120
+ 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.
121
+
122
+ 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).
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/KacperPucek/chain_of_responsibility. 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.
127
+
128
+
129
+ ## License
130
+
131
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
132
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ #!/usr/bin/env ruby
4
+
5
+ require "bundler/setup"
6
+ require "chain_of_responsibility"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.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
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # coding: utf-8
4
+
5
+ lib = File.expand_path("../lib", __FILE__)
6
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
7
+ require "chain_of_responsibility/version"
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = "chain_of_responsibility"
11
+ spec.version = ChainOfResponsibility::VERSION
12
+ spec.authors = ["KacperPucek"]
13
+ spec.email = ["kacper.pucek@gmail.com"]
14
+
15
+ spec.summary = "Ruby implementation of Chain of Responsibility pattern."
16
+ spec.description = spec.summary
17
+ spec.homepage = "https://github.com/KacperPucek/chain_of_responsibility"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`
21
+ .split("\x0")
22
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.12"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "sanelint", "~> 1.0"
31
+ spec.add_development_dependency "simplecov", "~> 0.16.1"
32
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "chain_of_responsibility/handler"
4
+
5
+ module ChainOfResponsibility
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def handlers
12
+ @handler ||= []
13
+ end
14
+
15
+ def def_handler(handler)
16
+ handlers << handler
17
+ end
18
+ end
19
+
20
+ def chain
21
+ build_chain(self.class.handlers)
22
+ end
23
+
24
+ private
25
+
26
+ def build_chain(handlers)
27
+ handler, *remaining = handlers
28
+
29
+ if remaining.empty?
30
+ handler.new
31
+ else
32
+ handler.new(build_chain(remaining))
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChainOfResponsibility
4
+ MethodNotImplementedError = Class.new(StandardError)
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChainOfResponsibility
4
+ NoAppropriateHandlerFoundError = Class.new(StandardError)
5
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "chain_of_responsibility/errors/method_not_implemented_error"
4
+ require "chain_of_responsibility/missing_successor"
5
+
6
+ module ChainOfResponsibility
7
+ class Handler
8
+ def initialize(successor = MissingSuccessor.new)
9
+ @successor = successor
10
+ end
11
+
12
+ attr_reader :successor
13
+
14
+ def call(*args)
15
+ if applicable?(*args)
16
+ resolve(*args)
17
+ else
18
+ successor.call(*args)
19
+ end
20
+ end
21
+
22
+ def resolve(*)
23
+ raise MethodNotImplementedError, "resolve"
24
+ end
25
+
26
+ def applicable?(*)
27
+ raise MethodNotImplementedError, "applicable?"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "chain_of_responsibility/errors/no_appropriate_handler_found_error"
4
+
5
+ module ChainOfResponsibility
6
+ class MissingSuccessor
7
+ def call(*)
8
+ raise NoAppropriateHandlerFoundError
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChainOfResponsibility
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chain_of_responsibility
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - KacperPucek
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-07 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: sanelint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.16.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.16.1
83
+ description: Ruby implementation of Chain of Responsibility pattern.
84
+ email:
85
+ - kacper.pucek@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".circleci/config.yml"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - chain_of_responsibility.gemspec
102
+ - lib/chain_of_responsibility.rb
103
+ - lib/chain_of_responsibility/errors/method_not_implemented_error.rb
104
+ - lib/chain_of_responsibility/errors/no_appropriate_handler_found_error.rb
105
+ - lib/chain_of_responsibility/handler.rb
106
+ - lib/chain_of_responsibility/missing_successor.rb
107
+ - lib/chain_of_responsibility/version.rb
108
+ homepage: https://github.com/KacperPucek/chain_of_responsibility
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.5.1
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Ruby implementation of Chain of Responsibility pattern.
132
+ test_files: []
133
+ has_rdoc: