class Fluent::Plugin::RecordModifierFilter::DynamicExpander

Public Class Methods

new(param_key, param_value, prepare_value) click to toggle source
# File lib/fluent/plugin/filter_record_modifier.rb, line 165
      def initialize(param_key, param_value, prepare_value)
        if param_value.include?('${')
          __str_eval_code__ = parse_parameter(param_value)

          # Use class_eval with string instead of define_method for performance.
          # It can't share instructions but this is 2x+ faster than define_method in filter case.
          # Refer: http://tenderlovemaking.com/2013/03/03/dynamic_method_definitions.html
          (class << self; self; end).class_eval <<-EORUBY,  __FILE__, __LINE__ + 1
            def expand(tag, time, record, tag_parts)
              #{__str_eval_code__}
            end
          EORUBY
        else
          @param_value = param_value
        end

        begin
          eval prepare_value if prepare_value
        rescue SyntaxError
          raise ConfigError, "Pass invalid syntax parameter : key = prepare_value, value = #{prepare_value}"
        end

        begin
          # check eval genarates wrong code or not
          expand(nil, nil, nil, nil)
        rescue SyntaxError
          raise ConfigError, "Pass invalid syntax parameter : key = #{param_key}, value = #{param_value}"
        rescue
          # Ignore other runtime errors
        end
      end

Public Instance Methods

expand(tag, time, record, tag_parts) click to toggle source

Default implementation for fixed value. This is overwritten when parameter contains '${xxx}' placeholder

# File lib/fluent/plugin/filter_record_modifier.rb, line 198
def expand(tag, time, record, tag_parts)
  @param_value
end

Private Instance Methods

parse_parameter(value) click to toggle source
# File lib/fluent/plugin/filter_record_modifier.rb, line 204
def parse_parameter(value)
  num_placeholders = value.scan('${').size
  if num_placeholders == 1
    if value.start_with?('${') && value.end_with?('}')
      return value[2..-2]
    else
      "\"#{value.gsub('${', '#{')}\""
    end
  else
    "\"#{value.gsub('${', '#{')}\""
  end
end