lib/timber/events/template_render.rb in timber-2.5.1 vs lib/timber/events/template_render.rb in timber-2.6.0.pre.beta1

- old
+ new

@@ -5,20 +5,26 @@ # The template render event track template renderings and their performance. # # @note This event should be installed automatically through integrations, # such as the {Integrations::ActionView::LogSubscriber} integration. class TemplateRender < Timber::Event + MESSAGE_MAX_BYTES = 8192.freeze + NAME_MAX_BYTES = 1024.freeze + attr_reader :message, :name, :time_ms def initialize(attributes) - @message = attributes[:message] || raise(ArgumentError.new(":message is required")) - @name = attributes[:name] || raise(ArgumentError.new(":name is required")) - @time_ms = attributes[:time_ms] || raise(ArgumentError.new(":time_ms is required")) - @time_ms = @time_ms.round(6) + normalizer = Util::AttributeNormalizer.new(attributes) + @message = normalizer.fetch!(:message, :string, :limit => MESSAGE_MAX_BYTES) + @name = normalizer.fetch!(:name, :string, :limit => NAME_MAX_BYTES) + @time_ms = normalizer.fetch!(:time_ms, :float, :precision => 6) end def to_hash - {name: name, time_ms: time_ms} + @to_hash ||= Util::NonNilHashBuilder.build do |h| + h.add(:name, name) + h.add(:time_ms, time_ms) + end end alias to_h to_hash # Builds a hash representation containing simple objects, suitable for serialization (JSON). def as_json(_options = {}) \ No newline at end of file