Skip to content

Commit

Permalink
- Update gems
Browse files Browse the repository at this point in the history
- Add the 'inactive' methods:
	- `inactive?`
	- `inactive_globally?`
	- `inactive_partially?`
	- `inactive_for?`
	- `when_inactive`
	- `when_inactive_globally`
	- `when_inactive_partially`
	- `when_inactive_for`
  • Loading branch information
Verseth committed Feb 18, 2022
1 parent d924be7 commit 22d13fb
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 50 deletions.
46 changes: 24 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,49 @@ GEM
specs:
ast (2.4.2)
backport (1.2.0)
benchmark (0.1.1)
bundler-audit (0.8.0)
benchmark (0.2.0)
bundler-audit (0.9.0.1)
bundler (>= 1.2.0, < 3)
thor (~> 1.0)
byebug (11.1.3)
diff-lcs (1.4.4)
diff-lcs (1.5.0)
e2mmap (0.1.0)
jaro_winkler (1.5.4)
kramdown (2.3.1)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
mini_portile2 (2.6.1)
minitest (5.14.4)
nokogiri (1.12.3)
mini_portile2 (~> 2.6.1)
mini_portile2 (2.7.1)
minitest (5.15.0)
nokogiri (1.13.1)
mini_portile2 (~> 2.7.0)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.2.0)
parallel (1.21.0)
parser (3.1.0.0)
ast (~> 2.4.1)
racc (1.5.2)
rainbow (3.0.0)
racc (1.6.0)
rainbow (3.1.1)
rake (12.3.3)
redis (4.4.0)
redis (4.6.0)
redis-namespace (1.8.1)
redis (>= 3.0.4)
regexp_parser (2.1.1)
reverse_markdown (2.0.0)
regexp_parser (2.2.1)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rubocop (1.19.0)
rubocop (1.25.1)
parallel (~> 1.10)
parser (>= 3.0.0.0)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.9.1, < 2.0)
rubocop-ast (>= 1.15.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.9.1)
rubocop-ast (1.15.2)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
solargraph (0.43.0)
solargraph (0.44.3)
backport (~> 1.2)
benchmark
bundler (>= 1.17.2)
Expand All @@ -65,10 +65,12 @@ GEM
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
thor (1.1.0)
thor (1.2.1)
tilt (2.0.10)
unicode-display_width (2.0.0)
yard (0.9.26)
unicode-display_width (2.1.0)
webrick (1.7.0)
yard (0.9.27)
webrick (~> 1.7.0)

PLATFORMS
ruby
Expand Down
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,19 @@ FEATURE_FLAGS.active?(:feature_name) #=> false
FEATURE_FLAGS.active_globally?(:feature_name) #=> false
FEATURE_FLAGS.active_partially?(:feature_name) #=> false

FEATURE_FLAGS.inactive?(:feature_name) #=> true
FEATURE_FLAGS.inactive_globally?(:feature_name) #=> true
FEATURE_FLAGS.inactive_partially?(:feature_name) #=> true

FEATURE_FLAGS.activate(:feature_name) # or FEATURE_FLAGS.activate_globally(:feature_name)

FEATURE_FLAGS.active?(:feature_name) #=> true
FEATURE_FLAGS.active_globally?(:feature_name) #=> true
FEATURE_FLAGS.active_partially?(:feature_name) #=> false

FEATURE_FLAGS.inactive?(:feature_name) #=> false
FEATURE_FLAGS.inactive_globally?(:feature_name) #=> false
FEATURE_FLAGS.inactive_partially?(:feature_name) #=> true
```

#### Deactivate a feature
Expand All @@ -149,10 +157,12 @@ Deactivates a feature in the global scope

```ruby
FEATURE_FLAGS.active?(:feature_name) #=> true
FEATURE_FLAGS.inactive?(:feature_name) #=> false

FEATURE_FLAGS.deactivate(:feature_name)

FEATURE_FLAGS.active?(:feature_name) #=> false
FEATURE_FLAGS.inactive?(:feature_name) #=> true
```

#### Activate a feature for a particular record/object
Expand All @@ -162,11 +172,19 @@ FEATURE_FLAGS.active_partially?(:feature_name) #=> true
FEATURE_FLAGS.active_for?(:feature_name, User.first) #=> false
FEATURE_FLAGS.active_for?(:feature_name, User.last) #=> false

FEATURE_FLAGS.inactive_partially?(:feature_name) #=> false
FEATURE_FLAGS.inactive_for?(:feature_name, User.first) #=> true
FEATURE_FLAGS.inactive_for?(:feature_name, User.last) #=> true

FEATURE_FLAGS.activate_for(:feature_name, User.first) #=> true

FEATURE_FLAGS.active_partially?(:feature_name) #=> true
FEATURE_FLAGS.active_for?(:feature_name, User.first) #=> true
FEATURE_FLAGS.active_for?(:feature_name, User.last) #=> false

FEATURE_FLAGS.inactive_partially?(:feature_name) #=> false
FEATURE_FLAGS.inactive_for?(:feature_name, User.first) #=> false
FEATURE_FLAGS.inactive_for?(:feature_name, User.last) #=> true
```

Note that the flag itself has to be active `partially` for any record/object specific settings to work.
Expand Down Expand Up @@ -299,34 +317,63 @@ if FEATURE_FLAGS.active?(:feature_name)
number += 1
end

if FEATURE_FLAGS.inactive?(:feature_name)
number += 1
end

# or using a block

