Sha256: 7ae3cd6f3573b05874d4374e6f5e9cd0aae890f032109d4f5e8969f0c648594a

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

class TransitionsListener::Stdout
  include BCDD::Result::Transitions::Listener

  def initialize
    @buffer = []
  end

  def on_start(scope:)
    scope => { id:, name:, desc: }

    @buffer << [id, "##{id} #{name} - #{desc}".chomp('- ')]
  end

  def on_record(record:)
    record => { current: { id: }, result: { kind:, type: } }

    method_name = record.dig(:and_then, :method_name)

    @buffer << [id, " * #{kind}(#{type}) from method: #{method_name}".chomp('from method: ')]
  end

  MapNestedMessages = ->(transitions, buffer, hide_given_and_continue) do
    ids_matrix = transitions.dig(:metadata, :ids_matrix)

    messages = buffer.filter_map { |(id, msg)| "#{'   ' * ids_matrix[id].last}#{msg}" if ids_matrix[id] }

    messages.reject! { _1.match?(/\(_(given|continue)_\)/) } if hide_given_and_continue

    messages
  end

  def on_finish(transitions:)
    messages = MapNestedMessages[transitions, @buffer, ENV['HIDE_GIVEN_AND_CONTINUE']]

    puts messages.join("\n")
  end

  def before_interruption(exception:, transitions:)
    messages = MapNestedMessages[transitions, @buffer, ENV['HIDE_GIVEN_AND_CONTINUE']]

    puts messages.join("\n")

    bc = ::ActiveSupport::BacktraceCleaner.new
    bc.add_filter { |line| line.gsub(__dir__.sub('/lib', ''), '').sub(/\A\//, '')}
    bc.add_silencer { |line| /lib\/bcdd\/result/.match?(line) }
    bc.add_silencer { |line| line.include?(RUBY_VERSION) }

    backtrace = bc.clean(exception.backtrace)

    puts "\nException: #{exception.message} (#{exception.class}); Backtrace: #{backtrace.join(", ")}"
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
bcdd-process-0.4.0 examples/business_processes/lib/transitions_listener/stdout.rb
bcdd-process-0.3.1 examples/business_processes/lib/transitions_listener/stdout.rb
bcdd-process-0.3.0 examples/business_processes/lib/transitions_listener/stdout.rb
bcdd-process-0.2.0 examples/business_processes/lib/transitions_listener/stdout.rb
bcdd-result-0.13.0 examples/multiple_listeners/lib/transitions_listener/stdout.rb