Module: Trebuchet::Strategy

Defined in:
lib/trebuchet/strategy.rb,
lib/trebuchet/strategy/stub.rb

Defined Under Namespace

Modules: Experimentable, PerDenominationable, Percentable, PercentableDeprecated Classes: Base, Custom, CustomRequestAware, Default, Everyone, Experiment, Hostname, Invalid, LogicAnd, LogicBase, LogicNot, LogicOr, Multiple, Nobody, PerDenomination, Percent, PercentDeprecated, Stub, UserId, VisitorExperiment, VisitorPercent, VisitorPercentDeprecated

Class Method Summary collapse

Class Method Details

.class_for_name(name) ⇒ Object



70
71
72
73
# File 'lib/trebuchet/strategy.rb', line 70

def self.class_for_name(name)
  classes = Hash[name_class_map]
  classes[name]
end

.deprecated_strategy_namesObject



63
64
65
66
67
68
# File 'lib/trebuchet/strategy.rb', line 63

def self.deprecated_strategy_names
  [
    :percent_deprecated,
    :visitor_percent_deprecated
  ]
end

.find(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trebuchet/strategy.rb', line 15

def self.find(*args)
  strategy_name, options = args

  if args.size > 2
    Multiple.new(args)
  elsif strategy_name.nil? || strategy_name == :default
    # Strategy hasn't been defined yet
    Default.instance
  elsif strategy_name == :everyone
    Everyone.instance
  elsif strategy_name == :nobody
    Nobody.instance
  elsif CustomRequestAware.exists?(strategy_name)
    CustomRequestAware.new(strategy_name, options)
  elsif Custom.exists?(strategy_name)
    Custom.new(strategy_name, options)
  elsif klass = class_for_name(strategy_name)
    # percent, users
    klass.new(options)
  else
    Invalid.new(strategy_name, options)
  end
end

.for_feature(feature) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/trebuchet/strategy.rb', line 5

def self.for_feature(feature)
  stub_state = Trebuchet::Feature.stubbed_features[feature.name]
  if stub_state
    Stub.new(stub_state)
  else
    strategy_args = Trebuchet.backend.get_strategy(feature.name)
    find(*strategy_args).tap {|s| s.feature = feature }
  end
end

.name_class_mapObject

The stub strategy purposely left out of this list as it should be accessible via the testing interface only and not externally.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/trebuchet/strategy.rb', line 41

def self.name_class_map
  [
    [:visitor_percent_deprecated, VisitorPercentDeprecated],
    [:percent_deprecated, PercentDeprecated],
    [:percent, Percent],
    [:per_denomination, PerDenomination],
    [:users, UserId],
    [:default, Default],
    [:everyone, Everyone],
    [:nobody, Nobody],
    [:custom, Custom],
    [:multiple, Multiple],
    [:experiment, Experiment],
    [:visitor_percent, VisitorPercent],
    [:hostname, Hostname],
    [:visitor_experiment, VisitorExperiment],
    [:logic_and, LogicAnd],
    [:logic_or, LogicOr],
    [:logic_not, LogicNot],
  ]
end

.name_for_class(klass) ⇒ Object



75
76
77
78
# File 'lib/trebuchet/strategy.rb', line 75

def self.name_for_class(klass)
  names = Hash[name_class_map.map(&:reverse)]
  names[klass]
end