Class: Flipper::Adapters::OperationLogger

Inherits:
Wrapper
  • Object
show all
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

Constants inherited from Wrapper

Wrapper::METHODS

Instance Attribute Summary collapse

Attributes inherited from Wrapper

#adapter

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, #get_all, #get_multi, #import, included, #name, #read_only?

Constructor Details

#initialize(adapter, operations = nil) ⇒ OperationLogger

Public



24
25
26
27
# File 'lib/flipper/adapters/operation_logger.rb', line 24

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

Instance Attribute Details

#operationsObject (readonly)

Internal: An array of the operations that have happened.



21
22
23
# File 'lib/flipper/adapters/operation_logger.rb', line 21

def operations
  @operations
end

Instance Method Details

#count(type = nil) ⇒ Object

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



30
31
32
33
34
35
36
# File 'lib/flipper/adapters/operation_logger.rb', line 30

def count(type = nil)
  if type
    type(type).size
  else
    @operations.size
  end
end

#inspectObject



53
54
55
56
# File 'lib/flipper/adapters/operation_logger.rb', line 53

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.



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

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

#resetObject

Public: Resets the operation log to empty



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

def reset
  @operations.clear
end

#type(type) ⇒ Object

Public: Get all operations of a certain type.



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

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