Module: LunaPark::Extensions::Repositories::Postgres::Read

Defined in:
lib/luna_park/extensions/repositories/postgres/read.rb

Instance Method Summary collapse

Instance Method Details

#all(**scope) ⇒ Object



41
42
43
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 41

def all(**scope)
  read_all(scoped(**scope).order { created_at.desc })
end

#count(**scope) ⇒ Object



37
38
39
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 37

def count(**scope)
  scoped(**scope).count
end

#find(pk_value, **scope) ⇒ Object



24
25
26
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 24

def find(pk_value, **scope)
  read_one scoped(**scope).where(primary_key => pk_value)
end

#find!(pk_value, **scope) ⇒ Object



20
21
22
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 20

def find!(pk_value, **scope)
  found! find(pk_value, **scope), not_found_by: pk_value
end

#first(**scope) ⇒ Object



45
46
47
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 45

def first(**scope)
  read_one scoped(**scope).order(:created_at).first
end

#last(**scope) ⇒ Object



49
50
51
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 49

def last(**scope)
  read_one scoped(**scope).order(:created_at).last
end

#lock(pk_value) ⇒ Object



12
13
14
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 12

def lock(pk_value)
  transaction { yield find pk_value, for_update: true }
end

#lock!(pk_value) ⇒ Object



8
9
10
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 8

def lock!(pk_value)
  transaction { yield find! pk_value, for_update: true }
end

#reload!(entity, **scope) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 28

def reload!(entity, **scope)
  new_rows = scoped(**scope).where(primary_key => entity.public_send(primary_key))
  found! new_rows, not_found_by: { primary_key => entity.public_send(primary_key) }

  new_attrs = from_row __one_from__ new_rows
  entity.set_attributes(new_attrs)
  entity
end

#transaction(&block) ⇒ Object



16
17
18
# File 'lib/luna_park/extensions/repositories/postgres/read.rb', line 16

def transaction(&block)
  dataset.transaction(&block)
end