Sha256: 661ae9639b096c9dd907fde3054ed32476f7a42141926969b1fdc9811751f1c0

Contents?: true

Size: 1.29 KB

Versions: 26

Compression:

Stored size: 1.29 KB

Contents

require "timber/events/controller_call"
require "timber/events/custom"
require "timber/events/error"
require "timber/events/http_request"
require "timber/events/http_response"
require "timber/events/sql_query"
require "timber/events/template_render"

module Timber
  # Namespace for all Timber supported events.
  module Events
    # Protocol for casting objects into a `Timber::Event`.
    #
    # @example Casting a hash
    #   Timber::Events.build({type: :custom_event, message: "My log message", data: {my: "data"}})
    def self.build(obj)
      if obj.is_a?(::Timber::Event)
        obj
      elsif obj.respond_to?(:to_timber_event)
        obj.to_timber_event
      elsif obj.is_a?(Hash) && obj.key?(:message) && obj.length == 2
        event = obj.select { |k,v| k != :message }
        type = event.keys.first
        data = event.values.first

        Events::Custom.new(
          type: type,
          message: obj[:message],
          data: data
        )
      elsif obj.is_a?(Struct) && obj.respond_to?(:message) && obj.respond_to?(:type)
        Events::Custom.new(
          type: obj.type,
          message: obj.message,
          data: obj.respond_to?(:to_h) ? obj.to_h : Timber::Util::Struct.to_hash(obj) # ruby 1.9.3 does not have to_h :(
        )
      else
        nil
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
timber-2.6.2 lib/timber/events.rb
timber-2.6.1 lib/timber/events.rb
timber-2.6.0 lib/timber/events.rb
timber-2.6.0.pre.beta2 lib/timber/events.rb
timber-2.6.0.pre.beta1 lib/timber/events.rb
timber-2.5.1 lib/timber/events.rb
timber-2.5.0 lib/timber/events.rb
timber-2.4.0 lib/timber/events.rb
timber-2.3.4 lib/timber/events.rb
timber-2.3.3 lib/timber/events.rb
timber-2.3.2 lib/timber/events.rb
timber-2.3.1 lib/timber/events.rb
timber-2.3.0 lib/timber/events.rb
timber-2.2.3 lib/timber/events.rb
timber-2.2.2 lib/timber/events.rb
timber-2.2.1 lib/timber/events.rb
timber-2.2.0 lib/timber/events.rb
timber-2.1.10 lib/timber/events.rb
timber-2.1.9 lib/timber/events.rb
timber-2.1.8 lib/timber/events.rb