Class: JunglePath::DBAccess::IO::Select

Inherits:
Object
  • Object
show all
Includes:
InitDB
Defined in:
lib/jungle_path/db_access/io/select.rb

Instance Method Summary collapse

Methods included from InitDB

#handle_json_columns, #initialize

Instance Method Details

#_model(model) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/jungle_path/db_access/io/select.rb', line 9

def _model(model)
  # select based on a model's primary key.
  ds = @db[model._table_name].where(model._primary_key)
  #puts ds.sql
  hash = ds.first
  return nil unless hash
  new_model = model.class.new(hash, false) # false since row (initial values) from db is considered unmodified.
end

#_model_by_any(model) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/jungle_path/db_access/io/select.rb', line 18

def _model_by_any(model)
  ds = @db[model._table_name].where(handle_json_columns(model, model._modified_hash))
  #puts ds.sql
  hash = ds.first
  #puts "hash: #{hash}. Nil? #{hash == nil}."
  return nil unless hash
  new_model = model.class.new(hash, false) # false since row (initial values) from db is considered unmodified.
end

#_models(model, *order_by) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jungle_path/db_access/io/select.rb', line 27

def _models(model, *order_by)
  # select all of a given model.
  ds = @db[model._table_name]
  ds = ds.where(handle_json_columns(model, model._has_value_hash)) if model._has_value_hash.length > 0
  ds = ds.order(*order_by) if order_by.length > 0
  ds = ds.order(model._primary_key_columns.keys) unless order_by.length > 0
  #puts ds.sql
  rows = ds.all
  models = []
  rows.each do |row|
    models << model.class.new(row, false)
  end
  models
end