Class: CodeStats::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/code_stats/project.rb

Constant Summary collapse

AVAILIABLE_OPTIONS =
[:spec_filter, :skip_filter]
DEFAUL_SPEC_FILTER =
/\/tests?\/|\/specs?\//
DEFAUL_SKIP_FILTER =
/\/docs?\/|\/(s|ex)amples?\/|\/guides?\/|\/.git\/|\/tmp\/|\/old\/|database\.php|\/vendor\//

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Project

Returns a new instance of Project.


9
10
11
12
13
14
15
# File 'lib/code_stats/project.rb', line 9

def initialize path, options = {}
  @path, @name = path, path.to_entry.name
  options.validate_options! *AVAILIABLE_OPTIONS
  @spec_filter = options.include?(:spec_filter) ? options[:spec_filter] : DEFAUL_SPEC_FILTER
  @skip_filter = options.include?(:skip_filter) ? options[:skip_filter] : DEFAUL_SKIP_FILTER
  clear
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


2
3
4
# File 'lib/code_stats/project.rb', line 2

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


2
3
4
# File 'lib/code_stats/project.rb', line 2

def path
  @path
end

#skip_filterObject (readonly)

Returns the value of attribute skip_filter


2
3
4
# File 'lib/code_stats/project.rb', line 2

def skip_filter
  @skip_filter
end

#sourcesObject (readonly)

Returns the value of attribute sources


3
4
5
# File 'lib/code_stats/project.rb', line 3

def sources
  @sources
end

#spec_filterObject (readonly)

Returns the value of attribute spec_filter


2
3
4
# File 'lib/code_stats/project.rb', line 2

def spec_filter
  @spec_filter
end

#specsObject (readonly)

Returns the value of attribute specs


3
4
5
# File 'lib/code_stats/project.rb', line 3

def specs
  @specs
end

#unknown_extensionsObject (readonly)

Returns the value of attribute unknown_extensions


2
3
4
# File 'lib/code_stats/project.rb', line 2

def unknown_extensions
  @unknown_extensions
end

Instance Method Details

#analyze!Object


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/code_stats/project.rb', line 28

def analyze!
  clear
  files do |file|
    ext = file.extension.to_sym
    if CodeStats.know?(ext)
      warn("file #{file} is too big, skipping") and next if file.size > CodeStats.file_size_limit
      script = CodeStats.parse file.read.force_utf8_encoding, ext
      source?(file) ? sources.add(script) : specs.add(script)
    else
      unknown_extensions << ext unless unknown_extensions.include? ext
    end
  end
end

#files(&b) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/code_stats/project.rb', line 17

def files &b
  files = []
  path.to_dir.files '**/*.*' do |file|
    next if skip?(file)
    b ? b.call(file) : files.push(file)
  end
  b ? nil : files
end