Class: Octocounter::Counter
- Inherits:
-
Object
- Object
- Octocounter::Counter
- Includes:
- Commander::Methods
- Defined in:
- lib/octocounter/counter.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(path) ⇒ Counter
constructor
A new instance of Counter.
- #print_to_screen ⇒ Object
Constructor Details
#initialize(path) ⇒ Counter
Returns a new instance of Counter.
8 9 10 11 |
# File 'lib/octocounter/counter.rb', line 8 def initialize(path) path = "#{path}/" unless path.match(/\/$/) @path = path end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/octocounter/counter.rb', line 6 def path @path end |
Instance Method Details
#calculate ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/octocounter/counter.rb', line 13 def calculate list = [] Dir.glob(path + "**/*").select { |f| File.file?(f) }.each do |file| file_path = File.open(file).path if matched = list.find { |item| FileUtils.compare_file(item[:file], file) } matched[:count] += 1 matched[:files] = "#{matched[:files]}\n#{file_path}" else list.push(files: file_path, file: file, count: 1) end end list end |
#print_to_screen ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/octocounter/counter.rb', line 27 def print_to_screen rows = calculate.map do |item| content = File.open(item[:file]).read.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')[0..100] content = content.scan(/.{40}/).join("\n") [item[:files], content, item[:count]] end puts Terminal::Table.new headings: ["Files", "Content", "Count"], rows: rows, style: { all_separators: true } end |