Sha256: 8b96add9dc07f7874901ee8f2239811c077e9097cb5baa7b630cae4bf6122575

Contents?: true

Size: 1.46 KB

Versions: 116

Compression:

Stored size: 1.46 KB

Contents

#
#
module Statistics
  
  #
  #
  class Count
    
    #
    #
    def initialize *patterns
      @patterns = patterns
      
      @count = 0
    end
    
    # Calculate the value starting from the given byte offset. 
    #
    def from filename, options = {}
      @patterns.inject(0) do |total, pattern|
        total + count(pattern, filename, options)
      end
    end
    
    # Calculate and reset the value starting from the given byte offset.
    #
    def reset_from filename, options = {}
      @count = from filename, options
    end
    
    # Calculate and add the value starting from the given byte offset.
    #
    def add_from filename, options = {}
      @count += from filename, options
    end
    
    # Count the pattern.
    #
    def count pattern, filename, options = {}
      extended       = options[:extended] ? '-E' : '-G'
      nonmatching    = options[:nonmatching] ? '-v' : ''
      count_lines "#{extended} #{nonmatching} -i -s -e \"#{pattern}\"", filename
    end
    
    # Count the amount of lines the pattern matches.
    #
    def count_lines extended_pattern, filename
      # puts "grep -c #{extended_pattern} #{filename}"
      sys_i "grep -c #{extended_pattern} #{filename}"
    end
    
    # Convert the system's response into an integer.
    #
    def sys_i text
      sys(text).to_i
    end
    
    # Run the text on the system.
    #
    def sys text
      `#{text}`.chomp
    end
    
    def to_s
      @count.to_s
    end
    
  end
  
end

Version data entries

116 entries across 116 versions & 1 rubygems

Version Path
picky-statistics-4.5.0 lib/picky-statistics/statistics/count.rb
picky-statistics-4.4.2 lib/picky-statistics/statistics/count.rb
picky-statistics-4.4.1 lib/picky-statistics/statistics/count.rb
picky-statistics-4.4.0 lib/picky-statistics/statistics/count.rb
picky-statistics-4.3.2 lib/picky-statistics/statistics/count.rb
picky-statistics-4.3.1 lib/picky-statistics/statistics/count.rb
picky-statistics-4.3.0 lib/picky-statistics/statistics/count.rb
picky-statistics-4.2.4 lib/picky-statistics/statistics/count.rb
picky-statistics-4.2.3 lib/picky-statistics/statistics/count.rb
picky-statistics-4.2.2 lib/picky-statistics/statistics/count.rb
picky-statistics-4.2.1 lib/picky-statistics/statistics/count.rb
picky-statistics-4.2.0 lib/picky-statistics/statistics/count.rb
picky-statistics-4.1.0 lib/picky-statistics/statistics/count.rb
picky-statistics-4.0.9 lib/picky-statistics/statistics/count.rb
picky-statistics-4.0.8 lib/picky-statistics/statistics/count.rb
picky-statistics-4.0.7 lib/picky-statistics/statistics/count.rb
picky-statistics-4.0.6 lib/picky-statistics/statistics/count.rb
picky-statistics-4.0.5 lib/picky-statistics/statistics/count.rb
picky-statistics-4.0.4 lib/picky-statistics/statistics/count.rb
picky-statistics-4.0.3 lib/picky-statistics/statistics/count.rb