Class: MethodMatch::Matcher

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

Constant Summary collapse

SPEC_MODE =
'spec'
CODE_MODE =
'code'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, line) ⇒ Matcher

Returns a new instance of Matcher.



7
8
9
10
11
# File 'lib/method_match/matcher.rb', line 7

def initialize name, line
  @name = NameExtractor.new( name ).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

#create_spec_nameObject



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

def create_spec_name
  @spec_name = @name.split('/')
  if is_spec
    return @spec_name = @name
  end
  @spec_name[0] = SPEC_MODE
  @spec_name[-1].sub!('.rb', '_spec.rb')
  @spec_name = @spec_name.join('/')
end

#get_method_matchObject



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

def get_method_match
  File.readlines(name).first(line).reverse.grep(/\sdef\s/).first
rescue LoadError
  return nil
end

#is_specObject



29
30
31
32
33
34
35
36
# File 'lib/method_match/matcher.rb', line 29

def is_spec
  if @spec_name[0] == SPEC_MODE
    @mode = SPEC_MODE
    return true
  end
  @mode = CODE_MODE
  return false
end

#method_nameObject



18
19
20
21
22
23
# File 'lib/method_match/matcher.rb', line 18

def method_name
  return nil if mode == SPEC_MODE
  @match = get_method_match
  return nil unless @match
  @match.match(/def\s(self\.(\w+)|\w+).*/)[1]
end

#modeObject



13
14
15
16
# File 'lib/method_match/matcher.rb', line 13

def mode
  is_spec unless @mode
  @mode
end

#spec?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/method_match/matcher.rb', line 25

def spec?
  mode == SPEC_MODE
end