class_interface 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef9514f807928b586c4e94eb5582b3dfda2c4dea838df114e4776d0fbf3c8602
4
- data.tar.gz: b2f3df2804c343c5235dd6f95fb219a5fb686e28fc8ad9c96ef1cbeecb6d0c4c
3
+ metadata.gz: c4663e879bc3f2eb287675c5f43d9bc02bb2ed55edf4762c01055cb1713ca88c
4
+ data.tar.gz: 115024e4b3f42352884d455feedbd4fb5ae64e7a9b303d3f19dcabc432377ab9
5
5
  SHA512:
6
- metadata.gz: 182e09823a2feb7a6f9272442c9f544d997924bbaab5c8144830133c44a1e5904740321fa54fa1800b651dae4c97a635fa8185f7a1b4893e6b8fb264765f4941
7
- data.tar.gz: 1c332c079c03043c360a306f375b0ad5e46dadf4ad438a72d4f4445b5e80ae52437050d2b56b93d884105af8c6a7f6e3a3920215f07b54049b808afd9198e3d6
6
+ metadata.gz: 763bce0e1b1cdbdd8757727c470fc480af32f8a4aa6ab83e8d1da9a4c8007aae458148aebea0d110fe403c2979ce9fe4c4cceb1d31ede7bbc6ab2ef949ae8632
7
+ data.tar.gz: 55d527df06e8eedb0cdc257996b62945780372258f48233d0d26ce95bb3c7e0c1c33e1970a67f1dec010968571891c8af97963319f753eb0def4cb5acb51cf46
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
@@ -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
@@ -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 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.
data/README.md CHANGED
@@ -1,94 +1,94 @@
1
- # Ruby gem class_interface
2
-
3
- Extends Ruby to support class interfaces.
4
-
5
- That can be very handy in teams to declare requirements for specific types of classes.
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'class_interface'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install class_interface
22
-
23
- ## Examples
24
-
25
- ### Defining an interface
26
-
27
- 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, ...`
28
-
29
- ```ruby
30
- class IExample
31
- MIN_AGE = Integer
32
- DEFAULT_ENV = String
33
- SOME_CONSTANT = nil
34
-
35
- def self.some_static_method
36
- end
37
-
38
- def some_instance_method
39
- end
40
- end
41
- ```
42
-
43
- ### Implementing an interface
44
-
45
- ```ruby
46
- class MyImplementation
47
- MIN_AGE = 21
48
- DEFAULT_ENV = 'dev'
49
- SOME_CONSTANT = 'some_value'
50
-
51
- def specific_method
52
- puts "very specific"
53
- end
54
-
55
- def self.some_static_method
56
- puts "static method is implemented!"
57
- end
58
-
59
- def some_instance_method
60
- # implementation
61
- end
62
-
63
- def self.another_methods
64
- # implementation
65
- end
66
-
67
- implements IExample
68
- end
69
- ```
70
-
71
- ## Documentation
72
- ```ruby
73
- #implements(InterfaceClassConstant)
74
- ```
75
-
76
- _InterfaceClassConstant_ must be a valid InterfaceClassConstant or a String, containing a valid InterfaceClassConstant, i.e.: IMyInterface / "IMyInterface"
77
-
78
- ### Methods
79
-
80
- All defined methods in the interface class must be implemented in the implementing class.
81
- The parameter count must be the same.
82
-
83
- ### Constant Types
84
-
85
- All CONSTANTS defined in the interface class must be implemented in the implementing class.
86
- CONSTANTS of interfaces may be defined = nil, to allow all types of definitions in the implementing class.
87
-
88
- To specify a type, assign its class constant, e.g. String, Array, ...
89
- If a specified type is defined, it is mandatory for the implementation to use that type.
90
-
91
- ## Contributing
92
-
93
- 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.
94
-
1
+ # Ruby gem class_interface
2
+
3
+ Extends Ruby to support class interfaces.
4
+
5
+ That can be very handy in teams to declare requirements for specific types of classes.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'class_interface'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install class_interface
22
+
23
+ ## Examples
24
+
25
+ ### Defining an interface
26
+
27
+ 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, ...`
28
+
29
+ ```ruby
30
+ class IExample
31
+ MIN_AGE = Integer
32
+ DEFAULT_ENV = String
33
+ SOME_CONSTANT = nil
34
+
35
+ def self.some_static_method
36
+ end
37
+
38
+ def some_instance_method
39
+ end
40
+ end
41
+ ```
42
+
43
+ ### Implementing an interface
44
+
45
+ ```ruby
46
+ class MyImplementation
47
+ MIN_AGE = 21
48
+ DEFAULT_ENV = 'dev'
49
+ SOME_CONSTANT = 'some_value'
50
+
51
+ def specific_method
52
+ puts "very specific"
53
+ end
54
+
55
+ def self.some_static_method
56
+ puts "static method is implemented!"
57
+ end
58
+
59
+ def some_instance_method
60
+ # implementation
61
+ end
62
+
63
+ def self.another_methods
64
+ # implementation
65
+ end
66
+
67
+ implements IExample
68
+ end
69
+ ```
70
+
71
+ ## Documentation
72
+ ```ruby
73
+ #implements(InterfaceClassConstant)
74
+ ```
75
+
76
+ _InterfaceClassConstant_ must be a valid InterfaceClassConstant or a String, containing a valid InterfaceClassConstant, i.e.: IMyInterface / "IMyInterface"
77
+
78
+ ### Methods
79
+
80
+ All defined methods in the interface class must be implemented in the implementing class.
81
+ The parameter count must be the same.
82
+
83
+ ### Constant Types
84
+
85
+ All CONSTANTS defined in the interface class must be implemented in the implementing class.
86
+ CONSTANTS of interfaces may be defined = nil, to allow all types of definitions in the implementing class.
87
+
88
+ To specify a type, assign its class constant, e.g. String, Array, ...
89
+ If a specified type is defined, it is mandatory for the implementation to use that type.
90
+
91
+ ## Contributing
92
+
93
+ 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.
94
+
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)
@@ -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
@@ -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', '>= 12.3.3'
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
@@ -1,113 +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
- 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("::")
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("::")
113
113
  end
@@ -1,3 +1,3 @@
1
- module ClassInterface
2
- VERSION = '0.1.2'.freeze
3
- end
1
+ module ClassInterface
2
+ VERSION = '0.1.3'.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.2
4
+ version: 0.1.3
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: 2020-03-03 00:00:00.000000000 Z
11
+ date: 2020-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -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
- rubyforge_project:
101
- rubygems_version: 2.7.6.2
102
- signing_key:
100
+ rubygems_version: 3.1.3
101
+ signing_key:
103
102
  specification_version: 4
104
103
  summary: Extends Ruby to support class interfaces
105
104
  test_files: []