Module: Mongoid::FeatureFlags
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid_feature_flags.rb,
lib/mongoid_feature_flags/errors.rb,
lib/mongoid_feature_flags/version.rb
Defined Under Namespace
Modules: ClassMethods
Classes: InvalidFlagName
Constant Summary
collapse
- VERSION =
ENV['CIRCLE_BUILD_NUM'] ? "0.1.#{ENV['CIRCLE_BUILD_NUM']}" : "0.0.2"
Instance Method Summary
collapse
Instance Method Details
#add_feature(*flags) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/mongoid_feature_flags.rb', line 33
def add_feature(*flags)
self.feature_flags ||= {}
flags.each do |flag|
self.feature_flags[flag] = true
end
self.save
end
|
#clear_feature(*flags) ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/mongoid_feature_flags.rb', line 49
def clear_feature(*flags)
self.feature_flags ||= {}
flags.each do |flag|
self.feature_flags.delete(flag)
end
self.save
end
|
#feature?(*required_features, &block) ⇒ Boolean
61
62
63
64
65
66
|
# File 'lib/mongoid_feature_flags.rb', line 61
def feature?(*required_features, &block)
my_features = features
if required_features.any? { |feature| my_features[feature] }
block.present? ? block.yield : true
end
end
|
#feature_flag_validate_flag(*flag) ⇒ Object
75
76
77
|
# File 'lib/mongoid_feature_flags.rb', line 75
def feature_flag_validate_flag(*flag)
raise(InvalidFlagName, "Flags must be a symbol") if !flag.any? { |f| f.is_a?(Symbol) }
end
|
#features ⇒ Object
57
58
59
|
# File 'lib/mongoid_feature_flags.rb', line 57
def features
HashWithIndifferentAccess.new (inherited_feature_flags || {}).merge(generated_feature_flags || {}).merge(self.feature_flags || {})
end
|
#generated_feature_flags ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/mongoid_feature_flags.rb', line 79
def generated_feature_flags
if self.class.instance_variable_defined?("@feature_flag_methods")
self.class.instance_variable_get("@feature_flag_methods").inject({}) do |flag_hash, method_name|
if self.respond_to?(method_name) && self.send(method_name).present? && self.send(method_name)
flag_hash.merge(self.send(method_name) || {})
else
flag_hash
end
end
end
end
|
#inherited_feature_flags ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/mongoid_feature_flags.rb', line 91
def inherited_feature_flags
if self.class.instance_variable_defined?("@feature_flag_parents")
self.class.instance_variable_get("@feature_flag_parents").inject({}) do |flag_hash, relative|
if self.respond_to?(relative) && self.send(relative).present? && self.send(relative).respond_to?(:features)
flag_hash.merge(self.send(relative).send(:features) || {})
else
flag_hash
end
end
end
end
|
#not_feature?(*unrequired_features, &block) ⇒ Boolean
68
69
70
71
72
73
|
# File 'lib/mongoid_feature_flags.rb', line 68
def not_feature?(*unrequired_features, &block)
my_features = features
if unrequired_features.none? { |feature| my_features[feature] }
block.present? ? block.yield : true
end
end
|
#remove_feature(*flags) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/mongoid_feature_flags.rb', line 41
def remove_feature(*flags)
self.feature_flags ||= {}
flags.each do |flag|
self.feature_flags[flag] = false
end
self.save
end
|