Sha256: afbda320ff8a29d36bdebcfbfebfdd3a038a4ee2a85a9601bc5e7cfc2936b93c

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Trifle
  module Logger
    module Tracer
      class Hash
        attr_accessor :key, :meta, :data, :tags, :state

        def initialize(key:, meta: nil)
          @key = key
          @meta = meta
          @data = []
          @tags = []
          @state = :success
          @result_prefix = '=> '

          trace("Trifle::Trace has been initialized for #{key}")
        end

        def keys
          parts = key.split('/')
          parts.count.times.map { |i| parts[0..i].join('/') }
        end

        def trace(message, state: :success, head: false)
          result = yield if block_given?
        rescue => e # rubocop:disable Style/RescueStandardError
          raise e
        ensure
          dump_message(
            message,
            head: head, state: block_given? && result.nil? || e ? :error : state
          )
          dump_result(result) if block_given?
          result
        end

        def dump_message(message, head:, state:)
          @data << {
            at: now, message: message,
            state: state, head: head, meta: false
          }
        end

        def dump_result(result)
          @data << {
            at: now, message: "#{@result_prefix}#{result.inspect}",
            state: :success, head: false, meta: true
          }
        end

        def now
          Time.now.to_i
        end

        def tag(tag)
          @tags << tag
        end

        def fail!
          @state = :error
        end

        def success?
          @state == :success
        end

        def wrapup
          Trifle::Logger.default.on_wrapup(self)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trifle-logger-0.0.2 lib/trifle/logger/tracer/hash.rb
trifle-logger-0.0.1 lib/trifle/logger/tracer/hash.rb