Class: Featuring::Persistence::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/featuring/persistence/transaction.rb

Overview

public

Persist multiple feature flag values for an object at once.

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

  extend Featuring::Declarable
  feature :feature_1
  feature :feature_2
end

User.find(1).features.transaction do |features|
  features.enable :feature_1
  features.disable :feature_2
end

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

User.find(1).features.feature_2?
=> false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(features) ⇒ Transaction

Returns a new instance of Transaction.



31
32
33
34
# File 'lib/featuring/persistence/transaction.rb', line 31

def initialize(features)
  @features = features
  @values = {}
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



29
30
31
# File 'lib/featuring/persistence/transaction.rb', line 29

def values
  @values
end

Instance Method Details

#disable(feature) ⇒ Object

public

Disable a feature flag.

See ‘Featuring::Persistence::Adapter::Methods#disable`.



64
65
66
# File 'lib/featuring/persistence/transaction.rb', line 64

def disable(feature)
  @values[feature.to_sym] = false
end

#enable(feature) ⇒ Object

public

Enable a feature flag.

See ‘Featuring::Persistence::Adapter::Methods#enable`.



56
57
58
# File 'lib/featuring/persistence/transaction.rb', line 56

def enable(feature)
  @values[feature.to_sym] = true
end

#persist(feature, *args) ⇒ Object

public

Persist the default or computed value for a feature flag within a transaction.

See ‘Featuring::Persistence::Adapter::Methods#persist`.



40
41
42
# File 'lib/featuring/persistence/transaction.rb', line 40

def persist(feature, *args)
  @values[feature.to_sym] = @features.fetch_feature_flag_value(feature, *args, raw: true)
end

#reset(feature) ⇒ Object

public

Reset a feature flag.

See ‘Featuring::Persistence::Adapter::Methods#reset`.



72
73
74
# File 'lib/featuring/persistence/transaction.rb', line 72

def reset(feature)
  @values.delete(feature.to_sym)
end

#set(feature, value) ⇒ Object

public

Set the value for a feature flag within a transaction.

See ‘Featuring::Persistence::Adapter::Methods#set`.



48
49
50
# File 'lib/featuring/persistence/transaction.rb', line 48

def set(feature, value)
  @values[feature.to_sym] = !!value
end