lib/semlogr/templates/property_token.rb in semlogr-0.3.0 vs lib/semlogr/templates/property_token.rb in semlogr-0.3.1

- old
+ new

@@ -1,39 +1,60 @@ +# frozen_string_literal: true + require 'semlogr/formatters/property_value_formatter' +require 'semlogr/self_logger' module Semlogr module Templates class PropertyToken - attr_accessor :property_name + attr_reader :raw_text, :property_name, :format_string - def initialize(raw_text, property_name) + def initialize(raw_text, property_name, format = nil) @raw_text = raw_text @property_name = property_name + @format_string = format ? "%#{format}" : nil end def render(output, properties) - if properties.key?(@property_name) - property_value = properties[@property_name] + output << + if properties.key?(property_name) + format_property_value(properties[property_name]) + else + raw_text + end + rescue StandardError => e + SelfLogger.error("Failed to render property token: #{property_name}", e) - Formatters::PropertyValueFormatter.format(output, property_value) - else - output << @raw_text - end + output << raw_text end def ==(other) return false unless other + return false unless other.respond_to?(:raw_text) return false unless other.respond_to?(:property_name) + return false unless other.respond_to?(:format_string) - @property_name == other.property_name + raw_text == other.raw_text && \ + property_name == other.property_name && \ + format_string == other.format_string end def eql?(other) self == other end def hash - @property_name.hash + [raw_text, property_name, format_string].hash + end + + private + + def format_property_value(property_value) + if format_string + format(format_string, property_value) + else + Formatters::PropertyValueFormatter.format(property_value) + end end end end end