Sha256: 394e3ddae9429cf629187682e200e89a945b1cfa11f5f2133d33b42be2592893

Contents?: true

Size: 1.44 KB

Versions: 5

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true
# 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, :finished, :ab_finished

      def initialize(context)
        @context = context
      end

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

    def ab_test(*arguments)
      ret = split_context_shim.ab_test(*arguments)
      # TODO there must be a better way to pass a block straight
      # through to the original ab_test
      if block_given?
        if defined?(capture) # a block in a rails view
          block = Proc.new { yield(ret) }
          concat(capture(ret, &block))
          false
        else
          yield(ret)
        end
      else
        ret
      end
    end

    def ab_test_finished(*arguments)
      warn 'DEPRECATION WARNING: ab_test_finished is deprecated and will be removed from Split 2.0.0'
      split_context_shim.finished *arguments
    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

5 entries across 5 versions & 1 rubygems

Version Path
split-1.7.0 lib/split/encapsulated_helper.rb
split-1.6.0 lib/split/encapsulated_helper.rb
split-1.5.0 lib/split/encapsulated_helper.rb
split-1.4.5 lib/split/encapsulated_helper.rb
split-1.4.4 lib/split/encapsulated_helper.rb