Sha256: 8befe104a3e6f3a9ce51885cbd3819a9f2afab5a7417f2a72382457f98c58d39

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Wayfarer
  module Middleware
    module Worker
      def self.included(base)
        base.include(Wayfarer::Redis::Connection)
        base.include(Wayfarer::Middleware::Stage::API)
        base.include(Wayfarer::Callbacks)
        base.include(InstanceMethods)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def route
          @route ||= Wayfarer::Routing::RootRoute.new.tap do |route|
            Docile.dsl_eval(route) { yield if block_given? }
          end
        end
      end

      module InstanceMethods
        extend Forwardable

        delegate %i[params agent] => "task.metadata"

        def call(task)
          run_callbacks :action do
            public_send(task.metadata.action)
            yield if block_given? # TODO: Should be excluded from callback block
          end
        end

        def chain
          Wayfarer::Middleware::Chain.new([*Wayfarer.middleware, self])
        end

        def browser
          agent.instance
        end

        def page(live: false)
          return task.metadata.page unless live

          task.metadata.page = agent.live&.page || task.metadata.page
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wayfarer-0.4.1 lib/wayfarer/middleware/worker.rb