Class: TrailGuide::Participant
- Inherits:
-
Object
- Object
- TrailGuide::Participant
- Defined in:
- lib/trail_guide/participant.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #active_experiments(include_control = true) ⇒ Object
- #adapter ⇒ Object
- #converted!(variant, checkpoint = nil, reset: false) ⇒ Object
- #converted?(experiment, checkpoint = nil) ⇒ Boolean
-
#initialize(context, adapter: nil) ⇒ Participant
constructor
A new instance of Participant.
- #participating!(variant) ⇒ Object
- #participating?(experiment, include_control = true) ⇒ Boolean
- #participating_in_active_experiments?(include_control = true) ⇒ Boolean
Constructor Details
#initialize(context, adapter: nil) ⇒ Participant
Returns a new instance of Participant.
6 7 8 9 |
# File 'lib/trail_guide/participant.rb', line 6 def initialize(context, adapter: nil) @context = context @adapter = adapter.new(context) unless adapter.nil? end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
3 4 5 |
# File 'lib/trail_guide/participant.rb', line 3 def context @context end |
Instance Method Details
#active_experiments(include_control = true) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/trail_guide/participant.rb', line 91 def active_experiments(include_control=true) return false if adapter.keys.empty? adapter.keys.map { |key| key.to_s.split(":").first.to_sym }.uniq.map do |key| experiment = TrailGuide.catalog.find(key) next unless experiment && experiment.started? && participating?(experiment, include_control) [ experiment.experiment_name, adapter[experiment.storage_key] ] end.compact.to_h end |
#adapter ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/trail_guide/participant.rb', line 11 def adapter @adapter ||= begin config_adapter = TrailGuide.configuration.adapter case config_adapter when :cookie config_adapter = TrailGuide::Adapters::Participants::Cookie when :session config_adapter = TrailGuide::Adapters::Participants::Session when :redis config_adapter = TrailGuide::Adapters::Participants::Redis when :anonymous config_adapter = TrailGuide::Adapters::Participants::Anonymous when :multi config_adapter = TrailGuide::Adapters::Participants::Multi else config_adapter = config_adapter.constantize if config_adapter.is_a?(String) end config_adapter.new(context) end end |
#converted!(variant, checkpoint = nil, reset: false) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/trail_guide/participant.rb', line 74 def converted!(variant, checkpoint=nil, reset: false) checkpoint ||= :converted storage_key = "#{variant.experiment.storage_key}:#{checkpoint.to_s.underscore}" if reset adapter.delete(variant.experiment.storage_key) adapter.delete(variant.storage_key) adapter.delete(storage_key) experiment.funnels.each do |funnel| funnel_key = "#{variant.experiment.storage_key}:#{funnel.to_s}" adapter.delete(funnel_key) end else adapter[storage_key] = Time.now.to_i end end |
#converted?(experiment, checkpoint = nil) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/trail_guide/participant.rb', line 43 def converted?(experiment, checkpoint=nil) if experiment.funnels.empty? raise InvalidGoalError, "You provided the checkpoint `#{checkpoint}` but the experiment `#{experiment.experiment_name}` does not have any goals defined." unless checkpoint.nil? storage_key = "#{experiment.storage_key}:converted" return false unless adapter.key?(storage_key) converted_at = Time.at(adapter[storage_key].to_i) converted_at >= experiment.started_at elsif !checkpoint.nil? raise InvalidGoalError, "Invalid goal checkpoint `#{checkpoint}` for experiment `#{experiment.experiment_name}`." unless experiment.funnels.any? { |funnel| funnel == checkpoint.to_s.underscore.to_sym } storage_key = "#{experiment.storage_key}:#{checkpoint.to_s.underscore}" return false unless adapter.key?(storage_key) converted_at = Time.at(adapter[storage_key].to_i) converted_at >= experiment.started_at else experiment.funnels.each do |funnel| storage_key = "#{experiment.storage_key}:#{funnel.to_s}" next unless adapter.key?(storage_key) converted_at = Time.at(adapter[storage_key].to_i) return true if converted_at >= experiment.started_at end return false end end |
#participating!(variant) ⇒ Object
69 70 71 72 |
# File 'lib/trail_guide/participant.rb', line 69 def participating!(variant) adapter[variant.experiment.storage_key] = variant.name adapter[variant.storage_key] = Time.now.to_i end |
#participating?(experiment, include_control = true) ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/trail_guide/participant.rb', line 32 def participating?(experiment, include_control=true) return false unless adapter.key?(experiment.storage_key) varname = adapter[experiment.storage_key] variant = experiment.variants.find { |var| var == varname } return false if !include_control && variant.control? return false unless variant && adapter.key?(variant.storage_key) chosen_at = Time.at(adapter[variant.storage_key].to_i) chosen_at >= experiment.started_at end |
#participating_in_active_experiments?(include_control = true) ⇒ Boolean
100 101 102 103 104 105 106 107 108 |
# File 'lib/trail_guide/participant.rb', line 100 def participating_in_active_experiments?(include_control=true) return false if adapter.keys.empty? adapter.keys.any? do |key| experiment_name = key.to_s.split(":").first.to_sym experiment = TrailGuide.catalog.find(experiment_name) experiment && experiment.started? && participating?(experiment, include_control) end end |