Class: XRT::Command::LCS

Inherits:
Object
  • Object
show all
Defined in:
lib/xrt/command/lcs.rb

Instance Method Summary collapse

Instance Method Details

#collect(*files) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xrt/command/lcs.rb', line 22

def collect(*files)
  statements_hash = {}

  files.each{|file|
    next unless File.file? file
    statements(file).each{|statement|
      statements_hash[statement] ||= []
      statements_hash[statement] << file
    }
  }

  statements_hash.each_pair.map{|k, v|
    {
      code: k,
      locations: v.sort,
      count: v.length,
      mass: k.length * v.length,
    }
  }.delete_if{|statement|
    statement[:count] == 1
  }.sort_by{|statement|
    statement[:mass]
  }.reverse
end

#execute(*files) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/xrt/command/lcs.rb', line 6

def execute(*files)
  lcs = collect(*files)
  if lcs.length > 0
    report = lcs.map{|statement|
      header = "### Seen #{statement[:count]} times, size: #{statement[:code].length}, mass: #{statement[:mass]}"
      list = statement[:locations].map{|location| "- #{location}" }.join("\n")
      quoted_code = ['```html', statement[:code], '```'].join("\n")
      [header, quoted_code, list].join("\n\n")
    }
    puts report.join("\n\n")
    true
  else
    false
  end
end

#statements(file) ⇒ Object



47
48
49
# File 'lib/xrt/command/lcs.rb', line 47

def statements(file)
  XRT::Parser.new(open(file).read).document.find_blocks.map{|s| s.auto_indent }
end