Class: Filecount::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/filecount/counter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Counter

Returns a new instance of Counter.



5
6
7
8
9
# File 'lib/filecount/counter.rb', line 5

def initialize(options)
  @options = options
  @file_count = 0
  @start_dir
end

Instance Attribute Details

#file_countObject

Returns the value of attribute file_count.



4
5
6
# File 'lib/filecount/counter.rb', line 4

def file_count
  @file_count
end

#start_dirObject

Returns the value of attribute start_dir.



4
5
6
# File 'lib/filecount/counter.rb', line 4

def start_dir
  @start_dir
end

Class Method Details

.execute(options) ⇒ Object



11
12
13
# File 'lib/filecount/counter.rb', line 11

def self.execute(options)
  new(options).execute
end

Instance Method Details

#count(directory) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/filecount/counter.rb', line 29

def count (directory)
  Dir.foreach(directory) do |file|
    if file != '.' && file != '..' then
      if @options[:dotfiles] == nil && file[0].chr == "." && file != '.git'
        next
      end
      f = "#{directory}/#{file}"

      if File.directory?(f)
        if @options[:count_git_dir] == nil && file == '.git' 
          next
        end
        count(f)
      else
        if @options[:verbose]
          puts f
        end
        @file_count += 1
      end
    end
  end
end

#executeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/filecount/counter.rb', line 15

def execute()
  if ARGV[0] != nil and File.directory?(ARGV[0])
    @start_dir = File.expand_path(ARGV[0])
    count(ARGV[0])
  elsif ARGV[0] != nil
    puts "Directory '#{ARGV[0]}' not found"
    exit
  else
    @start_dir = File.expand_path(".")
    count(".")
  end
  @file_count
end