Sha256: 158f008c81693aa5971068c1cdeccd2c7fb8a92c00f849fdb8fc447bcc549327

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 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()
#  - 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

      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 1.5.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

2 entries across 2 versions & 1 rubygems

Version Path
split-1.4.3 lib/split/encapsulated_helper.rb
split-1.4.2 lib/split/encapsulated_helper.rb