Class: Flipper::Adapters::OperationLogger

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

Overview

Public: Adapter that wraps another adapter and stores the operations.

Useful in tests to verify calls and such. Never use outside of testing.

Defined Under Namespace

Classes: Operation

Constant Summary collapse

OperationTypes =
[
  :import,
  :export,
  :features,
  :add,
  :remove,
  :clear,
  :get,
  :get_multi,
  :get_all,
  :enable,
  :disable,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, included, #name

Constructor Details

#initialize(adapter, operations = nil) ⇒ OperationLogger

Public



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

def initialize(adapter, operations = nil)
  @adapter = adapter
  @operations = operations || []
end

Instance Attribute Details

#operationsObject (readonly)

Internal: An array of the operations that have happened.



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

def operations
  @operations
end

Instance Method Details

#add(feature) ⇒ Object

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



50
51
52
53
# File 'lib/flipper/adapters/operation_logger.rb', line 50

def add(feature)
  @operations << Operation.new(:add, [feature])
  @adapter.add(feature)
end

#clear(feature) ⇒ Object

Public: Clears all the gate values for a feature.



63
64
65
66
# File 'lib/flipper/adapters/operation_logger.rb', line 63

def clear(feature)
  @operations << Operation.new(:clear, [feature])
  @adapter.clear(feature)
end

#count(type) ⇒ Object

Public: Count the number of times a certain operation happened.



111
112
113
# File 'lib/flipper/adapters/operation_logger.rb', line 111

def count(type)
  type(type).size
end

#disable(feature, gate, thing) ⇒ Object

Public



93
94
95
96
# File 'lib/flipper/adapters/operation_logger.rb', line 93

def disable(feature, gate, thing)
  @operations << Operation.new(:disable, [feature, gate, thing])
  @adapter.disable(feature, gate, thing)
end

#enable(feature, gate, thing) ⇒ Object

Public



87
88
89
90
# File 'lib/flipper/adapters/operation_logger.rb', line 87

def enable(feature, gate, thing)
  @operations << Operation.new(:enable, [feature, gate, thing])
  @adapter.enable(feature, gate, thing)
end

#export(format: :json, version: 1) ⇒ Object

Public



105
106
107
108
# File 'lib/flipper/adapters/operation_logger.rb', line 105

def export(format: :json, version: 1)
  @operations << Operation.new(:export, [format, version])
  @adapter.export(format: format, version: version)
end

#featuresObject

Public: The set of known features.



44
45
46
47
# File 'lib/flipper/adapters/operation_logger.rb', line 44

def features
  @operations << Operation.new(:features, [])
  @adapter.features
end

#get(feature) ⇒ Object

Public



69
70
71
72
# File 'lib/flipper/adapters/operation_logger.rb', line 69

def get(feature)
  @operations << Operation.new(:get, [feature])
  @adapter.get(feature)
end

#get_allObject

Public



81
82
83
84
# File 'lib/flipper/adapters/operation_logger.rb', line 81

def get_all
  @operations << Operation.new(:get_all, [])
  @adapter.get_all
end

#get_multi(features) ⇒ Object

Public



75
76
77
78
# File 'lib/flipper/adapters/operation_logger.rb', line 75

def get_multi(features)
  @operations << Operation.new(:get_multi, [features])
  @adapter.get_multi(features)
end

#import(source) ⇒ Object

Public



99
100
101
102
# File 'lib/flipper/adapters/operation_logger.rb', line 99

def import(source)
  @operations << Operation.new(:import, [source])
  @adapter.import(source)
end

#inspectObject



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

def inspect
  inspect_id = ::Kernel::format "%x", (object_id * 2)
  %(#<#{self.class}:0x#{inspect_id} @name=#{name.inspect}, @operations=#{@operations.inspect}, @adapter=#{@adapter.inspect}>)
end

#last(type) ⇒ Object

Public: Get the last operation of a certain type.



121
122
123
# File 'lib/flipper/adapters/operation_logger.rb', line 121

def last(type)
  @operations.reverse.find { |operation| operation.type == type }
end

#remove(feature) ⇒ Object

Public: Removes a feature from the set of known features and clears all the values for the feature.



57
58
59
60
# File 'lib/flipper/adapters/operation_logger.rb', line 57

def remove(feature)
  @operations << Operation.new(:remove, [feature])
  @adapter.remove(feature)
end

#resetObject

Public: Resets the operation log to empty



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

def reset
  @operations.clear
end

#type(type) ⇒ Object

Public: Get all operations of a certain type.



116
117
118
# File 'lib/flipper/adapters/operation_logger.rb', line 116

def type(type)
  @operations.select { |operation| operation.type == type }
end