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.5.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.



92
93
94
95
# File 'lib/flipper/adapters/cassanity.rb', line 92

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

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

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

Returns nothing.



131
132
133
134
135
# File 'lib/flipper/adapters/cassanity.rb', line 131

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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/flipper/adapters/cassanity.rb', line 76

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.



150
151
152
153
# File 'lib/flipper/adapters/cassanity.rb', line 150

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.



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

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.



98
99
100
101
# File 'lib/flipper/adapters/cassanity.rb', line 98

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.



158
159
160
161
162
163
# File 'lib/flipper/adapters/cassanity.rb', line 158

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flipper/adapters/cassanity.rb', line 30

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

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

Private: Select rows matching key and optionally field.

Returns an Array of Hashes.



111
112
113
114
115
116
# File 'lib/flipper/adapters/cassanity.rb', line 111

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.



104
105
106
# File 'lib/flipper/adapters/cassanity.rb', line 104

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.



167
168
169
# File 'lib/flipper/adapters/cassanity.rb', line 167

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.



121
122
123
124
125
126
# File 'lib/flipper/adapters/cassanity.rb', line 121

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.



141
142
143
144
145
# File 'lib/flipper/adapters/cassanity.rb', line 141

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