Class: Arturo::Feature

Inherits:
ActiveRecord::Base
  • Object
show all
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

Instance Method Summary collapse

Methods included from SpecialHandling

included

Constructor Details

#initialize(*args, &block) ⇒ Feature

Create a new Feature



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

def initialize(*args, &block)
  args[0] = DEFAULT_ATTRIBUTES.merge(args[0] || {})
  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.

Parameters:

  • feature_or_name (Symbol, Arturo::Feature)

    a Feature or the Symbol of a Feature

Returns:



36
37
38
39
# File 'lib/arturo/feature.rb', line 36

def self.find_feature(feature_or_symbol)
  feature = to_feature(feature_or_symbol)
  feature.is_a?(Arturo::NoSuchFeature) ? nil : feature
end

.last_updated_atObject



77
78
79
# File 'lib/arturo/feature.rb', line 77

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.

Parameters:

  • feature_or_name (Symbol, Arturo::Feature)

    a Feature or the Symbol of a Feature

Returns:

  • (Arturo::Feature, nil)

    the Feature if found, else Arturo::NoSuchFeature



27
28
29
30
31
# File 'lib/arturo/feature.rb', line 27

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.

Parameters:

  • feature_recipient (Object)

    a User, Account, or other model with an #id method

Returns:

  • (true, false)

    whether or not this feature is enabled for feature_recipient

See Also:

  • SpecialHandling#whitelisted?
  • SpecialHandling#blacklisted?


53
54
55
56
57
58
# File 'lib/arturo/feature.rb', line 53

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)
end

#inspectObject



73
74
75
# File 'lib/arturo/feature.rb', line 73

def inspect
  "<Arturo::Feature #{name}, deployed to #{deployment_percentage}%>"
end

#nameObject



60
61
62
63
# File 'lib/arturo/feature.rb', line 60

def name
  return I18n.translate("arturo.feature.nameless") if symbol.blank?
  I18n.translate("arturo.feature.#{symbol}", :default => symbol.to_s.titleize)
end

#to_paramObject



69
70
71
# File 'lib/arturo/feature.rb', line 69

def to_param
  persisted? ? "#{id}-#{symbol.to_s.parameterize}" : nil
end

#to_sObject



65
66
67
# File 'lib/arturo/feature.rb', line 65

def to_s
  "Feature #{name}"
end