Class: Cans::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/cans/address.rb

Constant Summary collapse

METHOD_KIND_REGEX =
%r{\.(.)}
MODULE_NAME_REGEX =
/^(.+)\/\.|^(.+)$/
METHOD_NAME_REGEX =
%r{\../(.+)$}

Instance Method Summary collapse

Constructor Details

#initialize(address_string) ⇒ Address

Returns a new instance of Address.



7
8
9
# File 'lib/cans/address.rb', line 7

def initialize(address_string)
  @string = address_string
end

Instance Method Details

#method_kindObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cans/address.rb', line 17

def method_kind
  md = METHOD_KIND_REGEX.match @string
  raise "No method_kind found" unless md
  return case md[1]
         when 'i'
           :instance
         when 'm'
           :module
         else
           raise 'Unexpected method_kind found'
         end
end

#method_nameObject



30
31
32
33
34
# File 'lib/cans/address.rb', line 30

def method_name
  md = METHOD_NAME_REGEX.match @string
  raise 'No method_name found' unless md
  return md[1].gsub('/','')
end

#module_nameObject



11
12
13
14
15
# File 'lib/cans/address.rb', line 11

def module_name
  md = MODULE_NAME_REGEX.match @string
  raise "No module_name found in #{@string.inspect}" unless md
  return md.captures.detect{ |m| !(m.nil? || m.empty?) }.gsub('/','::')
end

#target_methodObject



40
41
42
43
44
45
46
47
# File 'lib/cans/address.rb', line 40

def target_method
  case method_kind
  when :instance
    target_module.instance_method method_name
  when :module
    target_module.method method_name
  end
end

#target_moduleObject



36
37
38
# File 'lib/cans/address.rb', line 36

def target_module
  eval "::#{module_name}"
end