Sha256: 7d6bdbb50c63450505c42e6a5bd82fed96f376c2e0c2e4a1e8ec1c0efd8b71e0

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

# 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 and 
#  - 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
      def initialize(context)
        @context = context
      end
      def ab_user
        @ab_user ||= Split::Persistence.adapter.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)
      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

4 entries across 4 versions & 1 rubygems

Version Path
split-1.2.0 lib/split/encapsulated_helper.rb
split-1.1.0 lib/split/encapsulated_helper.rb
split-1.0.0 lib/split/encapsulated_helper.rb
split-0.8.0 lib/split/encapsulated_helper.rb