Sha256: 65ac73e72949f4949b4ec7c4e0ef7798eb523caffaa90546d482f37664ac4ab4

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

require "declarative"
require "disposable/callback"

# Needs #[], #[]= skill dependency.
class Trailblazer::Operation
  module Callback
    def self.import!(operation, import, group)
      import.(:&, ->(input, options) { input.callback!(group) },
        name: "callback.#{group}")

      operation.send :include, self
    end

    def callback!(name=:default, options=self) # FIXME: test options.
      config  = self["callback.#{name}.class"] || raise #.fetch(name) # TODO: test exception
      group   = config[:group].new(self["contract.default"])

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

      invocations[name] = group
    end

    def invocations
      @invocations ||= {}
    end

    module DSL
      def callback(name=:default, constant=nil, &block)
        heritage.record(:callback, name, constant, &block)

        # FIXME: make this nicer. we want to extend same-named callback groups.
        # TODO: allow the same with contract, or better, test it!
        extended = self["callback.#{name}.class"] && self["callback.#{name}.class"]

        path, group_class = Trailblazer::DSL::Build.new.({ prefix: :callback, class: Disposable::Callback::Group, container: self }, name, constant, block) { |extended| extended[:group] }

        self[path] = { group: group_class, context: constant ? nil : :operation }
      end
    end
  end

  DSL.macro!(:Callback, Callback) # Operation::Callback()
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trailblazer-2.0.0 lib/trailblazer/operation/callback.rb
trailblazer-2.0.0.rc1 lib/trailblazer/operation/callback.rb
trailblazer-2.0.0.beta3 lib/trailblazer/operation/callback.rb
trailblazer-2.0.0.beta2 lib/trailblazer/operation/callback.rb