Class: Clr::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/clr/search.rb

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearch

Returns a new instance of Search.



11
12
13
# File 'lib/clr/search.rb', line 11

def initialize
  @results = Array.new
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



3
4
5
# File 'lib/clr/search.rb', line 3

def results
  @results
end

Instance Method Details

#perform(file, pattern) ⇒ Object



15
16
17
18
19
20
# File 'lib/clr/search.rb', line 15

def perform(file, pattern)
  text    = file.binread
  matches = text.scan(pattern)
  item    = Item.new(file, pattern, matches)
  results.push(item).last
end

#summaryObject



22
23
24
25
26
27
28
29
# File 'lib/clr/search.rb', line 22

def summary
  results.group_by(&:filepath).each do |filepath, collection|
    # number of markers in file
    occurrences = collection.map(&:matches_count).inject(0,:+)

    yield filepath, occurrences, collection if occurrences > 0
  end
end