Class: RubyCode::Models::Base
- Inherits:
-
Object
- Object
- RubyCode::Models::Base
- Defined in:
- lib/rubycode/models/base.rb
Overview
Base class for all models that use the database
Direct Known Subclasses
Class Method Summary collapse
- .all ⇒ Object
- .count ⇒ Object
-
.dataset ⇒ Object
Expose Sequel dataset for direct querying Subclasses must implement table_name.
- .delete ⇒ Object
- .first ⇒ Object
- .last ⇒ Object
- .latest(field = :id) ⇒ Object
- .oldest(field = :id) ⇒ Object
- .order(field, direction = :asc) ⇒ Object
-
.table_name ⇒ Object
Subclasses must define their table name.
-
.where(*args) ⇒ Object
Delegate common Sequel methods to dataset.
Class Method Details
.all ⇒ Object
39 40 41 |
# File 'lib/rubycode/models/base.rb', line 39 def all dataset.all end |
.count ⇒ Object
51 52 53 |
# File 'lib/rubycode/models/base.rb', line 51 def count dataset.count end |
.dataset ⇒ Object
Expose Sequel dataset for direct querying Subclasses must implement table_name
10 11 12 |
# File 'lib/rubycode/models/base.rb', line 10 def dataset Database.connection[table_name] end |
.delete ⇒ Object
35 36 37 |
# File 'lib/rubycode/models/base.rb', line 35 def delete dataset.delete end |
.first ⇒ Object
43 44 45 |
# File 'lib/rubycode/models/base.rb', line 43 def first dataset.first end |
.last ⇒ Object
47 48 49 |
# File 'lib/rubycode/models/base.rb', line 47 def last dataset.order(:id).last end |
.latest(field = :id) ⇒ Object
27 28 29 |
# File 'lib/rubycode/models/base.rb', line 27 def latest(field = :id) order(field, :desc) end |
.oldest(field = :id) ⇒ Object
31 32 33 |
# File 'lib/rubycode/models/base.rb', line 31 def oldest(field = :id) order(field, :asc) end |
.order(field, direction = :asc) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/rubycode/models/base.rb', line 19 def order(field, direction = :asc) if direction == :desc dataset.order(Sequel.desc(field)) else dataset.order(field) end end |
.table_name ⇒ Object
Subclasses must define their table name
56 57 58 |
# File 'lib/rubycode/models/base.rb', line 56 def table_name raise NotImplementedError, "Subclasses must define table_name" end |
.where(*args) ⇒ Object
Delegate common Sequel methods to dataset
15 16 17 |
# File 'lib/rubycode/models/base.rb', line 15 def where(*args) dataset.where(*args) end |