Class: Trebuchet::Strategy::Custom

Inherits:
Base
  • Object
show all
Defined in:
lib/trebuchet/strategy/custom.rb

Direct Known Subclasses

CustomRequestAware

Constant Summary collapse

@@custom_strategies =
{}

Instance Attribute Summary collapse

Attributes inherited from Base

#feature

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#feature_id, #inspect, #name, strategy_name

Constructor Details

#initialize(name, options = nil) ⇒ Custom

Returns a new instance of Custom.



7
8
9
10
11
# File 'lib/trebuchet/strategy/custom.rb', line 7

def initialize(name, options = nil)
  @custom_name = name
  @options = options
  @block = @@custom_strategies[name]
end

Instance Attribute Details

#custom_nameObject (readonly)

Returns the value of attribute custom_name.



3
4
5
# File 'lib/trebuchet/strategy/custom.rb', line 3

def custom_name
  @custom_name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/trebuchet/strategy/custom.rb', line 3

def options
  @options
end

Class Method Details

.define(name, block) ⇒ Object



17
18
19
# File 'lib/trebuchet/strategy/custom.rb', line 17

def self.define(name, block)
  @@custom_strategies[name] = block
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/trebuchet/strategy/custom.rb', line 21

def self.exists?(name)
  @@custom_strategies.has_key?(name)
end

Instance Method Details

#as_json(options = {}) ⇒ Object



35
36
37
# File 'lib/trebuchet/strategy/custom.rb', line 35

def as_json(options = {})
  {:custom_name => @custom_name, :options => @options}
end

#exportObject



43
44
45
# File 'lib/trebuchet/strategy/custom.rb', line 43

def export
  super as_json
end

#launch_at?(user, request = nil) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/trebuchet/strategy/custom.rb', line 13

def launch_at?(user, request = nil)
  !!(options ? @block.call(user, options) : @block.call(user))
end

#needs_user?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/trebuchet/strategy/custom.rb', line 25

def needs_user?
  false
  # re-enable after adding  { |options, user, request| }
  # if block = @@custom_strategies[custom_name]
  #   block.arity > 0
  # else
  #   false
  # end
end

#to_sObject



39
40
41
# File 'lib/trebuchet/strategy/custom.rb', line 39

def to_s
  "#{custom_name} (custom) #{options.inspect if options}"
end