Class: Hemp::Orm::RecordInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/hemp/orm/record_interface.rb

Direct Known Subclasses

BaseRecord

Constant Summary collapse

SqlHelper =
Hemp::Orm::SqlHelper

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.internal_propsObject

Returns the value of attribute internal_props.



7
8
9
# File 'lib/hemp/orm/record_interface.rb', line 7

def internal_props
  @internal_props
end

.propertiesObject

Returns the value of attribute properties.



7
8
9
# File 'lib/hemp/orm/record_interface.rb', line 7

def properties
  @properties
end

.sql_propertiesObject

Returns the value of attribute sql_properties.



7
8
9
# File 'lib/hemp/orm/record_interface.rb', line 7

def sql_properties
  @sql_properties
end

.table_nameObject

Returns the value of attribute table_name.



7
8
9
# File 'lib/hemp/orm/record_interface.rb', line 7

def table_name
  @table_name
end

Class Method Details

.allObject



83
84
85
86
# File 'lib/hemp/orm/record_interface.rb', line 83

def all
  rows = SqlHelper.execute "select * from #{@table_name}"
  rows.map { |row| initialize_model row }
end

.construct_sql_propertiesObject



45
46
47
48
# File 'lib/hemp/orm/record_interface.rb', line 45

def construct_sql_properties
  @internal_props = get_properties
  @sql_properties = @internal_props.join(", ")
end

.countObject



77
78
79
80
81
# File 'lib/hemp/orm/record_interface.rb', line 77

def count
  row = SqlHelper.execute "select count(*) from #{@table_name}"

  row.first.first
end

.create(hash_arg) ⇒ Object



15
16
17
18
19
# File 'lib/hemp/orm/record_interface.rb', line 15

def create(hash_arg)
  new_model = const_get(name).new(hash_arg)

  new_model.save ? new_model : false
end

.create_tableObject



37
38
39
40
41
42
43
# File 'lib/hemp/orm/record_interface.rb', line 37

def create_table
  SqlHelper.execute "create table if not exists "\
  "#{@table_name} (#{@properties.join(', ')})"

  construct_sql_properties
  expose_instance_vars
end

.destroy(id) ⇒ Object



88
89
90
91
# File 'lib/hemp/orm/record_interface.rb', line 88

def destroy(id)
  SqlHelper.execute "delete from #{@table_name} "\
  "where id = ?", [id]
end

.destroy_allObject



93
94
95
# File 'lib/hemp/orm/record_interface.rb', line 93

def destroy_all
  SqlHelper.execute "delete from #{@table_name}"
end

.expose_instance_varsObject



50
51
52
53
54
# File 'lib/hemp/orm/record_interface.rb', line 50

def expose_instance_vars
  @properties.map(&:name).each do |name|
    const_get(self.name).class_eval { attr_accessor name.to_sym }
  end
end

.find(id) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/hemp/orm/record_interface.rb', line 69

def find(id)
  row = SqlHelper.execute "select * from #{@table_name} "\
  "where id = ?", [id]

  return initialize_model(row.first) unless row.empty?
  nil
end

.get_propertiesObject



30
31
32
33
34
35
# File 'lib/hemp/orm/record_interface.rb', line 30

def get_properties
  props = @properties.map(&:name)
  props.delete :id

  props
end

.initialize_model(row) ⇒ Object



62
63
64
65
66
67
# File 'lib/hemp/orm/record_interface.rb', line 62

def initialize_model(row)
  model = const_get(name).new
  set_instance_vars model, row

  model
end

.property(name, options) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/hemp/orm/record_interface.rb', line 21

def property(name, options)
  @properties ||= []
  new_property = Property.new(name, options)
  @properties << new_property unless @properties.include? new_property
rescue DatabaseError => e
  puts e.message
  exit
end

.set_instance_vars(scope, row) ⇒ Object



56
57
58
59
60
# File 'lib/hemp/orm/record_interface.rb', line 56

def set_instance_vars(scope, row)
  @properties.map(&:name).each_with_index do |name, index|
    scope.instance_variable_set "@#{name}", row[index]
  end
end

.to_table(name) ⇒ Object



10
11
12
13
# File 'lib/hemp/orm/record_interface.rb', line 10

def to_table(name)
  @table_name = name
  property :id, type: :integer, primary_key: true
end