Sha256: ef29fe4664f1d7f525c718d5f872dc9c0a0890a25fdb7788103a69095e4de915

Contents?: true

Size: 1.52 KB

Versions: 9

Compression:

Stored size: 1.52 KB

Contents

module Trailblazer
  class Circuit
    # This is a code structure to encapsulate the circuit execution behavior and the
    # start, intermediate and end events, within a "physical" business process.
    #
    #   activity[:Start]
    #   activity.()
    #   activity.values
    Activity = Struct.new(:circuit, :events) do
      def [](*args)
        events[*args]
      end

      def call(*args, &block)
        circuit.(*args, &block)
      end
    end

    # Builder for an Activity with Circuit instance and events.
    def self.Activity(name=:default, events={}, &block)
      # default events:
      start   = events[:start] || { default: Start.new(:default) }
      _end    = events[:end]   || { default: End.new(:default) }

      events = { start: start, end: _end }.merge(events)

      evts = Events(events)
      circuit = Circuit(name, evts, _end.values, &block)

      Activity.new(circuit, evts)
    end

    def self.Circuit(name=:default, events, end_events)
      Circuit.new(yield(events), end_events, name)
    end

    # @data structure to hold events for an activity, keyed by type, then name.
    # @api private
    def self.Events(events)
      evts = Struct.new(*events.keys) do # [Start, End, Resume]
        def [](event, name=:default)
          cfg = super(event.downcase)
          cfg[name.to_sym] or raise "[Circuit] Event `#{event}.#{name} unknown."
          # DISCUSS: the event type's events should also be a Struct to avoid :symbol vs. string.
        end
      end

      evts.new(*events.values)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
trailblazer-circuit-0.0.11 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.10 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.9 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.8 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.7 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.6 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.5 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.4 lib/trailblazer/circuit/activity.rb
trailblazer-circuit-0.0.3 lib/trailblazer/circuit/activity.rb