Class: Sapling::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/sapling/feature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, options = {}) ⇒ Feature

options

:users => array or hash
    users can be an array of user-ids or hash of user_ids mapped to true
:percentage =>


18
19
20
21
22
23
# File 'lib/sapling/feature.rb', line 18

def initialize(name=nil,options={})
  @name = name
  @users = options[:users] || {}
  normalize_users
  self.percentage = options[:percentage]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/sapling/feature.rb', line 3

def name
  @name
end

#percentageObject

Returns the value of attribute percentage.



3
4
5
# File 'lib/sapling/feature.rb', line 3

def percentage
  @percentage
end

#usersObject

Returns the value of attribute users.



3
4
5
# File 'lib/sapling/feature.rb', line 3

def users
  @users
end

Instance Method Details

#activate_percentage(percentage) ⇒ Object



50
51
52
# File 'lib/sapling/feature.rb', line 50

def activate_percentage(percentage)
  @percentage=percentage
end

#activate_user(user) ⇒ Object



42
43
44
# File 'lib/sapling/feature.rb', line 42

def activate_user(user)
  users[user.id]=true
end

#active?(options = {}) ⇒ Boolean

see Sapling::API::Client

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/sapling/feature.rb', line 29

def active?(options={})
  options = Util.normalized_options(options)
  individually_active?(options[:user]) || percentage_active?(options)
end

#deactivate_percentageObject



54
55
56
# File 'lib/sapling/feature.rb', line 54

def deactivate_percentage
  @percentage=0
end

#deactivate_user(user) ⇒ Object



46
47
48
# File 'lib/sapling/feature.rb', line 46

def deactivate_user(user)
  users.delete(user.id)
end

#individually_active?(user) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sapling/feature.rb', line 38

def individually_active?(user)
  user && users[user.id]
end

#percentage_active?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sapling/feature.rb', line 34

def percentage_active?(options={})
  percentage && (Util.modded_context_id(options)) < percentage
end

#to_sObject

the Feature reports its name with “to_s”



26
# File 'lib/sapling/feature.rb', line 26

def to_s; name.to_s; end