Class: Hemp::BaseRecord

Inherits:
Orm::RecordInterface show all
Includes:
Orm::ClassInstanceVars
Defined in:
lib/hemp/base_record.rb

Constant Summary

Constants inherited from Orm::RecordInterface

Orm::RecordInterface::SqlHelper

Instance Method Summary collapse

Methods included from Orm::ClassInstanceVars

#internal_props, #properties, #sql_properties, #table_name

Methods inherited from Orm::RecordInterface

all, construct_sql_properties, count, create, create_table, destroy, destroy_all, expose_instance_vars, find, get_properties, initialize_model, property, set_instance_vars, to_table

Constructor Details

#initialize(hash_arg = {}) ⇒ BaseRecord



12
13
14
15
16
# File 'lib/hemp/base_record.rb', line 12

def initialize(hash_arg = {})
  self.class.expose_instance_vars
  return super() if hash_arg.empty?
  save_model_attributes hash_arg
end

Instance Method Details

#==(other) ⇒ Object



90
91
92
93
94
# File 'lib/hemp/base_record.rb', line 90

def ==(other)
  instance_variables.each do |var|
    instance_variable_get(var) == other.instance_variable_get(var)
  end
end

#destroyObject



48
49
50
# File 'lib/hemp/base_record.rb', line 48

def destroy
  self.class.destroy id
end

#get_save_queryObject



52
53
54
55
56
57
# File 'lib/hemp/base_record.rb', line 52

def get_save_query
  sql_values = stringify_values.join(", ")

  "insert into #{table_name} (#{sql_properties})"\
    " values (#{sql_values})"
end

#get_update_queryObject



59
60
61
62
63
64
# File 'lib/hemp/base_record.rb', line 59

def get_update_query
  sql_update_properties = get_update_values.join(", ")

  "update #{table_name} set #{sql_update_properties}"\
    " where id = #{id}"
end

#get_update_valuesObject



81
82
83
84
85
86
87
88
# File 'lib/hemp/base_record.rb', line 81

def get_update_values
  all_update_properties = []
  stringify_values.each_with_index do |value, index|
    all_update_properties << (internal_props[index].to_s + " = " + value)
  end

  all_update_properties
end

#get_valuesObject



66
67
68
69
70
71
72
73
# File 'lib/hemp/base_record.rb', line 66

def get_values
  values = []
  internal_props.each do |column|
    values << (instance_variable_get("@#{column}") || "NULL")
  end

  values
end

#saveObject



27
28
29
30
31
32
33
34
# File 'lib/hemp/base_record.rb', line 27

def save
  row = self.class.find id
  return update if row
  SqlHelper.execute get_save_query
  set_id_value
rescue SQLite3::ConstraintException
  false
end

#save_model_attributes(hash_arg) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/hemp/base_record.rb', line 18

def save_model_attributes(hash_arg)
  row = []
  properties.map do |property|
    row << hash_arg.values_at(property.name)
  end

  self.class.set_instance_vars(self, row.flatten)
end

#set_id_valueObject



36
37
38
39
40
# File 'lib/hemp/base_record.rb', line 36

def set_id_value
  id = SqlHelper.execute("select last_insert_rowid() "\
    "from #{table_name}").first.first
  instance_variable_set "@id", id
end

#stringify_valuesObject



75
76
77
78
79
# File 'lib/hemp/base_record.rb', line 75

def stringify_values
  get_values.map do |val|
    (val == "NULL") || (val == "") ? "NULL" : %('#{val}')
  end
end

#updateObject



42
43
44
45
46
# File 'lib/hemp/base_record.rb', line 42

def update
  SqlHelper.execute get_update_query
rescue SQLite3::ConstraintException
  false
end