Class: LcpRuby::Sequences::SequenceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/sequences/sequence_manager.rb

Class Method Summary collapse

Class Method Details

.current(model:, field:, scope: {}) ⇒ Object

Get the current counter value for a specific model/field/scope combination.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lcp_ruby/sequences/sequence_manager.rb', line 30

def self.current(model:, field:, scope: {})
  counter_model = LcpRuby.registry.model_for("gapfree_sequence")
  scope_key = Sequences.build_scope_key(scope)

  row = counter_model.find_by(
    seq_model: model.to_s,
    seq_field: field.to_s,
    scope_key: scope_key
  )
  row&.current_value
end

.list(model: nil) ⇒ Object

List all counter rows, optionally filtered by model name.



43
44
45
46
47
48
# File 'lib/lcp_ruby/sequences/sequence_manager.rb', line 43

def self.list(model: nil)
  counter_model = LcpRuby.registry.model_for("gapfree_sequence")
  scope = counter_model.all
  scope = scope.where(seq_model: model.to_s) if model
  scope.order(:seq_model, :seq_field, :scope_key)
end

.set(model:, field:, scope: {}, value:) ⇒ Object

Set the current counter value for a specific model/field/scope combination.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lcp_ruby/sequences/sequence_manager.rb', line 15

def self.set(model:, field:, scope: {}, value:)
  counter_model = LcpRuby.registry.model_for("gapfree_sequence")
  scope_key = Sequences.build_scope_key(scope)

  row = counter_model.find_or_initialize_by(
    seq_model: model.to_s,
    seq_field: field.to_s,
    scope_key: scope_key
  )
  row.current_value = value
  row.save!
  row
end