Class: Dap::Filter::FilterFieldReplace

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/dap/filter/simple.rb

Direct Known Subclasses

FilterFieldReplaceAll

Instance Attribute Summary collapse

Attributes included from Base

#name, #opts

Instance Method Summary collapse

Constructor Details

#initialize(args, all = false) ⇒ FilterFieldReplace

Returns a new instance of FilterFieldReplace.



48
49
50
51
52
53
54
55
# File 'lib/dap/filter/simple.rb', line 48

def initialize(args, all=false)
  self.all = all
  super(args)
  missing_replace = self.opts.select { |k, v| v.nil? }.keys
  unless missing_replace.empty?
    fail "Missing search/replace for #{missing_replace.join(',')}"
  end
end

Instance Attribute Details

#allObject

Returns the value of attribute all.



46
47
48
# File 'lib/dap/filter/simple.rb', line 46

def all
  @all
end

Instance Method Details

#process(doc) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dap/filter/simple.rb', line 57

def process(doc)
  self.opts.each_pair do |k,v|
    if doc.has_key?(k)
      search, replace = v.split('=', 2)
      search = Regexp.new(search)
      if self.all
        doc[k] = doc[k].gsub(search, replace)
      else
        doc[k] = doc[k].sub(search, replace)
      end
    end
end
 [ doc ]
end