Class: Fluent::RecordReformerOutput::RubyPlaceholderExpander
- Inherits:
-
Object
- Object
- Fluent::RecordReformerOutput::RubyPlaceholderExpander
- Defined in:
- lib/fluent/plugin/out_record_reformer.rb
Defined Under Namespace
Classes: UndefOpenStruct
Instance Attribute Summary collapse
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#placeholders ⇒ Object
readonly
Returns the value of attribute placeholders.
Instance Method Summary collapse
-
#expand(str, force_stringify = false) ⇒ Object
Replace placeholders in a string.
-
#initialize(params) ⇒ RubyPlaceholderExpander
constructor
A new instance of RubyPlaceholderExpander.
-
#prepare_placeholders(time, record, opts) ⇒ Object
Get placeholders as a struct.
Constructor Details
#initialize(params) ⇒ RubyPlaceholderExpander
Returns a new instance of RubyPlaceholderExpander.
237 238 239 240 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 237 def initialize(params) @log = params[:log] @auto_typecast = params[:auto_typecast] end |
Instance Attribute Details
#log ⇒ Object (readonly)
Returns the value of attribute log.
235 236 237 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 235 def log @log end |
#placeholders ⇒ Object (readonly)
Returns the value of attribute placeholders.
235 236 237 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 235 def placeholders @placeholders end |
Instance Method Details
#expand(str, force_stringify = false) ⇒ Object
Replace placeholders in a string
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 257 def (str, force_stringify=false) if @auto_typecast and !force_stringify single_placeholder_matched = str.match(/\A\${([^}]+)}\z/) if single_placeholder_matched code = single_placeholder_matched[1] return eval code, @placeholders.instance_eval { binding } end end 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. log.warn_backtrace nil end |
#prepare_placeholders(time, record, opts) ⇒ Object
Get placeholders as a struct
247 248 249 250 251 252 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 247 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 |