Sha256: 04542e0753595c6cb747a7b316451364d7df903a87bccf9ecf9401cfe2bbbfd1

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'configurable'

module Tap
  class Middleware
    class << self
      
      # Instantiates an instance of self and causes app to use the instance
      # as middleware.
      def parse(argv=ARGV, app=Tap::App.instance)
        parse!(argv.dup, app)
      end
      
      # Same as parse, but removes arguments destructively.
      def parse!(argv=ARGV, app=Tap::App.instance)
        opts = ConfigParser.new
        opts.separator "configurations:"
        opts.add(configurations)
        
        args = opts.parse!(argv, :add_defaults => false)
        instantiate({:config => opts.nested_config}, app)
      end
      
      # Instantiates an instance of self and causes app to use the instance
      # as middleware.
      def instantiate(argh, app=Tap::App.instance)
        app.use(self, argh[:config] || {})
      end
    end
    
    include Configurable
    
    # The call stack.
    attr_reader :stack

    def initialize(stack, config={})
      @stack = stack
      initialize_config(config)
    end
    
    # Returns the app at the base of the stack.
    def app
      @app ||= begin
        current = stack
        until current.kind_of?(App::Stack)
          current = current.stack
        end
        current.app
      end
    end
    
    # By default call simply calls stack with the node and inputs.
    def call(node, inputs=[])
      stack.call(node, inputs)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tap-0.18.0 lib/tap/middleware.rb