Class: PGAdaptor
- Inherits:
-
Object
- Object
- PGAdaptor
- Defined in:
- lib/pg_adaptor.rb,
lib/pg_adaptor/version.rb
Constant Summary collapse
- VERSION =
"0.0.12"
Class Attribute Summary collapse
-
.db ⇒ Object
Returns the value of attribute db.
Instance Method Summary collapse
- #fetch(selector = {}, *args) ⇒ Object
- #find(selector = {}, *args) ⇒ Object
-
#initialize(name, klass) ⇒ PGAdaptor
constructor
A new instance of PGAdaptor.
- #insert(model) ⇒ Object
- #remove(selector = {}) ⇒ Object
- #update(model, query = { id: model.id }) ⇒ Object
- #upsert(model, opts = { field: :id }) ⇒ Object
Constructor Details
#initialize(name, klass) ⇒ PGAdaptor
Returns a new instance of PGAdaptor.
10 11 12 13 14 |
# File 'lib/pg_adaptor.rb', line 10 def initialize name, klass @name = name @table = self.class.db[name.to_sym] @klass = klass end |
Class Attribute Details
.db ⇒ Object
Returns the value of attribute db.
7 8 9 |
# File 'lib/pg_adaptor.rb', line 7 def db @db end |
Instance Method Details
#fetch(selector = {}, *args) ⇒ Object
29 30 31 |
# File 'lib/pg_adaptor.rb', line 29 def fetch selector = {}, *args build @table.select(*fields).where(selector, *args).first end |
#find(selector = {}, *args) ⇒ Object
37 38 39 |
# File 'lib/pg_adaptor.rb', line 37 def find selector = {}, *args @table.select(*fields).where(selector, *args).map { |row| build row } end |
#insert(model) ⇒ Object
16 17 18 |
# File 'lib/pg_adaptor.rb', line 16 def insert model @table.insert process(model) end |
#remove(selector = {}) ⇒ Object
33 34 35 |
# File 'lib/pg_adaptor.rb', line 33 def remove selector = {} @table.where(selector).delete end |
#update(model, query = { id: model.id }) ⇒ Object
25 26 27 |
# File 'lib/pg_adaptor.rb', line 25 def update model, query = { id: model.id } @table.where(query).update process(model) end |
#upsert(model, opts = { field: :id }) ⇒ Object
20 21 22 23 |
# File 'lib/pg_adaptor.rb', line 20 def upsert model, opts = { field: :id } values = process model @table.insert_conflict(target: opts[:field], update: values).insert values end |