Class: Inferior::Core::Method

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/inferior/core/method.rb', line 45

def name
  @name
end

#parametersObject (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

#definitionObject



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

Returns:

  • (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

Raises:



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