class_interface 0.1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +14 -14
- data/.rspec +2 -2
- data/.travis.yml +5 -5
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +4 -4
- data/LICENSE +21 -21
- data/README.md +121 -94
- data/Rakefile +10 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/class_interface.gemspec +35 -35
- data/lib/class_interface.rb +112 -108
- data/lib/class_interface/version.rb +3 -3
- data/lib/custom_errors/implementation_constant_type_error.rb +4 -4
- data/lib/custom_errors/implementation_incomplete_error.rb +4 -4
- data/lib/custom_errors/implementation_parameter_count_error.rb +4 -4
- data/lib/custom_errors/interface_constant_definition_error.rb +4 -4
- data/lib/custom_errors/interface_not_defined_error.rb +4 -4
- metadata +11 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ce5917f992d73dbb022c7849b7f7fdd39f640c641c770cb16e951b7d247ac1e
|
4
|
+
data.tar.gz: ab90883cb28aba282716f0731240d1547fa51ace82fba15d1fc77ef26eb0bd10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87ea34b00e74c391fa27a8f6989a5f9cf53c6cb938ad66d3886177c2d6650f882ddc50d00d187da3c838c821438e7f0e449c74884cbf5b71b166801d038a97a8
|
7
|
+
data.tar.gz: 9088a8903b83769b293f737fbf011e13bd44315664de6cce2c5da1f108b93aadc2f49d824d6a50a72f325b9aedc62f5a5aa476bf24cfcef593d1c35f95fe0e39
|
data/.gitignore
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
/.bundle/
|
2
|
-
/.yardoc
|
3
|
-
/Gemfile.lock
|
4
|
-
/_yardoc/
|
5
|
-
/coverage/
|
6
|
-
/doc/
|
7
|
-
/pkg/
|
8
|
-
/spec/reports/
|
9
|
-
/tmp/
|
10
|
-
/.vscode/
|
11
|
-
/.idea/
|
12
|
-
*.gem
|
13
|
-
|
14
|
-
# rspec failure tracking
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
/.vscode/
|
11
|
+
/.idea/
|
12
|
+
*.gem
|
13
|
+
|
14
|
+
# rspec failure tracking
|
15
15
|
.rspec_status
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--format documentation
|
2
|
-
--color
|
1
|
+
--format documentation
|
2
|
+
--color
|
data/.travis.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
sudo: false
|
2
|
-
language: ruby
|
3
|
-
rvm:
|
4
|
-
- 2.3.3
|
5
|
-
before_install: gem install bundler -v 1.14.6
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.3.3
|
5
|
+
before_install: gem install bundler -v 1.14.6
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -1,74 +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 class_interface.gemspec@mail.magynhard.de. 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/
|
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 class_interface.gemspec@mail.magynhard.de. 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
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in class_interface.gemspec
|
4
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in class_interface.gemspec
|
4
|
+
gemspec
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2017 Matthäus Beyrle
|
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 all
|
13
|
-
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 THE
|
21
|
-
SOFTWARE.
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017-2021 Matthäus J. N. Beyrle
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,94 +1,121 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
def
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
1
|
+
# class_interface
|
2
|
+
|
3
|
+
Ruby gem to extend Ruby to support class interfaces you may know them from other programming languages like C++ or Java.
|
4
|
+
|
5
|
+
Raises a variety of different exceptions when the requirements of the interface are not met.
|
6
|
+
|
7
|
+
That can be very handy in teams to declare requirements for specific types of classes.
|
8
|
+
|
9
|
+
# Contents
|
10
|
+
|
11
|
+
* [Installation](#installation)
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Documentation](#documentation)
|
14
|
+
* [Contributing](#contributing)
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
<a name="installation"></a>
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'class_interface'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle install
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install class_interface
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
<a name="usage"></a>
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
### Defining an interface
|
43
|
+
|
44
|
+
It is a good idea, to start an interface name by convention with an I, following by a capitalized camel case constant name, i.e.: `IMyInterface, IHouse, IClassName, ...`
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class IExample
|
48
|
+
MIN_AGE = Integer
|
49
|
+
DEFAULT_ENV = String
|
50
|
+
SOME_CONSTANT = nil
|
51
|
+
|
52
|
+
def self.some_static_method
|
53
|
+
end
|
54
|
+
|
55
|
+
def some_instance_method
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
### Implementing an interface
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class MyImplementation
|
64
|
+
MIN_AGE = 21
|
65
|
+
DEFAULT_ENV = 'dev'
|
66
|
+
SOME_CONSTANT = 'some_value'
|
67
|
+
|
68
|
+
def specific_method
|
69
|
+
puts "very specific"
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.some_static_method
|
73
|
+
puts "static method is implemented!"
|
74
|
+
end
|
75
|
+
|
76
|
+
def some_instance_method
|
77
|
+
# implementation
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.another_methods
|
81
|
+
# implementation
|
82
|
+
end
|
83
|
+
|
84
|
+
implements IExample
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
<a name="documentation"></a>
|
93
|
+
## Documentation
|
94
|
+
```ruby
|
95
|
+
#implements(InterfaceClassConstant)
|
96
|
+
```
|
97
|
+
|
98
|
+
_InterfaceClassConstant_ must be a valid InterfaceClassConstant or a String, containing a valid InterfaceClassConstant, i.e.: IMyInterface / "IMyInterface"
|
99
|
+
|
100
|
+
### Methods
|
101
|
+
|
102
|
+
All defined methods in the interface class must be implemented in the implementing class.
|
103
|
+
The parameter count must be the same. A distinction is made between static and dynamic methods.
|
104
|
+
|
105
|
+
### Constant Types
|
106
|
+
|
107
|
+
All CONSTANTS defined in the interface class must be implemented in the implementing class.
|
108
|
+
CONSTANTS of interfaces may be defined with `nil`, to allow all types of definitions in the implementing class.
|
109
|
+
|
110
|
+
Otherwise to specify a type, assign its class constant, e.g. `String`, `Array`, `MyCustomClass`, ...
|
111
|
+
If a specified type is defined, it is mandatory for the implementation to use that type.
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
<a name="contributing"></a>
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/class_interface. 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.
|
121
|
+
|
data/Rakefile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
#
|
5
|
-
# run default task to see tasks to build and publish gem
|
6
|
-
#
|
7
|
-
task :default do
|
8
|
-
system 'rake --tasks'
|
9
|
-
end
|
10
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
#
|
5
|
+
# run default task to see tasks to build and publish gem
|
6
|
+
#
|
7
|
+
task :default do
|
8
|
+
system 'rake --tasks'
|
9
|
+
end
|
10
|
+
|
11
11
|
RSpec::Core::RakeTask.new(:spec)
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "class_interface"
|
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__)
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "class_interface"
|
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
CHANGED
@@ -1,8 +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
|
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/class_interface.gemspec
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'class_interface/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "class_interface"
|
8
|
-
spec.version = ClassInterface::VERSION
|
9
|
-
spec.authors = ["Matthäus Beyrle"]
|
10
|
-
spec.email = ["class_interface.gemspec@mail.magynhard.de"]
|
11
|
-
|
12
|
-
spec.summary = %q{Extends Ruby to support class interfaces}
|
13
|
-
spec.homepage = "https://github.com/magynhard/class_interface"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
-
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
-
if spec.respond_to?(:metadata)
|
19
|
-
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
-
else
|
21
|
-
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
-
"public gem pushes."
|
23
|
-
end
|
24
|
-
|
25
|
-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
-
f.match(%r{^(test|spec|features)/})
|
27
|
-
end
|
28
|
-
spec.bindir = 'exe'
|
29
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = ['lib']
|
31
|
-
|
32
|
-
spec.add_development_dependency 'bundler', '>= 1.14'
|
33
|
-
spec.add_development_dependency 'rake', '
|
34
|
-
spec.add_development_dependency 'rspec', '~> 3.0'
|
35
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'class_interface/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "class_interface"
|
8
|
+
spec.version = ClassInterface::VERSION
|
9
|
+
spec.authors = ["Matthäus Beyrle"]
|
10
|
+
spec.email = ["class_interface.gemspec@mail.magynhard.de"]
|
11
|
+
|
12
|
+
spec.summary = %q{Extends Ruby to support class interfaces}
|
13
|
+
spec.homepage = "https://github.com/magynhard/class_interface"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
spec.add_development_dependency 'bundler', '>= 1.14'
|
33
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
35
|
+
end
|
data/lib/class_interface.rb
CHANGED
@@ -1,109 +1,113 @@
|
|
1
|
-
require 'class_interface/version'
|
2
|
-
|
3
|
-
require_relative 'custom_errors/implementation_constant_type_error'
|
4
|
-
require_relative 'custom_errors/implementation_incomplete_error'
|
5
|
-
require_relative 'custom_errors/implementation_parameter_count_error'
|
6
|
-
require_relative 'custom_errors/interface_constant_definition_error'
|
7
|
-
require_relative 'custom_errors/interface_not_defined_error'
|
8
|
-
|
9
|
-
#
|
10
|
-
# Equip classes with the requirement to implement interfaces
|
11
|
-
#
|
12
|
-
|
13
|
-
#
|
14
|
-
# - ensures self.static methods to be defined (checking parameters count to be equal)
|
15
|
-
# - ensures instance methods to be defined (checking parameters count to be equal)
|
16
|
-
# - ensures CONSTANTS to be defined (checking for type of CONSTANTS)
|
17
|
-
#
|
18
|
-
# @param interface_constant [Class|String]
|
19
|
-
def implements(interface_constant)
|
20
|
-
implementation_class = extract_caller_path caller_locations(1, 16)
|
21
|
-
if Object.const_defined? interface_constant.to_s
|
22
|
-
#--------------------------------------------------------------------------
|
23
|
-
# check if definitions exist
|
24
|
-
#--------------------------------------------------------------------------
|
25
|
-
# instance methods
|
26
|
-
interface_instance_methods = Object.const_get(interface_constant.to_s).instance_methods false
|
27
|
-
implementation_instance_methods = Object.const_get(implementation_class.to_s).instance_methods false
|
28
|
-
unimplemented_instance_methods = interface_instance_methods - implementation_instance_methods
|
29
|
-
# static methods
|
30
|
-
interface_static_methods = Object.const_get(interface_constant.to_s).methods false
|
31
|
-
implementation_static_methods = Object.const_get(implementation_class.to_s).methods false
|
32
|
-
unimplemented_static_methods = interface_static_methods - implementation_static_methods
|
33
|
-
# constants
|
34
|
-
interface_constants = Object.const_get(interface_constant.to_s).constants
|
35
|
-
implementation_constants = Object.const_get(implementation_class.to_s).constants
|
36
|
-
unimplemented_constants = interface_constants - implementation_constants
|
37
|
-
if unimplemented_instance_methods.any? || unimplemented_static_methods.any? || unimplemented_constants.any?
|
38
|
-
error_message = "\nThe interface '#{interface_constant}' was not completely implemented in class '#{implementation_class}'."
|
39
|
-
error_message += "\n - missing instance methods:\n #{unimplemented_instance_methods.join(' ')}" if unimplemented_instance_methods.any?
|
40
|
-
error_message += "\n - missing static methods:\n #{unimplemented_static_methods.join(' ')}" if unimplemented_static_methods.any?
|
41
|
-
error_message += "\n - missing constants:\n #{unimplemented_constants.join(' ')}" if unimplemented_constants.any?
|
42
|
-
raise ClassInterface::ImplementationIncompleteError, error_message
|
43
|
-
end
|
44
|
-
|
45
|
-
#--------------------------------------------------------------------------
|
46
|
-
# check if method parameters count does match on implementation and interface
|
47
|
-
#--------------------------------------------------------------------------
|
48
|
-
interface_instance_methods.each do |method_name|
|
49
|
-
if_params_count_arity = Object.const_get(interface_constant.to_s).new.method(method_name).arity
|
50
|
-
impl_params_count_arity = Object.const_get(implementation_class).new.method(method_name).arity
|
51
|
-
if if_params_count_arity != impl_params_count_arity
|
52
|
-
raise ClassInterface::ImplementationParameterCountError, "Parameters of instance method '#{implementation_class}##{method_name}' do not match with interface '#{interface_constant}##{method_name}' (given #{impl_params_count_arity.abs}, expected #{if_params_count_arity.abs})"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
interface_static_methods.each do |method_name|
|
56
|
-
if_params_count_arity = Object.const_get(interface_constant).method(method_name).arity
|
57
|
-
impl_params_count_arity = Object.const_get(implementation_class).method(method_name).arity
|
58
|
-
if if_params_count_arity != impl_params_count_arity
|
59
|
-
raise ClassInterface::ImplementationParameterCountError, "Parameters of static method '#{implementation_class}##{method_name}' do not match with interface '#{interface_constant}##{method_name}' (given #{impl_params_count_arity.abs}, expected #{if_params_count_arity.abs})"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
#--------------------------------------------------------------------------
|
64
|
-
# check if class types of CONSTANTS do match
|
65
|
-
#--------------------------------------------------------------------------
|
66
|
-
invalid_const_definitions = []
|
67
|
-
if_const_class_type = nil
|
68
|
-
interface_constants.each do |const_name|
|
69
|
-
if_const_string = "%s::%s" % [interface_constant, const_name]
|
70
|
-
if_const_class_type = Object.const_get(if_const_string)
|
71
|
-
if_const_class_type = Object.const_get(if_const_string).class unless Object.const_get(if_const_string).class == Class
|
72
|
-
invalid_const_definitions += [const_name] unless if_const_class_type.class == Class
|
73
|
-
next if if_const_class_type.to_s == 'NilClass'
|
74
|
-
impl_const_value_type = Object.const_get("%s::%s" % [implementation_class, const_name]).class
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
elsif
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
#
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
1
|
+
require 'class_interface/version'
|
2
|
+
|
3
|
+
require_relative 'custom_errors/implementation_constant_type_error'
|
4
|
+
require_relative 'custom_errors/implementation_incomplete_error'
|
5
|
+
require_relative 'custom_errors/implementation_parameter_count_error'
|
6
|
+
require_relative 'custom_errors/interface_constant_definition_error'
|
7
|
+
require_relative 'custom_errors/interface_not_defined_error'
|
8
|
+
|
9
|
+
#
|
10
|
+
# Equip classes with the requirement to implement interfaces
|
11
|
+
#
|
12
|
+
|
13
|
+
#
|
14
|
+
# - ensures self.static methods to be defined (checking parameters count to be equal)
|
15
|
+
# - ensures instance methods to be defined (checking parameters count to be equal)
|
16
|
+
# - ensures CONSTANTS to be defined (checking for type of CONSTANTS)
|
17
|
+
#
|
18
|
+
# @param interface_constant [Class|String]
|
19
|
+
def implements(interface_constant)
|
20
|
+
implementation_class = extract_caller_path caller_locations(1, 16)
|
21
|
+
if Object.const_defined? interface_constant.to_s
|
22
|
+
#--------------------------------------------------------------------------
|
23
|
+
# check if definitions exist
|
24
|
+
#--------------------------------------------------------------------------
|
25
|
+
# instance methods
|
26
|
+
interface_instance_methods = Object.const_get(interface_constant.to_s).instance_methods false
|
27
|
+
implementation_instance_methods = Object.const_get(implementation_class.to_s).instance_methods false
|
28
|
+
unimplemented_instance_methods = interface_instance_methods - implementation_instance_methods
|
29
|
+
# static methods
|
30
|
+
interface_static_methods = Object.const_get(interface_constant.to_s).methods false
|
31
|
+
implementation_static_methods = Object.const_get(implementation_class.to_s).methods false
|
32
|
+
unimplemented_static_methods = interface_static_methods - implementation_static_methods
|
33
|
+
# constants
|
34
|
+
interface_constants = Object.const_get(interface_constant.to_s).constants
|
35
|
+
implementation_constants = Object.const_get(implementation_class.to_s).constants
|
36
|
+
unimplemented_constants = interface_constants - implementation_constants
|
37
|
+
if unimplemented_instance_methods.any? || unimplemented_static_methods.any? || unimplemented_constants.any?
|
38
|
+
error_message = "\nThe interface '#{interface_constant}' was not completely implemented in class '#{implementation_class}'."
|
39
|
+
error_message += "\n - missing instance methods:\n #{unimplemented_instance_methods.join(' ')}" if unimplemented_instance_methods.any?
|
40
|
+
error_message += "\n - missing static methods:\n #{unimplemented_static_methods.join(' ')}" if unimplemented_static_methods.any?
|
41
|
+
error_message += "\n - missing constants:\n #{unimplemented_constants.join(' ')}" if unimplemented_constants.any?
|
42
|
+
raise ClassInterface::ImplementationIncompleteError, error_message
|
43
|
+
end
|
44
|
+
|
45
|
+
#--------------------------------------------------------------------------
|
46
|
+
# check if method parameters count does match on implementation and interface
|
47
|
+
#--------------------------------------------------------------------------
|
48
|
+
interface_instance_methods.each do |method_name|
|
49
|
+
if_params_count_arity = Object.const_get(interface_constant.to_s).new.method(method_name).arity
|
50
|
+
impl_params_count_arity = Object.const_get(implementation_class).new.method(method_name).arity
|
51
|
+
if if_params_count_arity != impl_params_count_arity
|
52
|
+
raise ClassInterface::ImplementationParameterCountError, "Parameters of instance method '#{implementation_class}##{method_name}' do not match with interface '#{interface_constant}##{method_name}' (given #{impl_params_count_arity.abs}, expected #{if_params_count_arity.abs})"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
interface_static_methods.each do |method_name|
|
56
|
+
if_params_count_arity = Object.const_get(interface_constant.to_s).method(method_name).arity
|
57
|
+
impl_params_count_arity = Object.const_get(implementation_class).method(method_name).arity
|
58
|
+
if if_params_count_arity != impl_params_count_arity
|
59
|
+
raise ClassInterface::ImplementationParameterCountError, "Parameters of static method '#{implementation_class}##{method_name}' do not match with interface '#{interface_constant}##{method_name}' (given #{impl_params_count_arity.abs}, expected #{if_params_count_arity.abs})"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
#--------------------------------------------------------------------------
|
64
|
+
# check if class types of CONSTANTS do match
|
65
|
+
#--------------------------------------------------------------------------
|
66
|
+
invalid_const_definitions = []
|
67
|
+
if_const_class_type = nil
|
68
|
+
interface_constants.each do |const_name|
|
69
|
+
if_const_string = "%s::%s" % [interface_constant, const_name]
|
70
|
+
if_const_class_type = Object.const_get(if_const_string)
|
71
|
+
if_const_class_type = Object.const_get(if_const_string).class unless Object.const_get(if_const_string).class == Class
|
72
|
+
invalid_const_definitions += [const_name] unless if_const_class_type.class == Class
|
73
|
+
next if if_const_class_type.to_s == 'NilClass'
|
74
|
+
impl_const_value_type = Object.const_get("%s::%s" % [implementation_class, const_name]).class
|
75
|
+
ruby_version_lt_24 = (1.class.to_s == 'Fixnum')
|
76
|
+
ruby_version_gte_24 = (1.class.to_s == 'Integer')
|
77
|
+
# Ruby < 2.4
|
78
|
+
if ruby_version_lt_24 && if_const_class_type.to_s == 'Numeric' && %w[Fixnum Float Bignum].include?(impl_const_value_type.to_s)
|
79
|
+
# we are fine, specific case for numbers
|
80
|
+
# Ruby => 2.4
|
81
|
+
elsif ruby_version_gte_24 && if_const_class_type.to_s == 'Numeric' && %w[Integer Float].include?(impl_const_value_type.to_s)
|
82
|
+
# we are fine, specific case for numbers
|
83
|
+
elsif ruby_version_lt_24 && ((if_const_class_type.to_s == 'Bignum' && impl_const_value_type.to_s == 'Integer') ||
|
84
|
+
(if_const_class_type.to_s == 'Fixnum' && impl_const_value_type.to_s == 'Integer') ||
|
85
|
+
(if_const_class_type.to_s == 'Integer' && impl_const_value_type.to_s == 'Fixnum') ||
|
86
|
+
(if_const_class_type.to_s == 'Integer' && impl_const_value_type.to_s == 'Bignum'))
|
87
|
+
# Ruby 2.4 unifies Fixnum and Bignum into Integer
|
88
|
+
elsif if_const_class_type != impl_const_value_type
|
89
|
+
raise ClassInterface::ImplementationConstantTypeError, "Value type of constant '#{implementation_class}::#{const_name}' does not match interface '#{if_const_string}'. (#{impl_const_value_type} given, #{if_const_class_type} expected)"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
raise ClassInterface::InterfaceConstantDefinitionError, "Value of constant(s) '#{invalid_const_definitions.join(" ")}' of interface '#{interface_constant}' must be a class constant or nil" if invalid_const_definitions.any?
|
93
|
+
else
|
94
|
+
raise ClassInterface::InterfaceNotDefinedError, "Interface '#{interface_constant}' is not defined"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
# extracts the (module) class name of the caller
|
100
|
+
# e.g. ModuleName::ClassName
|
101
|
+
#
|
102
|
+
# @param caller_locations [Array<Thread::Backtrace::Location>] array containing the result of caller_locations
|
103
|
+
# @return [String] full specified constant class name
|
104
|
+
def extract_caller_path(caller_locations)
|
105
|
+
path_elements = []
|
106
|
+
caller_locations.each do |el|
|
107
|
+
if el.label.start_with?('<class:') || el.label.start_with?('<module:')
|
108
|
+
name = el.label.gsub(/\<class:|\<module:/, '')[0...-1]
|
109
|
+
path_elements.push name
|
110
|
+
end
|
111
|
+
end
|
112
|
+
path_elements.reverse.join("::")
|
109
113
|
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module ClassInterface
|
2
|
-
VERSION = '0.1
|
3
|
-
end
|
1
|
+
module ClassInterface
|
2
|
+
VERSION = '1.0.1'.freeze
|
3
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module ClassInterface
|
2
|
-
class ImplementationConstantTypeError < StandardError
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module ClassInterface
|
2
|
+
class ImplementationConstantTypeError < StandardError
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module ClassInterface
|
2
|
-
class ImplementationIncompleteError < StandardError
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module ClassInterface
|
2
|
+
class ImplementationIncompleteError < StandardError
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module ClassInterface
|
2
|
-
class ImplementationParameterCountError < StandardError
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module ClassInterface
|
2
|
+
class ImplementationParameterCountError < StandardError
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module ClassInterface
|
2
|
-
class InterfaceConstantDefinitionError < StandardError
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module ClassInterface
|
2
|
+
class InterfaceConstantDefinitionError < StandardError
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module ClassInterface
|
2
|
-
class InterfaceNotDefinedError < StandardError
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module ClassInterface
|
2
|
+
class InterfaceNotDefinedError < StandardError
|
3
|
+
end
|
4
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class_interface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthäus Beyrle
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 12.3.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 12.3.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description:
|
55
|
+
description:
|
56
56
|
email:
|
57
57
|
- class_interface.gemspec@mail.magynhard.de
|
58
58
|
executables: []
|
@@ -82,7 +82,7 @@ licenses:
|
|
82
82
|
- MIT
|
83
83
|
metadata:
|
84
84
|
allowed_push_host: https://rubygems.org
|
85
|
-
post_install_message:
|
85
|
+
post_install_message:
|
86
86
|
rdoc_options: []
|
87
87
|
require_paths:
|
88
88
|
- lib
|
@@ -97,9 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
|
101
|
-
|
102
|
-
signing_key:
|
100
|
+
rubygems_version: 3.0.8
|
101
|
+
signing_key:
|
103
102
|
specification_version: 4
|
104
103
|
summary: Extends Ruby to support class interfaces
|
105
104
|
test_files: []
|