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) ⇒ Method

Returns a new instance of Method.



7
8
9
10
11
# File 'lib/markdown_ruby_documentation/method.rb', line 7

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

Instance Attribute Details

#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



45
46
47
48
49
50
51
# File 'lib/markdown_ruby_documentation/method.rb', line 45

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) ⇒ 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)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/markdown_ruby_documentation/method.rb', line 19

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

Instance Method Details

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



35
36
37
# File 'lib/markdown_ruby_documentation/method.rb', line 35

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

#contextClass

Returns:

  • (Class)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/markdown_ruby_documentation/method.rb', line 59

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

#context_nameObject



72
73
74
75
76
77
78
# File 'lib/markdown_ruby_documentation/method.rb', line 72

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

#hashObject



41
42
43
# File 'lib/markdown_ruby_documentation/method.rb', line 41

def hash
  @method_reference.hash
end

#inspectString

Returns:

  • (String)


91
92
93
# File 'lib/markdown_ruby_documentation/method.rb', line 91

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

#nameSymbol

Returns:

  • (Symbol)


81
82
83
# File 'lib/markdown_ruby_documentation/method.rb', line 81

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

#to_procProc

Returns:

  • (Proc)


96
97
98
# File 'lib/markdown_ruby_documentation/method.rb', line 96

def to_proc
  context.public_send(type, name)
end

#to_sString

Returns:

  • (String)


86
87
88
# File 'lib/markdown_ruby_documentation/method.rb', line 86

def to_s
  method_reference
end

#typeObject

Raises:

  • (NotImplementedError)


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

def type
  raise NotImplementedError
end

#type_symbolString

Returns:

  • (String)


54
55
56
# File 'lib/markdown_ruby_documentation/method.rb', line 54

def type_symbol
  self.class.type_symbol
end