Class: Franz::InputConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/franz/input_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs, logger = nil) ⇒ InputConfig

Returns a new instance of InputConfig.



5
6
7
8
9
10
11
12
13
14
# File 'lib/franz/input_config.rb', line 5

def initialize configs, logger=nil
  @logger  = logger || Logger.new(STDOUT)
  @configs = configs
  @configs.map! do |c|
    normalized_config c
  end
  @types = Hash.new
  @drop  = Hash.new
  @keep  = Hash.new
end

Instance Attribute Details

#configsObject (readonly)

Returns the value of attribute configs.



3
4
5
# File 'lib/franz/input_config.rb', line 3

def configs
  @configs
end

Instance Method Details

#config(path) ⇒ Object



16
17
18
19
# File 'lib/franz/input_config.rb', line 16

def config path
  t = type(path)
  configs.select { |c| c[:type] == t }.shift
end

#drop?(path, message) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/franz/input_config.rb', line 34

def drop? path, message
  patterns = drops_for(path)
  return true if patterns.nil?
  return false if patterns.empty?
  apply_patterns patterns, message
end

#json?(path) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/franz/input_config.rb', line 21

def json? path
  config(path)[:json?]
rescue
  false
end

#keep?(path, message) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/franz/input_config.rb', line 27

def keep? path, message
  patterns = keeps_for(path)
  return true if patterns.nil?
  return true if patterns.empty?
  apply_patterns patterns, message
end

#type(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/franz/input_config.rb', line 41

def type path
  @types.fetch path
rescue KeyError
  configs.each do |config|
    type = config[:type] if config[:includes].any? { |glob|
      included = File.fnmatch? glob, path, File::FNM_EXTGLOB
      excludes = !config[:excludes].nil?
      excluded = excludes && config[:excludes].any? { |exlude|
        File.fnmatch? exlude, File::basename(path), File::FNM_EXTGLOB
      }
      included && !excluded
    }
    unless type.nil?
      @types[path] = type
      return type
    end
  end
  log.warn \
    event: 'type unknown',
    file: path
  @types[path] = nil
end