Sha256: 4ba6289fa0a3691f6deeab01d0ef35b8501094b91e859e2292ebd4072b6aa441

Contents?: true

Size: 729 Bytes

Versions: 7

Compression:

Stored size: 729 Bytes

Contents

require "its-it"

module Enableable

  def enabled_middleware(root, env)
    middleware = self.singleton_class.ancestors.find(&it.to_s.start_with?("#{root}::Middleware"))
    return nil unless middleware.enabled?(env)
    middleware.disable if middleware.once?(env)
    middleware
  end

  def self.included(base)
    base.module_eval do
      def self.enable(condition = true, once:true)
        @enabled = condition
        @once = once
      end
      def self.enabled?(env)
        case @enabled
        when Proc then @enabled.call(env)
        else @enabled
        end
      end
      def self.once?(env)
        @once
      end
      def self.disable
        @enabled = false
      end

      disable
    end
  end
end


Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
schema_plus_core-2.2.3 spec/support/enableable.rb
schema_plus_core-2.2.2 spec/support/enableable.rb
schema_plus_core-2.2.1 spec/support/enableable.rb
schema_plus_core-2.2.0 spec/support/enableable.rb
schema_plus_core-2.1.1 spec/support/enableable.rb
schema_plus_core-2.1.0 spec/support/enableable.rb
schema_plus_core-2.0.1 spec/support/enableable.rb