Class: AutoTypeDoc::MethodInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_type_doc/method_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_location:) ⇒ MethodInfo

Returns a new instance of MethodInfo.



7
8
9
10
11
# File 'lib/auto_type_doc/method_info.rb', line 7

def initialize(source_location:)
  @source_location = source_location
  @arguments = Set.new
  @return_types = Hash.new(0)
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



3
4
5
# File 'lib/auto_type_doc/method_info.rb', line 3

def arguments
  @arguments
end

#return_typesObject

Returns the value of attribute return_types.



4
5
6
# File 'lib/auto_type_doc/method_info.rb', line 4

def return_types
  @return_types
end

#source_locationObject

Returns the value of attribute source_location.



5
6
7
# File 'lib/auto_type_doc/method_info.rb', line 5

def source_location
  @source_location
end

Instance Method Details

#add_argument(name:, type:, kind:, position:) ⇒ Object



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

def add_argument(name:, type:, kind:, position:)
  if (a = argument(name))
    a.add_type(type)
  else
    a = Argument.new(name: name, kind: kind, position: position)
    a.add_type(type)
    arguments << a
  end
end

#add_return_type(type) ⇒ Object



35
36
37
# File 'lib/auto_type_doc/method_info.rb', line 35

def add_return_type(type)
  return_types[type.to_s] += 1
end

#argument(name) ⇒ Object



21
22
23
# File 'lib/auto_type_doc/method_info.rb', line 21

def argument(name)
  arguments.find { |a| a.name == name.to_s }
end

#most_frequent_arg_type(arg_name) ⇒ String

Returns:

  • (String)


40
41
42
# File 'lib/auto_type_doc/method_info.rb', line 40

def most_frequent_arg_type(arg_name)
  most_frequent_type(argument(arg_name).types)
end

#most_frequent_return_typeString

Returns:

  • (String)


45
46
47
# File 'lib/auto_type_doc/method_info.rb', line 45

def most_frequent_return_type
  most_frequent_type(return_types)
end

#source_fileObject



13
14
15
# File 'lib/auto_type_doc/method_info.rb', line 13

def source_file
  source_location[0]
end

#source_lineObject



17
18
19
# File 'lib/auto_type_doc/method_info.rb', line 17

def source_line
  source_location[1]
end

#to_hObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/auto_type_doc/method_info.rb', line 49

def to_h
  h = {}
  h[:arguments] = arguments.map(&:to_h) if arguments.any?
  h.merge(
    return_types: return_types,
    source_location: {
      path: source_file,
      line: source_line
    }
  )
end

#to_json(*args) ⇒ Object



61
62
63
# File 'lib/auto_type_doc/method_info.rb', line 61

def to_json(*args)
  to_h.to_json(*args)
end