Class: RubyFlipper::Feature
- Inherits:
-
Object
- Object
- RubyFlipper::Feature
- Defined in:
- lib/ruby_flipper/feature.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .add(name, *conditions, &block) ⇒ Object
- .all ⇒ Object
- .condition_met?(condition, *args) ⇒ Boolean
- .find(name) ⇒ Object
- .reset ⇒ Object
Instance Method Summary collapse
- #active?(*args) ⇒ Boolean
-
#initialize(name, *conditions, &block) ⇒ Feature
constructor
A new instance of Feature.
Constructor Details
#initialize(name, *conditions, &block) ⇒ Feature
Returns a new instance of Feature.
38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby_flipper/feature.rb', line 38 def initialize(name, *conditions, &block) if conditions.last.is_a?(Hash) opts = conditions.pop @description = opts[:description] conditions << opts[:condition] conditions << opts[:conditions] end @name, @conditions = name, [conditions, block].flatten.compact end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
36 37 38 |
# File 'lib/ruby_flipper/feature.rb', line 36 def conditions @conditions end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
36 37 38 |
# File 'lib/ruby_flipper/feature.rb', line 36 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/ruby_flipper/feature.rb', line 36 def name @name end |
Class Method Details
.add(name, *conditions, &block) ⇒ Object
14 15 16 |
# File 'lib/ruby_flipper/feature.rb', line 14 def self.add(name, *conditions, &block) all[name] = new(name, *conditions, &block) end |
.all ⇒ Object
6 7 8 |
# File 'lib/ruby_flipper/feature.rb', line 6 def self.all @@features ||= {} end |
.condition_met?(condition, *args) ⇒ Boolean
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ruby_flipper/feature.rb', line 22 def self.condition_met?(condition, *args) if condition.is_a?(Symbol) find(condition).active? elsif condition.is_a?(Proc) RubyFlipper.silence_warnings do !!ConditionContext.new.instance_exec(*args, &condition) end elsif condition.respond_to?(:call) !!condition.call else !!condition end end |
.find(name) ⇒ Object
18 19 20 |
# File 'lib/ruby_flipper/feature.rb', line 18 def self.find(name) all[name] || raise(NotDefinedError, "'#{name}' is not defined") end |
.reset ⇒ Object
10 11 12 |
# File 'lib/ruby_flipper/feature.rb', line 10 def self.reset @@features = nil end |
Instance Method Details
#active?(*args) ⇒ Boolean
48 49 50 |
# File 'lib/ruby_flipper/feature.rb', line 48 def active?(*args) conditions.map {|condition| self.class.condition_met?(condition, *args)}.all? end |