Class: Fluent::RecordReformerOutput::RubyPlaceholderExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/out_record_reformer.rb

Defined Under Namespace

Classes: UndefOpenStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ RubyPlaceholderExpander

Returns a new instance of RubyPlaceholderExpander.



209
210
211
# File 'lib/fluent/plugin/out_record_reformer.rb', line 209

def initialize(log)
  @log = log
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



207
208
209
# File 'lib/fluent/plugin/out_record_reformer.rb', line 207

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



207
208
209
# File 'lib/fluent/plugin/out_record_reformer.rb', line 207

def placeholders
  @placeholders
end

Instance Method Details

#expand(str) ⇒ Object

Replace placeholders in a string

Parameters:

  • str (String)

    the string to be replaced



228
229
230
231
232
233
234
235
# File 'lib/fluent/plugin/out_record_reformer.rb', line 228

def expand(str)
  interpolated = str.gsub(/\$\{([^}]+)\}/, '#{\1}') # ${..} => #{..}
  eval "\"#{interpolated}\"", @placeholders.instance_eval { binding }
rescue => e
  log.warn "record_reformer: failed to expand `#{str}`", :error_class => e.class, :error => e.message
  log.warn_backtrace
  nil
end

#prepare_placeholders(time, record, opts) ⇒ Object

Get placeholders as a struct

Parameters:

  • time (Time)

    the time

  • record (Hash)

    the record

  • opts (Hash)

    others



218
219
220
221
222
223
# File 'lib/fluent/plugin/out_record_reformer.rb', line 218

def prepare_placeholders(time, record, opts)
  struct = UndefOpenStruct.new(record)
  struct.time = Time.at(time)
  opts.each {|key, value| struct.__send__("#{key}=", value) }
  @placeholders = struct
end