Class: Hasta::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/hasta/filter.rb

Overview

The filter that is used to drop unwanted lines from input files

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*accept_regexes) ⇒ Filter

Returns a new instance of Filter.



18
19
20
# File 'lib/hasta/filter.rb', line 18

def initialize(*accept_regexes)
  @accept_regexes = Set.new(accept_regexes)
end

Class Method Details

.from_file(file) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/hasta/filter.rb', line 8

def self.from_file(file)
  if lines = File.read(file)
    Hasta.logger.debug "Loading data filter file: #{File.expand_path(file)}"
    new(*lines.split("\n").map { |line| Regexp.new(line) })
  end
rescue => ex
  raise ConfigurationError.new,
    "Failed to load filter configuration file: #{file} - #{ex.message}"
end

Instance Method Details

#include?(line) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/hasta/filter.rb', line 22

def include?(line)
  to_proc.call(line)
end

#to_procObject



26
27
28
# File 'lib/hasta/filter.rb', line 26

def to_proc
  @proc ||= Proc.new { |line| !!(accept_regexes.find { |regex| line =~ regex }) }
end

#to_sObject



30
31
32
# File 'lib/hasta/filter.rb', line 30

def to_s
  "#<#{self.class.name}:#{accept_regexes.to_a.inspect}>"
end