class_interface 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
+ SHA256:
3
+ metadata.gz: 9e465ca44514991917f4a749a1dca839aa52ca7ce0e8c2d00c5d8f0ba319e402
4
+ data.tar.gz: a53c602ca939703b9f2ecff58d34176d6b9b101ab8cfd587a3e071086d57e35a
5
+ SHA512:
6
+ metadata.gz: 7519f9f2f4959c21aa41becf47b68bb5c4a52d95fd9959968d60432c64a4d3108cf63f36561d50ffa5c8b95fafd1cd5c5055c82faa91dfa69731b95cbda80c21
7
+ data.tar.gz: b9a57035bcf5f05fa76bc064daa4a5b87b147c3575b16c62a41453448a2369317374d42a847438fb3472c29f51bc889bce4267b8edda6d1fb3b79616c9cf62ba
data/.gitignore ADDED
@@ -0,0 +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
15
+ .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.3.3
5
+ before_install: gem install bundler -v 1.14.6
@@ -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 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 ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in class_interface.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +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.
data/README.md ADDED
@@ -0,0 +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
+
data/Rakefile ADDED
@@ -0,0 +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
+
11
+ RSpec::Core::RakeTask.new(:spec)
data/bin/console ADDED
@@ -0,0 +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__)
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,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', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
+ end
@@ -0,0 +1,109 @@
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 < 2.4
76
+ if defined?(Bignum) && if_const_class_type == Numeric && [Fixnum, Float, Bignum].include?(impl_const_value_type)
77
+ # we are fine, specific case for numbers
78
+ # Ruby => 2.4
79
+ elsif defined?(Integer) && if_const_class_type == Numeric && [Integer, Float].include?(impl_const_value_type)
80
+ # we are fine, specific case for numbers
81
+ elsif defined?(Bignum) && ((if_const_class_type == Bignum && impl_const_value_type == Integer) ||
82
+ (if_const_class_type == Fixnum && impl_const_value_type == Integer))
83
+ # Ruby 2.4 unifies Fixnum and Bignum into Integer
84
+ elsif if_const_class_type != impl_const_value_type
85
+ 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)"
86
+ end
87
+ end
88
+ 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?
89
+ else
90
+ raise ClassInterface::InterfaceNotDefinedError, "Interface '#{interface_constant}' is not defined"
91
+ end
92
+ end
93
+
94
+
95
+ # extracts the (module) class name of the caller
96
+ # e.g. ModuleName::ClassName
97
+ #
98
+ # @param caller_locations [Array<Thread::Backtrace::Location>] array containing the result of caller_locations
99
+ # @return [String] full specified constant class name
100
+ def extract_caller_path(caller_locations)
101
+ path_elements = []
102
+ caller_locations.each do |el|
103
+ if el.label.start_with?('<class:') || el.label.start_with?('<module:')
104
+ name = el.label.gsub(/\<class:|\<module:/, '')[0...-1]
105
+ path_elements.push name
106
+ end
107
+ end
108
+ path_elements.reverse.join("::")
109
+ end
@@ -0,0 +1,3 @@
1
+ module ClassInterface
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,4 @@
1
+ module ClassInterface
2
+ class ImplementationConstantTypeError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ClassInterface
2
+ class ImplementationIncompleteError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ClassInterface
2
+ class ImplementationParameterCountError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ClassInterface
2
+ class InterfaceConstantDefinitionError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ClassInterface
2
+ class InterfaceNotDefinedError < StandardError
3
+ end
4
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_interface
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthäus Beyrle
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-14 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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:
56
+ email:
57
+ - class_interface.gemspec@mail.magynhard.de
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
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - class_interface.gemspec
73
+ - lib/class_interface.rb
74
+ - lib/class_interface/version.rb
75
+ - lib/custom_errors/implementation_constant_type_error.rb
76
+ - lib/custom_errors/implementation_incomplete_error.rb
77
+ - lib/custom_errors/implementation_parameter_count_error.rb
78
+ - lib/custom_errors/interface_constant_definition_error.rb
79
+ - lib/custom_errors/interface_not_defined_error.rb
80
+ homepage: https://github.com/magynhard/class_interface
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.7.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Extends Ruby to support class interfaces
105
+ test_files: []