Sha256: 93dcd17e536f608b5fba54ed4faa2dc9661d55ed4921b428229a6a83705b2758

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

require 'rack/body_proxy'

module Flipper
  module Middleware
    class Memoizer
      # Public: Initializes an instance of the UI middleware.
      #
      # app - The app this middleware is included in.
      # flipper_or_block - The Flipper::DSL instance or a block that yields a
      #                    Flipper::DSL instance to use for all operations.
      #
      # Examples
      #
      #   flipper = Flipper.new(...)
      #
      #   # using with a normal flipper instance
      #   use Flipper::Middleware::Memoizer, flipper
      #
      #   # using with a block that yields a flipper instance
      #   use Flipper::Middleware::Memoizer, lambda { Flipper.new(...) }
      #
      def initialize(app, flipper_or_block)
        @app = app

        if flipper_or_block.respond_to?(:call)
          @flipper_block = flipper_or_block
        else
          @flipper = flipper_or_block
        end
      end

      def flipper
        @flipper ||= @flipper_block.call
      end

      def call(env)
        original = flipper.adapter.memoizing?
        flipper.adapter.memoize = true

        response = @app.call(env)
        response[2] = Rack::BodyProxy.new(response[2]) {
          flipper.adapter.memoize = original
        }
        response
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
flipper-0.7.5 lib/flipper/middleware/memoizer.rb
flipper-0.7.4 lib/flipper/middleware/memoizer.rb
flipper-0.7.3 lib/flipper/middleware/memoizer.rb
flipper-0.7.2 lib/flipper/middleware/memoizer.rb
flipper-0.7.1 lib/flipper/middleware/memoizer.rb
flipper-0.7.0 lib/flipper/middleware/memoizer.rb
flipper-0.7.0.beta6 lib/flipper/middleware/memoizer.rb
flipper-0.7.0.beta5 lib/flipper/middleware/memoizer.rb
flipper-0.7.0.beta4 lib/flipper/middleware/memoizer.rb
flipper-0.7.0.beta3 lib/flipper/middleware/memoizer.rb
flipper-0.7.0.beta2 lib/flipper/middleware/memoizer.rb
flipper-0.7.0.beta1 lib/flipper/middleware/memoizer.rb
flipper-0.6.3 lib/flipper/middleware/memoizer.rb
flipper-0.6.2 lib/flipper/middleware/memoizer.rb
flipper-0.6.1 lib/flipper/middleware/memoizer.rb
flipper-0.6.0 lib/flipper/middleware/memoizer.rb