module Featuring::Persistence::Adapter::Methods

Public Instance Methods

disable(feature) click to toggle source
public

Disable a feature flag.

class User < ActiveRecord::Base
  extend Featuring::Persistence::ActiveRecord

  extend Featuring::Declarable
  feature :feature_1
end

User.find(1).features.disable :feature_1
User.find(1).features.feature_1?
=> false
# File lib/featuring/persistence/adapter.rb, line 144
def disable(feature)
  create_or_update_feature_flags(feature.to_sym => false)
end
enable(feature) click to toggle source
public

Enable a feature flag.

class User < ActiveRecord::Base
  extend Featuring::Persistence::ActiveRecord

  extend Featuring::Declarable
  feature :feature_1
end

User.find(1).features.enable :feature_1
User.find(1).features.feature_1?
=> true
# File lib/featuring/persistence/adapter.rb, line 127
def enable(feature)
  create_or_update_feature_flags(feature.to_sym => true)
end
fetch_feature_flag_value(name, *args, raw: false) click to toggle source
Calls superclass method
# File lib/featuring/persistence/adapter.rb, line 229
def fetch_feature_flag_value(name, *args, raw: false)
  if !raw && persisted?(name)
    if feature_flag_has_block?(name)
      persisted(name) && super(name, *args)
    else
      persisted(name)
    end
  else
    super(name, *args)
  end
end
persist(feature, *args) click to toggle source
public

Persist the default or computed value for a feature flag.

class User < ActiveRecord::Base
  extend Featuring::Persistence::ActiveRecord

  extend Featuring::Declarable
  feature :feature_1, true
end

User.find(1).features.persist :feature_1
User.find(1).features.feature_1?
=> true

Passing arguments to a feature flag block:

class User < ActiveRecord::Base
  extend Featuring::Persistence::ActiveRecord

  extend Featuring::Declarable
  feature :feature_1 do |value|
    value == :foo
  end
end

User.find(1).features.persist :feature_1, :bar
User.find(1).features.feature_1?
=> false
# File lib/featuring/persistence/adapter.rb, line 68
def persist(feature, *args)
  create_or_update_feature_flags(feature => fetch_feature_flag_value(feature, *args, raw: true))
end
persisted?(name = nil, value = value_omitted = true) click to toggle source
public

Returns ‘true` if the feature flag is persisted, optionally with the specified value.

class User < ActiveRecord::Base
  extend Featuring::Persistence::ActiveRecord

  extend Featuring::Declarable
  feature :feature_1
end

User.find(1).features.persisted?(:feature_1)
=> false

User.find(1).features.enable :feature_1

User.find(1).features.persisted?(:feature_1)
=> true
User.find(1).features.persisted?(:feature_1, true)
=> true
User.find(1).features.persisted?(:feature_1, false)
=> false
# File lib/featuring/persistence/adapter.rb, line 185
def persisted?(name = nil, value = value_omitted = true)
  if name && persisted_flags
    persisted_flags.key?(name.to_sym) && (value_omitted || persisted(name) == value)
  else
    !persisted_flags.nil?
  end
end
reload() click to toggle source
public

Reload feature flag values for the object.

# File lib/featuring/persistence/adapter.rb, line 150
def reload
  @_persisted_flags = nil
end
reset(feature) click to toggle source
public

Ensure that a feature flag is not persisted, falling back to its default value.

class User < ActiveRecord::Base
  extend Featuring::Persistence::ActiveRecord

  extend Featuring::Declarable
  feature :feature_1, true
end

User.find(1).features.disable :feature_1
User.find(1).features.feature_1?
=> false

User.find(1).features.reset :feature_1
User.find(1).features.feature_1?
=> true
# File lib/featuring/persistence/adapter.rb, line 89
def reset(feature)
  if persisted?(feature)
    features = persisted_flags
    features.delete(feature)
    feature_flag_adapter.replace(@parent, **features.symbolize_keys)
  end
end
set(feature, value) click to toggle source
public

Set the value for a feature flag.

class User < ActiveRecord::Base
  extend Featuring::Persistence::ActiveRecord

  extend Featuring::Declarable
  feature :feature_1
end

User.find(1).features.set :feature_1, true
User.find(1).features.feature_1?
=> true
# File lib/featuring/persistence/adapter.rb, line 110
def set(feature, value)
  create_or_update_feature_flags(feature.to_sym => !!value)
end
transaction() { |transaction| ... } click to toggle source
public

Start a transaction in which multiple feature flags values can be persisted at once.

See ‘Featuring::Persistence::Transaction`.

# File lib/featuring/persistence/adapter.rb, line 158
def transaction
  transaction = Transaction.new(self)
  yield transaction
  create_or_update_feature_flags(__perform: :replace, **transaction.values)
end

Private Instance Methods

create_or_update_feature_flags(__perform: :update, **features) click to toggle source
# File lib/featuring/persistence/adapter.rb, line 207
        def create_or_update_feature_flags(__perform: :update, **features)
  if persisted?
    feature_flag_adapter.public_send(__perform, @parent, **features)

    # Update the local persisted values to match.
    #
    features.each do |feature, value|
      persisted_flags[feature] = value
    end

    # Remove local feature flags if no longer present.
    #
    persisted_flags.each_key do |feature|
      unless features.include?(feature.to_sym)
        persisted_flags.delete(feature)
      end
    end
  else
    feature_flag_adapter.create(@parent, **features)
  end
end
feature_flag_has_block?(name) click to toggle source
# File lib/featuring/persistence/adapter.rb, line 241
        def feature_flag_has_block?(name)
  internal_feature_delegator.method(name).arity > 0
end
fetch_flags() click to toggle source
# File lib/featuring/persistence/adapter.rb, line 201
        def fetch_flags
  if (flags = feature_flag_adapter.fetch(@parent))
    ActiveSupport::HashWithIndifferentAccess.new(flags)
  end
end
persisted(name) click to toggle source
# File lib/featuring/persistence/adapter.rb, line 193
        def persisted(name)
  persisted_flags[name.to_sym]
end
persisted_flags() click to toggle source
# File lib/featuring/persistence/adapter.rb, line 197
        def persisted_flags
  @_persisted_flags ||= fetch_flags
end