Class: Peeky::MethodInfo

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/peeky/method_info.rb

Overview

Method info store a list of instance methods and attr_* for a ruby class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, target_instance = nil) ⇒ MethodInfo

TODO: target_instance is required…


26
27
28
29
30
31
32
33
34
# File 'lib/peeky/method_info.rb', line 26

def initialize(method, target_instance = nil)
  @focal_method = method
  @parameters = ParameterInfo.from_method(method)
  # stage 1
  # @implementation_type = :method

  # stage 2
  @implementation_type = infer_implementation_type(target_instance)
end

Instance Attribute Details

#focal_methodObject (readonly)

MethodInfo delegates to the underlying ruby method object


15
16
17
# File 'lib/peeky/method_info.rb', line 15

def focal_method
  @focal_method
end

#implementation_typeObject (readonly)

Implementation type indicates the probable representation of this method in ruby, was it `def method` or `attr_reader` / `attr_writer`


23
24
25
# File 'lib/peeky/method_info.rb', line 23

def implementation_type
  @implementation_type
end

#parametersObject

List of parameters for this method


12
13
14
# File 'lib/peeky/method_info.rb', line 12

def parameters
  @parameters
end

Instance Method Details

#clean_nameObject

Stage 2 (working out) attr_accessor Name of method minus writer annotations Example :writable_attribute= becomes :writable_attribute


39
40
41
42
43
44
# File 'lib/peeky/method_info.rb', line 39

def clean_name
  @clean_name ||= begin
    n = name.to_s
    n.end_with?('=') ? n.delete_suffix('=').to_sym : name
  end
end

#debugObject


78
79
80
81
82
83
# File 'lib/peeky/method_info.rb', line 78

def debug
  puts '-' * 70
  puts name
  puts '-' * 70
  parameters.each(&:debug)
end

#infer_implementation_type(target_instance) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/peeky/method_info.rb', line 46

def infer_implementation_type(target_instance)
  if target_instance.nil?
    :method
  elsif match(target_instance, Peeky::Predicates::AttrReaderPredicate)
    :attr_reader
  elsif match(target_instance, Peeky::Predicates::AttrWriterPredicate)
    :attr_writer
  else
    :method
  end
end

#match(target_instance, predicate) ⇒ Object


58
59
60
# File 'lib/peeky/method_info.rb', line 58

def match(target_instance, predicate)
  predicate.new.match(target_instance, self)
end

#method?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/peeky/method_info.rb', line 62

def method?
  @implementation_type == :method
end

#readable?Boolean

github.com/rubyide/vscode-ruby/issues/454 if I prefix these methods with attr_ then will get an issue in the language server. Cannot read property 'namedChildren' of undefined

Returns:

  • (Boolean)

70
71
72
# File 'lib/peeky/method_info.rb', line 70

def readable?
  @implementation_type == :attr_reader
end

#writable?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'lib/peeky/method_info.rb', line 74

def writable?
  @implementation_type == :attr_writer
end