Class: Inferior::Core::Method
- Inherits:
-
Object
- Object
- Inferior::Core::Method
- Defined in:
- lib/inferior/core/method.rb
Constant Summary collapse
- Error =
Class.new(Error)
- InvalidNameError =
Class.new(Error)
- VerificationError =
Class.new(Error)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Class Method Summary collapse
Instance Method Summary collapse
- #definition ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(name:, parameters:) ⇒ Method
constructor
A new instance of Method.
- #verify_class!(klass) ⇒ Object
Constructor Details
#initialize(name:, parameters:) ⇒ Method
Returns a new instance of Method.
47 48 49 50 |
# File 'lib/inferior/core/method.rb', line 47 def initialize(name:, parameters:) @name = name @parameters = parameters end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
45 46 47 |
# File 'lib/inferior/core/method.rb', line 45 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
45 46 47 |
# File 'lib/inferior/core/method.rb', line 45 def parameters @parameters end |
Class Method Details
.build!(name:, parameters:) ⇒ Object
17 18 19 20 21 |
# File 'lib/inferior/core/method.rb', line 17 def build!(name:, parameters:) validate_name!(name) validate_parameters!(parameters) new(name: name, parameters: parameters) end |
.parse!(method) ⇒ Object
23 24 25 26 |
# File 'lib/inferior/core/method.rb', line 23 def parse!(method) parameters = parse_parameters!(method) build!(parameters: parameters, name: method.name) end |
.parse_parameters!(method) ⇒ Object
28 29 30 31 32 |
# File 'lib/inferior/core/method.rb', line 28 def parse_parameters!(method) method.parameters.map do |ruby_internal_type, ruby_internal_name| Parameter.parse!(ruby_internal_type, ruby_internal_name) end end |
Instance Method Details
#definition ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/inferior/core/method.rb', line 84 def definition parameters_definition = parameters.any? ? "(#{parameters.map(&:definition).join(", ")})" : nil " def \#{name}\#{parameters_definition}\n end\n RUBY\nend\n".strip |
#eql?(other) ⇒ Boolean
52 53 54 55 56 57 58 59 |
# File 'lib/inferior/core/method.rb', line 52 def eql?(other) return false unless name.eql?(other.name) return false unless parameters.size.eql?(other.parameters.size) parameters.each_with_index.all? do |parameter, index| parameter.eql?(other.parameters[index]) end end |
#verify_class!(klass) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/inferior/core/method.rb', line 61 def verify_class!(klass) unless klass.method_defined?(name) raise VerificationError, " \#{klass.name} should implement a method with the following signature:\n \n \#{definition}\n ERROR\n end\n\n actual = self.class.parse!(klass.instance_method(name))\n return if eql?(actual)\n\n raise VerificationError, <<~ERROR\n \#{klass.name} should implement a method with the following signature:\n \n \#{definition}\n\n but the actual signature is the following:\n\n \#{actual.definition}\n ERROR\nend\n".strip |