pg_conn 0.53.0 → 0.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +19 -11
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c7a478850b588c635e78fd50cac46e52665ece96fc538eb467eb2bf88fe6d6d7
|
|
4
|
+
data.tar.gz: 8882d70f0289f8b3cdec8c9e3135513788eb09416a0ceb5ad763849f71324d95
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5c3709874689cbfadf194fde8e4dd1d956ab0456ab205fd70064b680aa5dcebc161e11e5f3441237f502bf2c4df7cafa5a9a9b6608df42cd21ddc75848062c64
|
|
7
|
+
data.tar.gz: b791c4afaefddc8286abf836abdbb6e92657bb5139bc377be032f174a6d2888dc774c8f8fd3c8643c6ca9f98f920d3f747d3163380c2fa85569a373ac30e45f0
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
|
@@ -686,23 +686,31 @@ module PgConn
|
|
|
686
686
|
r.each.to_a.map(&:to_h)
|
|
687
687
|
end
|
|
688
688
|
|
|
689
|
-
# Return a record as a OpenStruct object
|
|
690
|
-
#
|
|
691
|
-
# ruby symbol
|
|
692
|
-
|
|
693
|
-
|
|
689
|
+
# Return a record as a OpenStruct object or as a :klass object if present.
|
|
690
|
+
# It is an error if the query returns more than one record and it blows up
|
|
691
|
+
# if a column name is not a valid ruby symbol. The :klass argument should
|
|
692
|
+
# be a class derived from OpenStruct. Eg.
|
|
693
|
+
#
|
|
694
|
+
# class Person
|
|
695
|
+
# def name = first_name + " " + last_name
|
|
696
|
+
# end
|
|
697
|
+
# person = db.struct "persons", 42, klass: Person
|
|
698
|
+
# puts person.name => "Alice Brock"
|
|
699
|
+
#
|
|
700
|
+
def struct(*query, klass: OpenStruct)
|
|
701
|
+
klass.new(**record(parse_query *query))
|
|
694
702
|
end
|
|
695
703
|
|
|
696
704
|
# Like #struct but returns nil if no record was found
|
|
697
|
-
def struct?(*query)
|
|
705
|
+
def struct?(*query, klass: OpenStruct)
|
|
698
706
|
args = record?(parse_query *query)
|
|
699
707
|
return nil if args.nil?
|
|
700
|
-
|
|
708
|
+
klass.new(**args)
|
|
701
709
|
end
|
|
702
710
|
|
|
703
711
|
# Return an array of OpenStruct objects
|
|
704
|
-
def structs(*query)
|
|
705
|
-
records(parse_query *query).map { |record|
|
|
712
|
+
def structs(*query, klass: OpenStruct)
|
|
713
|
+
records(parse_query *query).map { |record| klass.new(**record) }
|
|
706
714
|
end
|
|
707
715
|
|
|
708
716
|
# Return a hash from the record id column to record (hash from column name
|
|
@@ -739,7 +747,7 @@ module PgConn
|
|
|
739
747
|
# of the record. If the :key_column option is defined it will be used
|
|
740
748
|
# instead of id as the key. It is an error if the id field value is not
|
|
741
749
|
# unique
|
|
742
|
-
def set(*query, key_column: :id)
|
|
750
|
+
def set(*query, key_column: :id, klass: OpenStruct)
|
|
743
751
|
key_column = key_column.to_sym
|
|
744
752
|
keys = {}
|
|
745
753
|
r = pg_exec(parse_query *query)
|
|
@@ -750,7 +758,7 @@ module PgConn
|
|
|
750
758
|
end
|
|
751
759
|
h = {}
|
|
752
760
|
for i in 0...r.ntuples
|
|
753
|
-
struct =
|
|
761
|
+
struct = klass.new(**r[i])
|
|
754
762
|
key = struct.send(key_column)
|
|
755
763
|
!h.key?(key) or raise Error, "Duplicate key: #{key.inspect}"
|
|
756
764
|
h[key] = struct
|