Class: BetterAuth::DatabaseHooks

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/database_hooks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, options) ⇒ DatabaseHooks

Returns a new instance of DatabaseHooks.



7
8
9
10
# File 'lib/better_auth/database_hooks.rb', line 7

def initialize(adapter, options)
  @adapter = adapter
  @options = options
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/better_auth/database_hooks.rb', line 5

def adapter
  @adapter
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/better_auth/database_hooks.rb', line 5

def options
  @options
end

Instance Method Details

#create(data, model, custom: nil, context: nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/better_auth/database_hooks.rb', line 12

def create(data, model, custom: nil, context: nil)
  run_before(model, :create, data, context) do |actual_data|
    created = custom ? custom.call(actual_data) : adapter.create(model: model, data: actual_data, force_allow_id: true)
    run_after(model, :create, created)
    created
  end
end

#delete(where, model, custom: nil, context: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/better_auth/database_hooks.rb', line 36

def delete(where, model, custom: nil, context: nil)
  entity = adapter.find_one(model: model, where: where)
  return custom ? custom.call(where) : adapter.delete(model: model, where: where) unless entity

  return nil if before_hooks(model, :delete).any? { |hook| hook.call(entity, context) == false }

  deleted = custom ? custom.call(where) : adapter.delete(model: model, where: where)
  after_hooks(model, :delete).each { |hook| hook.call(entity, context) }
  deleted
end

#delete_many(where, model, custom: nil, context: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/better_auth/database_hooks.rb', line 47

def delete_many(where, model, custom: nil, context: nil)
  entities = adapter.find_many(model: model, where: where)
  entities.each do |entity|
    return nil if before_hooks(model, :delete).any? { |hook| hook.call(entity, context) == false }
  end
  deleted = custom ? custom.call(where) : adapter.delete_many(model: model, where: where)
  entities.each { |entity| after_hooks(model, :delete).each { |hook| hook.call(entity, context) } }
  deleted
end

#update(data, where, model, custom: nil, context: nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/better_auth/database_hooks.rb', line 20

def update(data, where, model, custom: nil, context: nil)
  run_before(model, :update, data, context) do |actual_data|
    updated = custom ? custom.call(actual_data) : adapter.update(model: model, where: where, update: actual_data)
    run_after(model, :update, updated) if updated
    updated
  end
end

#update_many(data, where, model, custom: nil, context: nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/better_auth/database_hooks.rb', line 28

def update_many(data, where, model, custom: nil, context: nil)
  run_before(model, :update, data, context) do |actual_data|
    updated = custom ? custom.call(actual_data) : adapter.update_many(model: model, where: where, update: actual_data)
    run_after(model, :update, updated) if updated
    updated
  end
end