Class: PGB::Model

Inherits:
Object
  • Object
show all
Extended by:
Enumerable, Shortcuts
Includes:
Shortcuts
Defined in:
lib/pgb/model.rb

Overview

TODO Explicit table names TODO Support queries for from TODO Automatically infer validations from db constraints TODO Do not litter this class with methods since others inherit from it (e.g. parse) TODO Support explicitly setting table_name / query / etc TODO table_name changing at runtime TODO Defined attributes might be needed before connecting to the database TODO Support keyword attributes for #new and make parse use that TODO #inspect TODO #to_s TODO Module inclusions on relations? TODO Include enumerable methods unless they clash with existing TODO Enumerable methods should act on current selection TODO Check all enumerable methods are functional (e.g. sort, to_h) TODO Implement base methods, e.g. ==, etc TODO Check Enumerable and Shortcuts methods clashing (e.g. []) TODO Lazy for enumerable

Direct Known Subclasses

User

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Shortcuts

[], execute, from, func, lit, select

Class Attribute Details

.table_nameObject

Returns the value of attribute table_name.



29
30
31
# File 'lib/pgb/model.rb', line 29

def table_name
  @table_name
end

Class Method Details

.allObject

TODO Only execute when data is required TODO Schema auto reload? TODO Schema manual reload? TODO Called again? Remove all conditions or do nothing?



44
45
46
# File 'lib/pgb/model.rb', line 44

def all
  from(table_name).then { execute(_1) }.map { parse(_1) }
end

.columnsObject



58
59
60
# File 'lib/pgb/model.rb', line 58

def columns
  schema.keys
end

.eachObject



36
37
38
# File 'lib/pgb/model.rb', line 36

def each
  all.each { yield _1 }
end

.inherited(klass) ⇒ Object



31
32
33
34
# File 'lib/pgb/model.rb', line 31

def inherited(klass)
  # TODO Bad method for setting table name
  klass.table_name = "#{klass.name.downcase.split('::').last}s"
end

.schemaObject

TODO Column names clashing with existing methods TODO Overwriting column readers / writers



50
51
52
53
54
55
56
# File 'lib/pgb/model.rb', line 50

def schema
  @schema ||= class_exec do
    PGB.schema(table_name).each_key do
      attr_accessor _1
    end
  end
end