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) ⇒ Object
Replace placeholders in a string.
-
#initialize(log) ⇒ RubyPlaceholderExpander
constructor
A new instance of RubyPlaceholderExpander.
-
#prepare_placeholders(time, record, opts) ⇒ Object
Get placeholders as a struct.
Constructor Details
#initialize(log) ⇒ RubyPlaceholderExpander
Returns a new instance of RubyPlaceholderExpander.
168 169 170 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 168 def initialize(log) @log = log end |
Instance Attribute Details
#log ⇒ Object (readonly)
Returns the value of attribute log.
166 167 168 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 166 def log @log end |
#placeholders ⇒ Object (readonly)
Returns the value of attribute placeholders.
166 167 168 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 166 def placeholders @placeholders end |
Instance Method Details
#expand(str) ⇒ Object
Replace placeholders in a string
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 187 def (str) interpolated = str.gsub(/\$\{([^}]+)\}/, '#{\1}') # ${..} => #{..} begin 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 end |
#prepare_placeholders(time, record, opts) ⇒ Object
Get placeholders as a struct
177 178 179 180 181 182 |
# File 'lib/fluent/plugin/out_record_reformer.rb', line 177 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 |