<commit_before>require "declarative"
require "disposable/callback"

module Trailblazer::Operation::Dispatch
  def self.included(base)
    base.extend ClassMethods

    base.extend Declarative::Heritage::Inherited
    base.extend Declarative::Heritage::DSL
  end

  def dispatch!(name=:default, options={ operation: self, contract: contract }) # FIXME: test options.
    config  = self.class.callbacks[name]
    group   = config[:group].new(contract)

    options[:context] ||= (config[:context] == :operation ? self : group)
    group.(options)

    invocations[name] = group
  end

  def invocations
    @invocations ||= {}
  end

  module ClassMethods
    def callbacks
      @callbacks ||= {}
    end

    def callback(name=:default, constant=nil, &block)
      return callbacks[name][:group] unless constant or block_given?

      add_callback(name, constant, &block)
    end

  private
    def add_callback(name, constant, &block)
      heritage.record(:add_callback, name, constant, &block)

      callbacks[name] ||= {
        group:   Class.new(constant || Disposable::Callback::Group),
        context: constant ? nil : :operation # `context: :operation` when the callback is inline. `context: group` otherwise.
      }

      callbacks[name][:group].class_eval(&block) if block_given?
    end
  end
end
<commit_msg>pass params into callback invocations.<commit_after>require "declarative"
require "disposable/callback"

module Trailblazer::Operation::Dispatch
  def self.included(base)
    base.extend ClassMethods

    base.extend Declarative::Heritage::Inherited
    base.extend Declarative::Heritage::DSL
  end

  def dispatch!(name=:default, options={ operation: self, contract: contract, params: @params }) # FIXME: test options.
    config  = self.class.callbacks[name]
    group   = config[:group].new(contract)

    options[:context] ||= (config[:context] == :operation ? self : group)
    group.(options)

    invocations[name] = group
  end

  def invocations
    @invocations ||= {}
  end

  module ClassMethods
    def callbacks
      @callbacks ||= {}
    end

    def callback(name=:default, constant=nil, &block)
      return callbacks[name][:group] unless constant or block_given?

      add_callback(name, constant, &block)
    end

  private
    def add_callback(name, constant, &block)
      heritage.record(:add_callback, name, constant, &block)

      callbacks[name] ||= {
        group:   Class.new(constant || Disposable::Callback::Group),
        context: constant ? nil : :operation # `context: :operation` when the callback is inline. `context: group` otherwise.
      }

      callbacks[name][:group].class_eval(&block) if block_given?
    end
  end
end

{'fraction_non_alphanumeric': 0.12109955423476969, 'fraction_numerical': 0.0, 'mean_word_length': 3.691637630662021, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2349188', 'n_tokens_mistral': 822, 'n_tokens_neox': 784, 'n_words': 196}