Class: MethodMatch::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/method_match/matcher.rb

Constant Summary collapse

SPEC_MODE =
'spec'
CODE_MODE =
'code'
RUBY_FILETYPE =
".rb"
SPEC_FILE_ENDING =
'_spec.rb'
METHOD_NAME_REGEX =
/def\s(self\.(\w+)|\w+).*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, line) ⇒ Matcher

Returns a new instance of Matcher.



10
11
12
13
14
# File 'lib/method_match/matcher.rb', line 10

def initialize name, line
  @name = NameExtractor.new( name ).final_name
  @line = line
  create_spec_name
end

Instance Attribute Details

#lineObject

Returns the value of attribute line.



3
4
5
# File 'lib/method_match/matcher.rb', line 3

def line
  @line
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/method_match/matcher.rb', line 3

def name
  @name
end

#spec_nameObject

Returns the value of attribute spec_name.



3
4
5
# File 'lib/method_match/matcher.rb', line 3

def spec_name
  @spec_name
end

Instance Method Details

#convert_code_to_spec(name) ⇒ Object



41
42
43
44
45
# File 'lib/method_match/matcher.rb', line 41

def convert_code_to_spec name
  name[0] = SPEC_MODE
  name.last.sub!(RUBY_FILETYPE, SPEC_FILE_ENDING)
  name.join File::SEPARATOR
end

#create_spec_nameObject



32
33
34
35
36
37
38
39
# File 'lib/method_match/matcher.rb', line 32

def create_spec_name
  @spec_name = name.split(File::SEPARATOR)
  @spec_name = if spec?
    name
  else
    convert_code_to_spec name.split(File::SEPARATOR)
  end
end

#get_method_matchObject



47
48
49
50
51
# File 'lib/method_match/matcher.rb', line 47

def get_method_match
  File.readlines(name).first(line).reverse.grep(METHOD_NAME_REGEX).first
rescue LoadError
  return nil
end

#method_nameObject



21
22
23
24
25
26
# File 'lib/method_match/matcher.rb', line 21

def method_name
  return nil if spec?
  @match = get_method_match
  return nil unless @match
  @match.match(METHOD_NAME_REGEX)[1]
end

#modeObject



16
17
18
19
# File 'lib/method_match/matcher.rb', line 16

def mode
  set_mode unless @mode
  @mode
end

#spec?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/method_match/matcher.rb', line 28

def spec?
  mode == SPEC_MODE
end