Class: Arturo::Feature
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Arturo::Feature
- Includes:
- SpecialHandling
- Defined in:
- lib/arturo/feature.rb
Constant Summary collapse
- SYMBOL_REGEX =
/^[a-zA-z][a-zA-Z0-9_]*$/
- DEFAULT_ATTRIBUTES =
{ :deployment_percentage => 0 }.with_indifferent_access
Class Method Summary collapse
-
.find_feature(feature_or_symbol) ⇒ Arturo::Feature?
Looks up a feature by symbol.
- .last_updated_at ⇒ Object
-
.to_feature(feature_or_symbol) ⇒ Arturo::Feature?
Looks up a feature by symbol.
Instance Method Summary collapse
-
#enabled_for?(feature_recipient) ⇒ true, false
Whether or not this feature is enabled for feature_recipient.
-
#initialize(*args, &block) ⇒ Feature
constructor
Create a new Feature.
- #inspect ⇒ Object
- #name ⇒ Object
-
#passes_threshold?(feature_recipient, threshold) ⇒ Boolean
made public so as to allow for thresholds stored outside of the model.
- #to_param ⇒ Object
- #to_s ⇒ Object
Methods included from SpecialHandling
Constructor Details
#initialize(*args, &block) ⇒ Feature
Create a new Feature
43 44 45 46 |
# File 'lib/arturo/feature.rb', line 43 def initialize(*args, &block) args[0] = DEFAULT_ATTRIBUTES.merge(args[0].try(:to_h) || {}) super(*args, &block) end |
Class Method Details
.find_feature(feature_or_symbol) ⇒ Arturo::Feature?
Looks up a feature by symbol. Also accepts a Feature as input.
37 38 39 40 |
# File 'lib/arturo/feature.rb', line 37 def self.find_feature(feature_or_symbol) feature = to_feature(feature_or_symbol) feature.is_a?(Arturo::NoSuchFeature) ? nil : feature end |
.last_updated_at ⇒ Object
78 79 80 |
# File 'lib/arturo/feature.rb', line 78 def self.last_updated_at maximum(:updated_at) end |
.to_feature(feature_or_symbol) ⇒ Arturo::Feature?
Looks up a feature by symbol. Also accepts a Feature as input.
28 29 30 31 32 |
# File 'lib/arturo/feature.rb', line 28 def self.to_feature(feature_or_symbol) return feature_or_symbol if feature_or_symbol.kind_of?(self) symbol = feature_or_symbol.to_sym.to_s self.where(:symbol => symbol).first || Arturo::NoSuchFeature.new(symbol) end |
Instance Method Details
#enabled_for?(feature_recipient) ⇒ true, false
Returns whether or not this feature is enabled for feature_recipient.
54 55 56 57 58 59 |
# File 'lib/arturo/feature.rb', line 54 def enabled_for?(feature_recipient) return false if feature_recipient.nil? return false if blacklisted?(feature_recipient) return true if whitelisted?(feature_recipient) passes_threshold?(feature_recipient, deployment_percentage || 0) end |
#inspect ⇒ Object
74 75 76 |
# File 'lib/arturo/feature.rb', line 74 def inspect "<Arturo::Feature #{name}, deployed to #{deployment_percentage}%>" end |
#name ⇒ Object
61 62 63 64 |
# File 'lib/arturo/feature.rb', line 61 def name return I18n.translate("arturo.feature.nameless") if symbol.blank? I18n.translate("arturo.feature.#{symbol}", :default => symbol.to_s.titleize) end |
#passes_threshold?(feature_recipient, threshold) ⇒ Boolean
made public so as to allow for thresholds stored outside of the model
83 84 85 86 87 |
# File 'lib/arturo/feature.rb', line 83 def passes_threshold?(feature_recipient, threshold) return true if threshold == 100 return false if threshold == 0 || !feature_recipient.id (((feature_recipient.id + (self.id || 1) + 17) * 13) % 100) < threshold end |
#to_param ⇒ Object
70 71 72 |
# File 'lib/arturo/feature.rb', line 70 def to_param persisted? ? "#{id}-#{symbol.to_s.parameterize}" : nil end |
#to_s ⇒ Object
66 67 68 |
# File 'lib/arturo/feature.rb', line 66 def to_s "Feature #{name}" end |