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
  <<~RUBY.strip
    def #{name}#{parameters_definition}
    end
  RUBY
end

#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, <<~ERROR.strip
      #{klass.name} should implement a method with the following signature:
        
      #{definition}
    ERROR
  end

  actual = self.class.parse!(klass.instance_method(name))
  return if eql?(actual)

  raise VerificationError, <<~ERROR
    #{klass.name} should implement a method with the following signature:
        
    #{definition}

    but the actual signature is the following:

    #{actual.definition}
  ERROR
end