Module: CircuitBreaker::History

Included in:
Token
Defined in:
lib/circuit_breaker/history.rb

Defined Under Namespace

Classes: Entry

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/circuit_breaker/history.rb', line 23

def self.included(base)
  base.class_eval do
    attr_reader :history

    # Add history initialization to existing initialize method
    original_initialize = instance_method(:initialize)
    define_method(:initialize) do |*args, **kwargs|
      original_initialize.bind(self).call(*args, **kwargs)
      @history = []
      record_event(:created, "Object created")
    end
  end
end

Instance Method Details

#export_history(format = :json) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/circuit_breaker/history.rb', line 56

def export_history(format = :json)
  history_data = history.map(&:to_h)
  case format
  when :json
    JSON.pretty_generate(history_data)
  when :yaml
    history_data.to_yaml
  when :csv
    require 'csv'
    CSV.generate do |csv|
      csv << ['Timestamp', 'Type', 'Details', 'Actor']
      history_data.each do |entry|
        csv << [
          entry[:timestamp].iso8601,
          entry[:type],
          entry[:details],
          entry[:actor_id]
        ]
      end
    end
  else
    raise ArgumentError, "Unsupported format: #{format}"
  end
end

#history_by_actor(actor_id) ⇒ Object



52
53
54
# File 'lib/circuit_breaker/history.rb', line 52

def history_by_actor(actor_id)
  history.select { |entry| entry.actor_id == actor_id }
end

#history_by_type(type) ⇒ Object



48
49
50
# File 'lib/circuit_breaker/history.rb', line 48

def history_by_type(type)
  history.select { |entry| entry.type == type }
end

#history_since(timestamp) ⇒ Object



44
45
46
# File 'lib/circuit_breaker/history.rb', line 44

def history_since(timestamp)
  history.select { |entry| entry.timestamp >= timestamp }
end

#record_event(type, details, actor_id: nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/circuit_breaker/history.rb', line 37

def record_event(type, details, actor_id: nil)
  entry = Entry.new(type: type, details: details, actor_id: actor_id)
  @history << entry
  trigger(:history_updated, entry: entry)
  entry
end