design_by_contract 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2848a2d974ba0719cfde67f1869c159823ef7a5c
4
+ data.tar.gz: b09db1b0e27ffaa7fad9fa0367af71b7563cb69f
5
+ SHA512:
6
+ metadata.gz: ae579964b09e3f8e096924837277a4c54a40c1896fabce5187402a70a7931ac04560b6f0e575354af5b892eb0c73a9ed8b70f05ea5ca3f597756071ed6cd4b04
7
+ data.tar.gz: 31ba66b25fda22e573e3358bd384174d7415428128b7e32a64c9c686b65f3617652097a2d44512b17eef472dcfbe8ce88b4ee9a28d8ad976bd35ae351618e12c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.15.4
@@ -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 adamluzsi@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 design_by_contract.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Adam Luzsi
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,50 @@
1
+ # DesignByContract
2
+
3
+ This is a collection of techniques to create contract between Objects
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'design_by_contract'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install design_by_contract
20
+
21
+ ## Usage
22
+
23
+ ### Interface
24
+
25
+ The most basic simple usecase for interface is to use it for simply validate a class
26
+
27
+ ```ruby
28
+
29
+ i = DesignByContract::Interface.new test: [:req, :opt, %i[keyreq keyword] ]
30
+
31
+ class T
32
+ def test(value, value_with_default="def", keyword:)
33
+ end
34
+ end
35
+
36
+ i.implemented_by?(T) #=> true
37
+ i.implemented_by?(Class) #=> false
38
+
39
+ ```
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/design_by_contract. 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.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
+
48
+ ## Code of Conduct
49
+
50
+ Everyone interacting in the DesignByContract project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/design_by_contract/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "design_by_contract"
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__)
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,24 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "design_by_contract"
5
+ spec.version = File.read(File.join(__dir__, "VERSION")).strip
6
+ spec.authors = ["Adam Luzsi"]
7
+ spec.email = ["adamluzsi@gmail.com"]
8
+
9
+ spec.summary = %q{Design by contract patterns for developer happiness}
10
+ spec.description = %q{Design by contract patterns for developer happiness}
11
+ spec.homepage = "https://github.com/adamluzsi/design_by_contract.rb"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.15"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,80 @@
1
+ class DesignByContract::Interface
2
+ REQUIRED_ARGUMENT = :req
3
+ OPTIONAL_ARGUMENT = :opt
4
+ REST_OF_THE_ARGUMENTS = :rest
5
+
6
+ REQUIRED_KEYWORD = :keyreq
7
+ OPTIONAL_KEYWORD = :key
8
+ AFTER_KEYWORD_ARGUMENTS = :keyrest
9
+
10
+ def initialize(method_specifications)
11
+ @method_specifications = method_specifications
12
+ end
13
+
14
+ def implemented_by?(implementator_class)
15
+ @method_specifications.each do |name, signature|
16
+ return false unless implementator_class.method_defined?(name)
17
+ return false unless signature_match?(implementator_class, name, signature)
18
+ end
19
+ true
20
+ end
21
+
22
+ private
23
+
24
+ def signature_match?(klass, name, signature)
25
+ parameters = klass.instance_method(name).parameters
26
+
27
+ return false unless req_match?(parameters, signature)
28
+ return false unless opt_match?(parameters, signature)
29
+ return false unless rest_match?(parameters, signature)
30
+ return false unless keyreq_match?(parameters, signature)
31
+ return false unless key_match?(parameters, signature)
32
+ return false unless keyrest_match?(parameters, signature)
33
+
34
+ true
35
+ end
36
+
37
+ def keyrest_match?(parameters, signature)
38
+ return true unless signature.include?(:keyrest)
39
+
40
+ parameters.any? { |k, _| k == :keyrest }
41
+ end
42
+
43
+ def key_match?(parameters, signature)
44
+ optional_keys = signature.select { |k| k.is_a?(Array) && k[0] == :key }.map(&:last)
45
+ return true if optional_keys.empty?
46
+ actual_keys = parameters.select { |k, _| k == :key }.map(&:last)
47
+ (optional_keys - actual_keys).empty?
48
+ end
49
+
50
+ def keyreq_match?(parameters, signature)
51
+ expected_keys = signature.select { |k| k.is_a?(Array) && k[0] == :keyreq }.sort
52
+ return true if expected_keys.empty?
53
+ actual_keys = parameters.select { |k, _| k == :keyreq }.sort
54
+ expected_keys == actual_keys
55
+ end
56
+
57
+ def req_match?(parameters, signature)
58
+ expected, actually = arg_counts_for(parameters, signature, :req)
59
+ return true if expected == 0
60
+ expected == actually
61
+ end
62
+
63
+ def opt_match?(parameters, signature)
64
+ expected, actually = arg_counts_for(parameters, signature, :opt)
65
+ return true if expected == 0
66
+ expected <= actually
67
+ end
68
+
69
+ def rest_match?(parameters, signature)
70
+ return true unless signature.include?(:rest)
71
+
72
+ parameters.any? { |k, _| k == :rest }
73
+ end
74
+
75
+ def arg_counts_for(parameters, signature, type)
76
+ expected_req_count = signature.select { |v| v == type }.length
77
+ actual_req_count = parameters.map(&:first).select { |v| v == type }.length
78
+ [expected_req_count, actual_req_count]
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module DesignByContract
2
+ autoload :Interface, 'design_by_contract/interface'
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: design_by_contract
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Luzsi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-21 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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
+ description: Design by contract patterns for developer happiness
56
+ email:
57
+ - adamluzsi@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - VERSION
71
+ - bin/console
72
+ - bin/setup
73
+ - design_by_contract.gemspec
74
+ - lib/design_by_contract.rb
75
+ - lib/design_by_contract/interface.rb
76
+ homepage: https://github.com/adamluzsi/design_by_contract.rb
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.13
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Design by contract patterns for developer happiness
100
+ test_files: []