Class: FileIndexing::IndexerPatterns

Inherits:
Object
  • Object
show all
Defined in:
lib/file_indexing/indexer_patterns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexer_patterns = nil) ⇒ IndexerPatterns

Returns a new instance of IndexerPatterns.

Parameters:

  • indexer_patterns_str (String)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/file_indexing/indexer_patterns.rb', line 10

def initialize (indexer_patterns = nil)
  Log.debug1 "Initialize index patterns #{indexer_patterns}."
  @positive_patterns = Array.new
  @negative_patterns = Array.new
  # TODO add a test (including empty collections)
  if indexer_patterns
    indexer_patterns.positive_patterns.each do |pattern|
      add_pattern(pattern)
    end
    indexer_patterns.negative_patterns.each do |pattern|
      add_pattern(pattern, false)
    end
  end
end

Instance Attribute Details

#negative_patternsObject (readonly)

Returns the value of attribute negative_patterns.



7
8
9
# File 'lib/file_indexing/indexer_patterns.rb', line 7

def negative_patterns
  @negative_patterns
end

#positive_patternsObject (readonly)

Returns the value of attribute positive_patterns.



7
8
9
# File 'lib/file_indexing/indexer_patterns.rb', line 7

def positive_patterns
  @positive_patterns
end

Instance Method Details

#add_pattern(pattern, is_positive = true) ⇒ Object

Parameters:

  • pattern (String)
  • is_positive (true) (defaults to: true)
  • is_positive (false) (defaults to: true)


40
41
42
43
44
45
46
47
# File 'lib/file_indexing/indexer_patterns.rb', line 40

def add_pattern(pattern, is_positive = true)
  pattern.gsub!(/\\/,'/')
  if (is_positive)
    @positive_patterns << pattern
  else
    @negative_patterns << pattern
  end
end

#parse_from_file(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/file_indexing/indexer_patterns.rb', line 49

def parse_from_file(file)
  input_patterns = IO.readlines(file)
  begin
    Log.debug1 "Error loading patterns=%s" % file
    raise IOError("Error loading patterns=%s" % file)
  end unless not input_patterns.nil?

  input_patterns.each do |pattern|
    if (m = /^\s*([+-]):(.*)/.match(pattern))
      add_pattern(m[2], m[1].eql?('+') ? true : false)
    elsif (not /^\s*[\/\/|#]/.match(pattern))   # not a comment
      Log.debug1 "pattern in incorrect format: #{pattern}"
      raise RuntimeError("pattern in incorrect format: #{pattern}")
    end
  end
end

#serializeObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/file_indexing/indexer_patterns.rb', line 25

def serialize
  # TODO add a test (including empty collections)
  indexer_patterns = IndexerPatternsMessage.new
  positive_patterns.each do |pattern|
    indexer_patterns.positive_patterns << pattern
  end
  negative_patterns.each do |pattern|
    indexer_patterns.negative_patterns << pattern
  end
  indexer_patterns
end

#sizeObject



66
67
68
# File 'lib/file_indexing/indexer_patterns.rb', line 66

def size
  return @positive_patterns.size
end