Class: Flipper::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Flipper::Adapter
Defined in:
lib/flipper/adapters/activerecord.rb,
lib/flipper/adapters/activerecord/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



16
17
18
19
# File 'lib/flipper/adapters/activerecord.rb', line 16

def initialize(collection)
  @collection = collection
  @name = :activerecord
end

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



14
15
16
# File 'lib/flipper/adapters/activerecord.rb', line 14

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

Public: Adds a feature to the set of known features.



27
28
29
30
# File 'lib/flipper/adapters/activerecord.rb', line 27

def add(feature)
  Flipper::ActiveRecord::Feature.create!(name: feature.name.to_s)
  true
end

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



38
39
40
41
# File 'lib/flipper/adapters/activerecord.rb', line 38

def clear(feature)
  Flipper::ActiveRecord::Feature.where(name: feature.key).destroy_all
  true
end

#criteria(key) ⇒ Object

Private



147
148
149
# File 'lib/flipper/adapters/activerecord.rb', line 147

def criteria(key)
  {:_id => key.to_s}
end

#delete(key) ⇒ Object

Private



142
143
144
# File 'lib/flipper/adapters/activerecord.rb', line 142

def delete(key)
  @collection.remove criteria(key)
end

#disable(feature, gate, thing) ⇒ Object

Public: Disables a gate for a given thing.

feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.

Returns true.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/flipper/adapters/activerecord.rb', line 105

def disable(feature, gate, thing)
  conditions = { flipper_features: {name: feature.key} }

  g = case gate.data_type
  when :boolean
    Flipper::ActiveRecord::Gate.joins(:feature).where(conditions).destroy_all
  when :integer
    conditions.merge!(name: gate.key.to_s)
    Flipper::ActiveRecord::Gate.joins(:feature).where(conditions).
      limit(1).update_all(value: thing.value.to_s)
  when :set
    conditions.merge!(name: gate.key.to_s, value: thing.value.to_s)
    Flipper::ActiveRecord::Gate.joins(:feature).where(conditions).destroy_all
  else
    unsupported_data_type gate.data_type
  end

  true
end

#enable(feature, gate, thing) ⇒ Object

Public: Enables a gate for a given thing.

feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.

Returns true.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/flipper/adapters/activerecord.rb', line 78

def enable(feature, gate, thing)
  f = Flipper::ActiveRecord::Feature.find_or_create_by(name: feature.key.to_s)
  f.gates.find_or_create_by!(name: gate.key.to_s, value: thing.value.to_s)
  true
#         case gate.data_type
#         when :boolean, :integer
#           update feature.key, '$set' => {
#             gate.key.to_s => thing.value.to_s,
#           }
#         when :set
#           update feature.key, '$addToSet' => {
#             gate.key.to_s => thing.value.to_s,
#           }
#         else
#           unsupported_data_type gate.data_type
#         end
# 
#         true
end

#featuresObject

Public: The set of known features.



22
23
24
# File 'lib/flipper/adapters/activerecord.rb', line 22

def features
  Flipper::ActiveRecord::Feature.select(:name).map(&:name).to_set
end

#find(key) ⇒ Object

Private



131
132
133
# File 'lib/flipper/adapters/activerecord.rb', line 131

def find(key)
  @collection.find_one(criteria(key)) || {}
end

#get(feature) ⇒ Object

Public: Gets the values for all gates for a given feature.

Returns a Hash of Flipper::Gate#key => value.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/flipper/adapters/activerecord.rb', line 46

def get(feature)
  result = {}
  f = Flipper::ActiveRecord::Feature.find_by(name: feature.key)

  feature.gates.each do |gate|
    result[gate.key] = case gate.data_type
    when :boolean, :integer
      if f
        g = f.gates.detect {|g| g.name == gate.key.to_s}
        g.value if g
      end
    when :set
      if f
        f.gates.select {|g| g.name == gate.key.to_s}.map{|g| g.value }.to_set
      else
        Set.new
      end
    else
      unsupported_data_type gate.data_type
    end
  end

  result
end

#remove(feature) ⇒ Object

Public: Removes a feature from the set of known features.



33
34
35
# File 'lib/flipper/adapters/activerecord.rb', line 33

def remove(feature)
  clear(feature)
end

#unsupported_data_type(data_type) ⇒ Object

Private



126
127
128
# File 'lib/flipper/adapters/activerecord.rb', line 126

def unsupported_data_type(data_type)
  raise "#{data_type} is not supported by this adapter"
end

#update(key, updates) ⇒ Object

Private



136
137
138
139
# File 'lib/flipper/adapters/activerecord.rb', line 136

def update(key, updates)
  options = {:upsert => true}
  @collection.update criteria(key), updates, options
end