Sha256: 4a1bf7a15b3d8402441a20b9f4f4e04b13f9debc5a6451b9523252f73af25b5c

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Orthoses
  # Middleware builder like the Rack
  #   Builder.new do
  #     use Orthoses::CreateFileByName
  #     use Orthoses::Constant
  #     use Orthoses::IncludeExtendPrepend
  #     use Orthoses::Walk,
  #       root: "Foo"
  #     run ->() { require 'foo' }
  #   end
  class Builder
    def initialize(&block)
      @use = []
      @run = nil
      instance_eval(&block) if block
    end

    module CallLogable
      def call()
        Orthoses.logger.info("[#{self.class}]#call start")
        super.tap do
          Orthoses.logger.info("[#{self.class}]#call end")
        end
      end
    end

    def use(middleware, *args, **key, &block)
      @use << proc do |loader|
        middleware.new(loader, *args, **key, &block).tap do |m|
          m.extend CallLogable
        end
      end
    end

    def run(loader)
      use Store
      @run = proc do
        Orthoses.logger.info("[loader].call start")
        loader.call.tap do
          Orthoses.logger.info("[loader].call end")
        end
      end
    end

    def to_loader
      @use.reverse.inject(@run) { |current, next_proc| next_proc[current] }
    end

    def call
      to_loader.call
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
orthoses-0.1.0 lib/orthoses/builder.rb