Sha256: 6bf8f84b8a33638b7784d6c93ee4061e7b21c4c3b24291ddf6701dbd18d5eb6a

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # Internal before hook module, not for external use.
    # Allows for plugins to configure the order in which
    # before processing is done by using _roda_before_*
    # private instance methods that are called in sorted order.
    module BeforeHook # :nodoc:
      # Rebuild the rack app if the rack app already exists,
      # so the before hooks are setup inside the rack app
      # route block.
      def self.configure(app)
        app.instance_exec do
          build_rack_app if @app
        end
      end

      module ClassMethods
        # Rebuild the _roda_before method whenever a plugin might
        # have added a _roda_before_* method.
        def include(*a)
          res = super
          def_roda_before
          res
        end

        private

        # Build a _roda_before method that calls each _roda_before_* method
        # in order.
        def def_roda_before
          meths = private_instance_methods.grep(/\A_roda_before_\d\d/).sort.join(';')
          class_eval("def _roda_before; #{meths} end", __FILE__, __LINE__)
          private :_roda_before
        end

        # Modify rack app route block to use before hook.
        def rack_app_route_block(block)
          lambda do |r|
            _roda_before
            instance_exec(r, &block)
          end
        end
      end
    end

    register_plugin(:_before_hook, BeforeHook)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
roda-3.16.0 lib/roda/plugins/_before_hook.rb
roda-3.15.0 lib/roda/plugins/_before_hook.rb
roda-3.14.1 lib/roda/plugins/_before_hook.rb
roda-3.14.0 lib/roda/plugins/_before_hook.rb
roda-3.13.0 lib/roda/plugins/_before_hook.rb
roda-3.12.0 lib/roda/plugins/_before_hook.rb