cbra_contracts 0.1.0b

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d2368442341678d0753676087bd38c87e5daa2f712d927d0eb8fd8559957fbd
4
+ data.tar.gz: ef5b754910e1eb711fe3347a46d2b84f156f69ae593d99d98f119a3eac784f29
5
+ SHA512:
6
+ metadata.gz: 4cd6aede1fc1a351d985a3ab975e3d1d69dffe56e60a3de7af6ad27c7708972c2eb31156e3a769089841ba37222a3fb28a794415624b879dde0a2e0381038276
7
+ data.tar.gz: 1c5d236e69e7b2c42fac38271367d21c01ef964b41d73f88a9a58351c68f3410eab5b42cac096ea0acea63569ae10180d3fa60788dd202f7073e8771d5f1b70b
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.3
@@ -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 kyle.wiest@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,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 cbra_contracts.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Kyle Wiest
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,130 @@
1
+ # Component Based Rails Application (CBRA) Contracts
2
+
3
+ ## Introduction
4
+
5
+ Many Rails applications start simply enough. A generator command here, a
6
+ migration there and soon enough, you've got a working application. Some end up
7
+ as a [Majestic Monolith](https://m.signalvnoise.com/the-majestic-monolith/).
8
+
9
+ As some applications grow, and new developers come and go they can drift from
10
+ "Majestic" and become more and more challenging to work in. God objects, tightly
11
+ coupled models and a lack of clear boundaries creep up seemingly overnight and
12
+ now what you thought was a small change can trigger a host of test failures.
13
+
14
+ At this point, a common solution is to reach for microservices. Smaller, domain
15
+ specific applications that are deployed independently sound great! The tradeoffs
16
+ being that you now have a new problem to worry about - the network. What calls
17
+ can fail and where, retry policies and latency are now brought to the forefront.
18
+ Deployment infrastructure, more complicated CI/CD pipelines, complicated local
19
+ environments are all potential pitfalls of a microservice architecture.
20
+
21
+ Another alternative is to break your monolith into modular "components." These
22
+ components are still built and deployed together but offer a more clear
23
+ separation of domain-specific functionality.
24
+
25
+ CBRA Contracts aims to help guide the transition from a less-than-majestic
26
+ monolith to a collection of clearly separated functionality with distinct
27
+ boundaries and ... contracts.
28
+
29
+ ## How it Works
30
+
31
+ CBRA Contracts provides a DSL and "convention over configuration" approach to
32
+ define contract interfaces between application components. These contracts serve
33
+ to define a public interface for a component while hiding all implementation
34
+ details.
35
+
36
+ If a developer needs to use a functional component, she needs to look no further
37
+ than the contract to see what methods are available, what arguments need to be
38
+ passed and what return type she can expect.
39
+
40
+ This also allows for fearless refactoring of implementation code _inside_ of the
41
+ component. As long as the contract is fulfilled, any implementation is safe to
42
+ refactor or swap altogether.
43
+
44
+ ### Usage
45
+
46
+ In your component Gemfile, include `gem 'cbra_contracts'` or in your `.gemspec`
47
+ include `spec.runtime_dependency 'cbra_contracts'`.
48
+
49
+ ### Example
50
+
51
+ Let's define a contract!
52
+
53
+ ```ruby
54
+ # A sample `Weather` component of our application
55
+
56
+ # weather/lib/contract.rb
57
+ module Weather
58
+ extend CBRAContracts::DSL
59
+
60
+ define_contract 'Weather', 'Provides weather lookups and predictions' do
61
+
62
+ contract_method :lookup, 'Lookup the current weather of a location' do |m|
63
+ m.param :postal_code, :string, 'Postal code of the weather request'
64
+ m.param :time_of_day, :int, 'The time of day, in seconds'
65
+ m.produces WeatherReport
66
+ end
67
+
68
+ end
69
+ end
70
+ ```
71
+
72
+ Our Weather component now has a public interface that provides one method,
73
+ `lookup` and promises to return a `WeatherReport` provided that the caller
74
+ also adheres to the contract and provides arguments for `:postal_code` and
75
+ `:time_of_day`.
76
+
77
+ By convention, the implementation of the lookup should live in the
78
+ `weather/lib/weather/lookup.rb` class.
79
+
80
+ ```ruby
81
+ # weather/lib/weather/lookup.rb
82
+
83
+ module Weather
84
+ class Lookup
85
+ def call(postal_code:, time_of_day:)
86
+ ## implementation goes here
87
+
88
+ WeatherReport.new(88, 'Sunny')
89
+ end
90
+ end
91
+
92
+ class WeatherReport
93
+ attr_reader :degrees, :description
94
+
95
+ def initialize(degrees, description)
96
+ @degrees = degrees
97
+ @description = description
98
+ end
99
+ end
100
+ end
101
+ ```
102
+
103
+ This can be overriden in the contract method definition block by setting
104
+ `m.implementation = MyCustomImplementationClass.new`.
105
+
106
+ ## Future Goals
107
+
108
+ * Contract test stubs
109
+ * Documentation generation
110
+ * OpenAPI spec generation
111
+ * Rack endpoint routing
112
+
113
+ ## Contributing
114
+
115
+ Bug reports and pull requests are welcome on GitHub at
116
+ https://github.com/bluebottlecoffee/cbra_contracts. This project is intended to
117
+ be a safe, welcoming space for collaboration, and contributors are expected to
118
+ adhere to the [Contributor Covenant](http://contributor-covenant.org) code of
119
+ conduct.
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the
124
+ [AGPL License](https://www.gnu.org/licenses/agpl-3.0.en.html).
125
+
126
+ ## Code of Conduct
127
+
128
+ Everyone interacting in the CbraContracts project’s codebases, issue trackers,
129
+ chat rooms and mailing lists is expected to follow the
130
+ [code of conduct](https://github.com/bluebottlecoffee/cbra_contracts/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cbra_contracts"
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,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'cbra_contracts/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'cbra_contracts'
9
+ spec.version = CBRAContracts::VERSION
10
+ spec.authors = ['Blue Bottle Coffee']
11
+ spec.email = ['opensource@bluebottlecoffee.com']
12
+
13
+ spec.summary = 'DSL to delcare your Rails component contracts'
14
+ spec.description = 'DSL to delcare your Rails component contracts'
15
+ spec.homepage = 'https://github.com/bluebottlecoffee/CBRA-Contracts'
16
+ spec.license = 'AGPL'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_runtime_dependency 'dry-inflector', '~> 0.2'
27
+ spec.add_runtime_dependency 'dry-schema', '~> 1.5'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 2.1'
30
+ spec.add_development_dependency 'minitest', '~> 5.0'
31
+ spec.add_development_dependency 'rake', '~> 13.0'
32
+ end
@@ -0,0 +1,5 @@
1
+ require 'cbra_contracts/version'
2
+
3
+ module CBRAContracts
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CBRAContracts
4
+ class Contract
5
+ attr_reader :name, :description, :contract_methods
6
+
7
+ def initialize(name, description)
8
+ @name = name
9
+ @description = description
10
+ @contract_methods = []
11
+ end
12
+
13
+ def add_contract_method(contract_method)
14
+ contract_methods << contract_method
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/inflector'
4
+ require 'dry/schema'
5
+ require 'cbra_contracts/contract_method_parameter'
6
+
7
+ module CBRAContracts
8
+ class ContractMethod
9
+ attr_reader :name, :description, :params, :return_type
10
+
11
+ def initialize(name, description, impl_scope)
12
+ @name = name
13
+ @description = description
14
+ @impl_scope = impl_scope
15
+ @params = []
16
+ end
17
+
18
+ def param(name, type, description)
19
+ @params << Parameter.new(name, type, description)
20
+ end
21
+
22
+ def produces(klass)
23
+ @return_type = klass
24
+ end
25
+
26
+ def invoke(args)
27
+ valid = argument_schema.call(args)
28
+ if valid.success?
29
+ implementation.call(args)
30
+ else
31
+ raise_argument_error(valid.errors.messages)
32
+ end
33
+ end
34
+
35
+ def implementation
36
+ @implementation ||= Object.const_get(default_impl_class).new
37
+ end
38
+
39
+ def implementation=(impl)
40
+ @implementation = impl
41
+ end
42
+
43
+ def build_argument_schema
44
+ schema = Dry::Schema::DSL.new
45
+ params.each do |p|
46
+ schema.required(p.name).filled(p.type)
47
+ end
48
+
49
+ @argument_schema = schema.call
50
+ end
51
+
52
+ private
53
+
54
+ attr_reader :impl_scope, :argument_schema
55
+
56
+ def default_impl_class
57
+ "#{impl_scope}::#{Dry::Inflector.new.classify(name)}"
58
+ end
59
+
60
+ def raise_argument_error(error_messages)
61
+ msg = error_messages.map do |m|
62
+ "Argument #{m.path} #{m.text}"
63
+ end.join(', ')
64
+
65
+ raise ArgumentError, msg
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CBRAContracts
4
+ class Parameter
5
+ class InvalidParameterType < Error; end
6
+
7
+ VALID_TYPES = %i[string integer float].freeze
8
+
9
+ attr_reader :name, :type, :description
10
+
11
+ def initialize(name, type, description)
12
+ raise InvalidParameterType unless VALID_TYPES.include?(type)
13
+
14
+ @name = name
15
+ @type = type
16
+ @description = description
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cbra_contracts'
4
+ require 'cbra_contracts/contract'
5
+ require 'cbra_contracts/contract_method'
6
+
7
+ module CBRAContracts
8
+ module DSL
9
+ def self.extended(component)
10
+ @@component = component
11
+ end
12
+
13
+ def define_contract(name, description)
14
+ @@contract = Contract.new(name, description)
15
+ add_contract_getter_and_setter
16
+ @@component.contract = @@contract
17
+
18
+ yield
19
+ end
20
+
21
+ def contract_method(name, description)
22
+ contract_method = ContractMethod.new(name, description, @@component.to_s)
23
+ @@contract.add_contract_method(contract_method)
24
+ define_component_contract_method(contract_method)
25
+
26
+ yield contract_method
27
+
28
+ contract_method.build_argument_schema
29
+ end
30
+
31
+ # Class private; not meant to be used publicly
32
+ def add_contract_getter_and_setter
33
+ class << @@component
34
+ def contract
35
+ @@contract
36
+ end
37
+
38
+ def contract=(contract)
39
+ @@contract = contract
40
+ end
41
+ end
42
+ end
43
+
44
+ # Class private; not meant to be used publicly
45
+ def define_component_contract_method(contract_method)
46
+ @@component.module_eval do
47
+ self.define_method(contract_method.name) do |args|
48
+ contract_method.invoke(args)
49
+ end
50
+ module_function contract_method.name
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CBRAContracts
4
+ VERSION = '0.1.0b'
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbra_contracts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0b
5
+ platform: ruby
6
+ authors:
7
+ - Blue Bottle Coffee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-inflector
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ description: DSL to delcare your Rails component contracts
84
+ email:
85
+ - opensource@bluebottlecoffee.com
86
+ executables:
87
+ - console
88
+ - setup
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - cbra_contracts.gemspec
102
+ - lib/cbra_contracts.rb
103
+ - lib/cbra_contracts/contract.rb
104
+ - lib/cbra_contracts/contract_method.rb
105
+ - lib/cbra_contracts/contract_method_parameter.rb
106
+ - lib/cbra_contracts/dsl.rb
107
+ - lib/cbra_contracts/version.rb
108
+ homepage: https://github.com/bluebottlecoffee/CBRA-Contracts
109
+ licenses:
110
+ - AGPL
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: 1.3.1
126
+ requirements: []
127
+ rubygems_version: 3.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: DSL to delcare your Rails component contracts
131
+ test_files: []