Class: Dap::Filter::FilterRenameSubkeyMatch

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

Overview

Example below replaces periods with underscores in the names of all keys one level below ‘my_key’ rename_subkey_match my_key ‘.’ ‘_’

Instance Attribute Summary

Attributes included from Base

#name, #opts

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FilterRenameSubkeyMatch

Returns a new instance of FilterRenameSubkeyMatch.



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

def initialize(args)
  super
  fail "Expected 3 arguments to '#{self.name}' but got #{args.size}" unless args.size == 3
  self.opts = args
end

Instance Method Details

#process(doc) ⇒ Object



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

def process(doc)
  temp_field = {}
  field, original, updated = self.opts
  return [ doc ] unless doc[field].is_a?(::Hash)
  doc[field].each_key do |k|
    new_k = k.gsub(original, updated)
    temp_field[new_k] = doc[field][k]
  end
  doc[field] = temp_field
  [ doc ]
end