Module: Featuring::Persistence::Adapter::Methods

Defined in:
lib/featuring/persistence/adapter.rb

Instance Method Summary collapse

Instance Method Details

#disable(feature) ⇒ Object

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


144
145
146
# File 'lib/featuring/persistence/adapter.rb', line 144

def disable(feature)
  create_or_update_feature_flags(feature.to_sym => false)
end

#enable(feature) ⇒ Object

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


127
128
129
# 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) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# 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) ⇒ Object

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


68
69
70
# 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) ⇒ Boolean

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


185
186
187
188
189
190
191
# 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

#reloadObject

public

Reload feature flag values for the object.



150
151
152
# File 'lib/featuring/persistence/adapter.rb', line 150

def reload
  @_persisted_flags = nil
end

#reset(feature) ⇒ Object

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


89
90
91
92
93
94
95
# 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) ⇒ Object

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


110
111
112
# File 'lib/featuring/persistence/adapter.rb', line 110

def set(feature, value)
  create_or_update_feature_flags(feature.to_sym => !!value)
end

#transaction {|transaction| ... } ⇒ Object

public

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

See Featuring::Persistence::Transaction.

Yields:



158
159
160
161
162
# 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