Class: Flipper::Adapters::Cassanity

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

Constant Summary collapse

FeaturesKey =

Private: The key that stores the set of known features.

:flipper_features
VERSION =
"0.7.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_family) ⇒ Cassanity

Public: Initializes a Cassanity flipper adapter.

column_family - The Cassanity::ColumnFamily that should store the info.



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

def initialize(column_family)
  @column_family = column_family
  @name = :cassanity
end

Instance Attribute Details

#column_familyObject (readonly)

Private: The column family where the data is stored.



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

def column_family
  @column_family
end

#nameObject (readonly)

Public: The name of the adapter.



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

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

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



34
35
36
37
# File 'lib/flipper/adapters/cassanity.rb', line 34

def add(feature)
  update FeaturesKey, feature.name, 1
  true
end

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



47
48
49
50
# File 'lib/flipper/adapters/cassanity.rb', line 47

def clear(feature)
  delete feature.key
  true
end

#delete(key, field = :skip) ⇒ Object

Private: Delete rows matching key and optionally field as well.

Returns nothing.



144
145
146
147
148
# File 'lib/flipper/adapters/cassanity.rb', line 144

def delete(key, field = :skip)
  @column_family.delete({
    where: where(key, field),
  })
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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/flipper/adapters/cassanity.rb', line 101

def disable(feature, gate, thing)
  case gate.data_type
  when :boolean
    delete feature.key
  when :integer
    update feature.key, gate.key, thing.value.to_s
  when :set
    delete feature.key, to_field(gate, thing)
  else
    unsupported_data_type gate.data_type
  end

  true
end

#doc_for(feature) ⇒ Object

Private: Gets a hash of fields => values for the given feature.

Returns a Hash of fields => values.



163
164
165
166
# File 'lib/flipper/adapters/cassanity.rb', line 163

def doc_for(feature)
  field_value_pairs = select(feature.key).map { |row| row.values }
  Hash[*field_value_pairs.flatten]
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.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/flipper/adapters/cassanity.rb', line 81

def enable(feature, gate, thing)
  case gate.data_type
  when :boolean, :integer
    update feature.key, gate.key, thing.value.to_s
  when :set
    update feature.key, to_field(gate, thing), thing.value.to_s
  else
    unsupported_data_type gate.data_type
  end

  true
end

#featuresObject

Public: The set of known features.



28
29
30
31
# File 'lib/flipper/adapters/cassanity.rb', line 28

def features
  rows = select(FeaturesKey)
  rows.map { |row| row['field'] }.to_set
end

#fields_to_gate_value(fields, gate) ⇒ Object

Private: Returns a set of values given an array of fields and a gate.

Returns a Set of the values enabled for the gate.



171
172
173
174
175
176
# File 'lib/flipper/adapters/cassanity.rb', line 171

def fields_to_gate_value(fields, gate)
  regex = /^#{Regexp.escape(gate.key)}\//
  keys = fields.grep(regex)
  values = keys.map { |key| key.split('/', 2).last }
  values.to_set
end

#get(feature) ⇒ Object

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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flipper/adapters/cassanity.rb', line 55

def get(feature)
  result = {}
  doc = doc_for(feature)
  fields = doc.keys

  feature.gates.each do |gate|
    result[gate.key] = case gate.data_type
    when :boolean, :integer
      doc[gate.key.to_s]
    when :set
      fields_to_gate_value fields, gate
    else
      unsupported_data_type gate.data_type
    end
  end

  result
end

#remove(feature) ⇒ Object

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



40
41
42
43
44
# File 'lib/flipper/adapters/cassanity.rb', line 40

def remove(feature)
  delete FeaturesKey, feature.name
  clear feature
  true
end

#select(key, field = :skip) ⇒ Object

Private: Select rows matching key and optionally field.

Returns an Array of Hashes.



124
125
126
127
128
129
# File 'lib/flipper/adapters/cassanity.rb', line 124

def select(key, field = :skip)
  @column_family.select({
    select: [:field, :value],
    where: where(key, field),
  })
end

#to_field(gate, thing) ⇒ Object

Private: Converts gate and thing to hash key.



117
118
119
# File 'lib/flipper/adapters/cassanity.rb', line 117

def to_field(gate, thing)
  "#{gate.key}/#{thing.value}"
end

#unsupported_data_type(data_type) ⇒ Object

Private: Raises error letting user know that data type is not supported by this adapter.



180
181
182
# File 'lib/flipper/adapters/cassanity.rb', line 180

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

#update(key, field, value) ⇒ Object

Private: Update key/field combo to value.

Returns nothing.



134
135
136
137
138
139
# File 'lib/flipper/adapters/cassanity.rb', line 134

def update(key, field, value)
  @column_family.update({
    set: {value: value},
    where: {key: key, field: field},
  })
end

#where(key, field) ⇒ Object

Private: Given a key and field it returns appropriate where hash for querying the column family.

Returns a Hash to be used as criteria for a query.



154
155
156
157
158
# File 'lib/flipper/adapters/cassanity.rb', line 154

def where(key, field)
  where = {key: key}
  where[:field] = field unless field == :skip
  where
end