Sha256: d36abc62cb8590cfcc65820cddbefc2af85b550ca74f09597c4d500b933cf841

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "split/helper"

# Split's helper exposes all kinds of methods we don't want to
# mix into our model classes.
#
# This module exposes only two methods:
#  - ab_test()
#  - ab_finished()
# that can safely be mixed into any class.
#
# Passes the instance of the class that it's mixed into to the
# Split persistence adapter as context.
#
module Split
  module EncapsulatedHelper
    class ContextShim
      include Split::Helper
      public :ab_test, :ab_finished

      def initialize(context)
        @context = context
      end

      def params
        request.params if request && request.respond_to?(:params)
      end

      def request
        @context.request if @context.respond_to?(:request)
      end

      def ab_user
        @ab_user ||= Split::User.new(@context)
      end
    end

    def ab_test(*arguments, &block)
      split_context_shim.ab_test(*arguments, &block)
    end

    private
      # instantiate and memoize a context shim in case of multiple ab_test* calls
      def split_context_shim
        @split_context_shim ||= ContextShim.new(self)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
split-4.0.4 lib/split/encapsulated_helper.rb