Class: Lyra::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/lyra/configuration.rb

Constant Summary collapse

MODES =

Valid modes for Lyra operation

[:disabled, :monitor, :hijack, :event_sourcing].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Configuration

Returns a new instance of Configuration.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lyra/configuration.rb', line 13

def initialize
  @mode = :monitor
  @event_backend = :rails_event_store
  @hijack_enabled = false
  @monitored_models = []
  @model_configs = {}
  @retention_policy = nil
  # Event sourcing specific options
  @projection_mode = :sync  # :sync, :async, or :disabled
  @strict_projections = false  # Raise on projection errors if true
  @projection_error_handler = nil  # Custom error handler proc
  @async_projections_inline = false  # Run async projections synchronously (useful for testing)
  # Schema validation options
  @strict_schema = false  # Fail on startup if schema changes detected
  @schema_path = nil  # Custom path for schema files (defaults to db/lyra_schemas/)
  # Strict data access - raise on operations that bypass callbacks
  @strict_data_access = false
  # User tracking - custom metadata proc called for every event
  # Signature: ->(record, operation) { { user_id: ..., ... } }
   = nil
end

Instance Attribute Details

#async_projections_inline ⇒ Object

Returns the value of attribute async_projections_inline.



7
8
9
# File 'lib/lyra/configuration.rb', line 7

def async_projections_inline
  @async_projections_inline
end

#event_backend ⇒ Object

Returns the value of attribute event_backend.



6
7
8
# File 'lib/lyra/configuration.rb', line 6

def event_backend
  @event_backend
end

#event_store ⇒ Object

Returns the value of attribute event_store.



6
7
8
# File 'lib/lyra/configuration.rb', line 6

def event_store
  @event_store
end

#hijack_enabled ⇒ Object

Returns the value of attribute hijack_enabled.



6
7
8
# File 'lib/lyra/configuration.rb', line 6

def hijack_enabled
  @hijack_enabled
end

#metadata_proc ⇒ Object

Custom metadata proc for events



10
11
12
# File 'lib/lyra/configuration.rb', line 10

def 
  
end

#mode ⇒ Object

Returns the value of attribute mode.



6
7
8
# File 'lib/lyra/configuration.rb', line 6

def mode
  @mode
end

#monitored_models ⇒ Object (readonly)

Returns the value of attribute monitored_models.



11
12
13
# File 'lib/lyra/configuration.rb', line 11

def monitored_models
  @monitored_models
end

#projection_error_handler ⇒ Object

Returns the value of attribute projection_error_handler.



7
8
9
# File 'lib/lyra/configuration.rb', line 7

def projection_error_handler
  @projection_error_handler
end

#projection_mode ⇒ Object

Returns the value of attribute projection_mode.



7
8
9
# File 'lib/lyra/configuration.rb', line 7

def projection_mode
  @projection_mode
end

#retention_policy ⇒ Object

Returns the value of attribute retention_policy.



6
7
8
# File 'lib/lyra/configuration.rb', line 6

def retention_policy
  @retention_policy
end

#schema_path ⇒ Object

Returns the value of attribute schema_path.



8
9
10
# File 'lib/lyra/configuration.rb', line 8

def schema_path
  @schema_path
end

#strict_data_access ⇒ Object

Raise on callback-bypassing operations



9
10
11
# File 'lib/lyra/configuration.rb', line 9

def strict_data_access
  @strict_data_access
end

#strict_projections ⇒ Object

Returns the value of attribute strict_projections.



7
8
9
# File 'lib/lyra/configuration.rb', line 7

def strict_projections
  @strict_projections
end

#strict_schema ⇒ Object

Returns the value of attribute strict_schema.



8
9
10
# File 'lib/lyra/configuration.rb', line 8

def strict_schema
  @strict_schema
end

Instance Method Details

#disable! ⇒ Object

Disable Lyra completely



92
93
94
95
# File 'lib/lyra/configuration.rb', line 92

def disable!
  @mode = :disabled
  @hijack_enabled = false
end

#disabled_mode? ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/lyra/configuration.rb', line 69

def disabled_mode?
  @mode == :disabled
end

#enable_event_sourcing! ⇒ Object

Enable event sourcing mode (events as source of truth, no direct DB writes)



86
87
88
89
# File 'lib/lyra/configuration.rb', line 86

def enable_event_sourcing!
  @mode = :event_sourcing
  @hijack_enabled = false
end

#enable_hijack! ⇒ Object

Enable hijack mode (can override CRUD operations)



74
75
76
77
# File 'lib/lyra/configuration.rb', line 74

def enable_hijack!
  @hijack_enabled = true
  @mode = :hijack
end

#enable_monitor! ⇒ Object

Enable monitor mode (only log events, don't override)



80
81
82
83
# File 'lib/lyra/configuration.rb', line 80

def enable_monitor!
  @hijack_enabled = false
  @mode = :monitor
end

#event_sourcing_mode? ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/lyra/configuration.rb', line 65

def event_sourcing_mode?
  @mode == :event_sourcing
end

#hijack_mode? ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/lyra/configuration.rb', line 61

def hijack_mode?
  @mode == :hijack || @hijack_enabled
end

#model_config(model_class) ⇒ Object



53
54
55
# File 'lib/lyra/configuration.rb', line 53

def model_config(model_class)
  @model_configs[model_class] || ModelConfiguration.new(model_class)
end

#monitor_mode? ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/lyra/configuration.rb', line 57

def monitor_mode?
  @mode == :monitor
end

#monitor_model(model_class, options = {}) ⇒ Object

Register a model for monitoring/hijacking



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lyra/configuration.rb', line 36

def monitor_model(model_class, options = {})
  # Check by name to handle Rails development reloading (class objects change on reload)
  model_name = begin
    model_class.name
  rescue StandardError
    model_class.object_id.to_s
  end

  unless @monitored_models.any? { |m| (m.name rescue m.object_id.to_s) == model_name }
    @monitored_models << model_class
  else
    # Update the reference to the new class object (after reload)
    @monitored_models.map! { |m| (m.name rescue m.object_id.to_s) == model_name ? model_class : m }
  end
  @model_configs[model_class] = ModelConfiguration.new(model_class, options)
end