Class: MarkdownRubyDocumentation::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_ruby_documentation/method.rb

Direct Known Subclasses

ClassMethod, InstanceMethod, NullMethod

Constant Summary collapse

InvalidMethodReference =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_reference, context: Kernel, visibility: :public, file_path: nil, line_no: nil) ⇒ Method

Returns a new instance of Method.



8
9
10
11
12
13
14
# File 'lib/markdown_ruby_documentation/method.rb', line 8

def initialize(method_reference, context: Kernel, visibility: :public, file_path: nil, line_no: nil)
  @method_reference = method_reference.to_s
  @context          = context
  @visibility       = visibility
  @file_path        = file_path
  @line_no          = line_no
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



5
6
7
# File 'lib/markdown_ruby_documentation/method.rb', line 5

def file_path
  @file_path
end

#line_noObject

Returns the value of attribute line_no.



5
6
7
# File 'lib/markdown_ruby_documentation/method.rb', line 5

def line_no
  @line_no
end

#visibilityObject (readonly)

Returns the value of attribute visibility.



4
5
6
# File 'lib/markdown_ruby_documentation/method.rb', line 4

def visibility
  @visibility
end

Class Method Details

.===(value) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/markdown_ruby_documentation/method.rb', line 48

def self.===(value)
  if value.is_a?(String)
    value.include?(type_symbol) && !!/\A[:A-Za-z_0-9!?#{type_symbol}]+\z/.match(value)
  else
    super
  end
end

.create(method_reference, null_method: false, context: Kernel, visibility: :public, file_path: nil) ⇒ Object

Examples:

".class_method_name" class method in the current scope.
"Constant.class_method_name" class method on a specific constant.
"SomeClass#instance_method_name" an instance method on a specific constant.
"#instance_method_name" an instance method in the current scope.

Parameters:

  • method_reference (String)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/markdown_ruby_documentation/method.rb', line 22

def self.create(method_reference, null_method: false, context: Kernel, visibility: :public, file_path: nil)
  return method_reference if method_reference.is_a?(Method)
  case method_reference
  when InstanceMethod
    InstanceMethod.new(method_reference, context: context, visibility: visibility, file_path: file_path)
  when ClassMethod
    ClassMethod.new(method_reference, context: context, visibility: visibility, file_path: file_path)
  else
    if null_method
      NullMethod.new(method_reference, context: context, visibility: visibility, file_path: file_path)
    else
      raise InvalidMethodReference, "method_reference is formatted incorrectly: '#{method_reference}'"
    end
  end
end

Instance Method Details

#==(other_method) ⇒ Object Also known as: eql?



38
39
40
# File 'lib/markdown_ruby_documentation/method.rb', line 38

def ==(other_method)
  self.class == other_method.class && other_method.method_reference == self.method_reference
end

#contextClass

Returns:

  • (Class)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/markdown_ruby_documentation/method.rb', line 62

def context
  if method_reference.start_with?(type_symbol)
    @context
  else
    constant = method_reference.split(type_symbol).first
    begin
      constant.constantize
    rescue NameError
      @context.const_get(constant)
    end
  end
end

#context_nameObject



75
76
77
78
79
80
81
# File 'lib/markdown_ruby_documentation/method.rb', line 75

def context_name
  if method_reference.start_with?(type_symbol)
    @context.name
  else
    method_reference.split(type_symbol).first
  end
end

#hashObject



44
45
46
# File 'lib/markdown_ruby_documentation/method.rb', line 44

def hash
  @method_reference.hash
end

#inspectString

Returns:

  • (String)


94
95
96
# File 'lib/markdown_ruby_documentation/method.rb', line 94

def inspect
  "#<#{self.class.name} #{to_s}>"
end

#nameSymbol

Returns:

  • (Symbol)


84
85
86
# File 'lib/markdown_ruby_documentation/method.rb', line 84

def name
  method_reference.split(type_symbol).last.try!(:to_sym)
end

#source_locationObject



103
104
105
106
107
108
109
# File 'lib/markdown_ruby_documentation/method.rb', line 103

def source_location
  if file_path && line_no
    [file_path, line_no]
  else
    context.public_send(type, name).source_location
  end
end

#to_procProc

Returns:

  • (Proc)


99
100
101
# File 'lib/markdown_ruby_documentation/method.rb', line 99

def to_proc
  context.public_send(type, name)
end

#to_sString

Returns:

  • (String)


89
90
91
# File 'lib/markdown_ruby_documentation/method.rb', line 89

def to_s
  method_reference
end

#typeObject

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/markdown_ruby_documentation/method.rb', line 111

def type
  raise NotImplementedError
end

#type_symbolString

Returns:

  • (String)


57
58
59
# File 'lib/markdown_ruby_documentation/method.rb', line 57

def type_symbol
  self.class.type_symbol
end