# this code will run only when the :feature_name flag is active (either partially or globally)
FEATURE_FLAGS.when_active(:feature_name) do
number += 1
end

# the opposite
FEATURE_FLAGS.when_inactive(:feature_name) do
number += 1
end

# this code will run only when the :feature_name flag is active globally
FEATURE_FLAGS.when_active_globally(:feature_name) do
number += 1
end

# the opposite
FEATURE_FLAGS.when_inactive_globally(:feature_name) do
number += 1
end

# this code will run only when the :feature_name flag is active partially (only for specific records/users)
FEATURE_FLAGS.when_active_partially(:feature_name) do
number += 1
end

# this code will run only if the :feature_name flag is active partially for the first User
# the opposite
FEATURE_FLAGS.when_inactive_partially(:feature_name) do
number += 1
end

# this code will run only if the :feature_name flag is active for the first User
FEATURE_FLAGS.when_active_for(:feature_name, User.first) do
number += 1
end

# the opposite
FEATURE_FLAGS.when_inactive_for(:feature_name, User.first) do
number += 1
end

# feature flags that don't exist will return false
FEATURE_FLAGS.active?(:non_existant) #=> false
FEATURE_FLAGS.inactive?(:non_existant) #=> true

if FEATURE_FLAGS.active_for?(:feature_name, User.first)
number += 1
end

if FEATURE_FLAGS.inactive_for?(:feature_name, User.first)
number += 1
end
```

#### Adding feature flags
Expand All @@ -340,7 +387,7 @@ FEATURE_FLAGS.add(:feature_name, 'Description')
FEATURE_FLAGS.active?(:feature_name) #=> false
FEATURE_FLAGS.active_partially?(:feature_name) #=> false
FEATURE_FLAGS.active_globally?(:feature_name) #=> false
FEATURE_FLAGS.active_for?(:feature_active_partially, User.first) #=> false
FEATURE_FLAGS.active_for?(:feature_name, User.first) #=> false

# add a new globally active flag
FEATURE_FLAGS.add(:active_feature, 'Description', :globally)
Expand Down Expand Up @@ -370,6 +417,10 @@ FEATURE_FLAGS.remove(:feature_name)
FEATURE_FLAGS.active?(:feature_name) #=> false
FEATURE_FLAGS.active_partially?(:feature_name) #=> false
FEATURE_FLAGS.active_globally?(:feature_name) #=> false

FEATURE_FLAGS.inactive?(:feature_name) #=> true
FEATURE_FLAGS.inactive_partially?(:feature_name) #=> true
FEATURE_FLAGS.inactive_globally?(:feature_name) #=> true
```


Expand Down
4 changes: 2 additions & 2 deletions lib/simple_feature_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module SimpleFeatureFlags
UI_CLASS_NAME = '::SimpleFeatureFlags::Ui'
WEB_UI_CLASS_NAME = '::SimpleFeatureFlags::Ui::Web'

ACTIVE_GLOBALLY = ['globally', :globally, 'true', true].freeze
ACTIVE_PARTIALLY = ['partially', :partially].freeze
ACTIVE_GLOBALLY = ::Set['globally', :globally, 'true', true].freeze
ACTIVE_PARTIALLY = ::Set['partially', :partially].freeze

class NoSuchCommandError < StandardError; end

Expand Down
56 changes: 48 additions & 8 deletions lib/simple_feature_flags/ram_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,26 @@ def active?(feature)
false
end

def inactive?(feature)
!active?(feature)
end

def active_globally?(feature)
ACTIVE_GLOBALLY.include? flags.dig(feature.to_sym, 'active')
end

def inactive_globally?(feature)
!active_globally?(feature)
end

def active_partially?(feature)
ACTIVE_PARTIALLY.include? flags.dig(feature.to_sym, 'active')
end

def inactive_partially?(feature)
!active_partially?(feature)
end

def active_for?(feature, object, object_id_method = CONFIG.default_id_method)
return false unless active?(feature)
return true if active_globally?(feature)
Expand All @@ -54,6 +66,10 @@ def active_for?(feature, object, object_id_method = CONFIG.default_id_method)
active_ids.include? object.public_send(object_id_method)
end

def inactive_for?(feature, object, object_id_method = CONFIG.default_id_method)
!active_for?(feature, object, object_id_method)
end

def exists?(feature)
return false if [nil, ''].include? flags[feature.to_sym]

Expand All @@ -64,28 +80,52 @@ def description(feature)
flags.dig(feature.to_sym, 'description')
end

def when_active(feature, &block)
def when_active(feature)
return unless active?(feature)

block.call
yield
end

def when_inactive(feature)
return unless inactive?(feature)

yield
end

def when_active_globally(feature, &block)
def when_active_globally(feature)
return unless active_globally?(feature)

block.call
yield
end

def when_active_partially(feature, &block)
def when_inactive_globally(feature)
return unless inactive_globally?(feature)

yield
end

def when_active_partially(feature)
return unless active_partially?(feature)

block.call
yield
end

def when_inactive_partially(feature)
return unless inactive_partially?(feature)

yield
end

def when_active_for(feature, object, object_id_method = CONFIG.default_id_method, &block)
def when_active_for(feature, object, object_id_method = CONFIG.default_id_method)
return unless active_for?(feature, object, object_id_method)

block.call
yield
end

def when_inactive_for(feature, object, object_id_method = CONFIG.default_id_method)
return unless inactive_for?(feature, object, object_id_method)

yield
end

def activate(feature)
Expand Down
Loading

0 comments on commit 22d13fb

Please sign in to comment.