Class: DRG::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/drg/scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Scanner

Returns a new instance of Scanner.



7
8
9
# File 'lib/drg/scanner.rb', line 7

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/drg/scanner.rb', line 5

def file
  @file
end

Instance Method Details

#describesObject



61
62
63
# File 'lib/drg/scanner.rb', line 61

def describes
  File.readlines(file).map { |line| $2 if line =~ /(describe|context)\s*['"%](.+)\s*['"%]/ }.compact
end

#indent_size(line) ⇒ Object



65
66
67
# File 'lib/drg/scanner.rb', line 65

def indent_size(line)
  line.to_s.chomp.rstrip[/\s{0,}/].size.to_i
end

#indentationObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/drg/scanner.rb', line 27

def indentation
  line = nil
  File.open(file) do |f|
    previous_line = nil
    until indent_size(previous_line) != indent_size(line = f.gets)
      previous_line = line
    end
  end
  indent_size(line)
end

#letsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/drg/scanner.rb', line 11

def lets
  _lets = []
  continuation = false
  File.readlines(file).each do |line|
    if continuation && continuation = line.strip != 'end'
      _lets[-1].value << line
    elsif line =~ /\A\s+let\(?:(\w+)\)?\s*do\s*\n/
      continuation = true
      _lets << Let.new($1.strip, '')
    elsif line =~ /\A\s+let\(?:(\w+)\)?\s*\{(.+)\}/m
      _lets << Let.new($1.strip, $2.strip)
    end
  end
  _lets
end

#methodsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/drg/scanner.rb', line 38

def methods
  items = []
  klasses = Set.new
  File.readlines(file).each do |line|
    if line =~ /class (\w+)/
      klasses << $1
    end
    case line.to_s.chomp.strip
    when /#?\s*private/i
      break
    when /def (self)?(\.?[a-z0-9_]+)/
      method_name = $2
      if method_name !~ /\A\./
        method_name = "##{method_name}"
      end
      items << "#{klasses.to_a.join('::')}#{method_name}"
    else
      nil
    end
  end
  items
end