Class: TangoOrm::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tango_orm/model/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



29
30
31
32
# File 'lib/tango_orm/model/base.rb', line 29

def initialize(options = {})
  options = options.merge(id: nil)
  options.each {|name, value| instance_variable_set("@#{name}", value)}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tango_orm/model/base.rb', line 34

def method_missing(method_name, *arguments, &block)
  attribute_getters = self.class.columns
  attribute_setters = attribute_getters.map{|getter| "#{getter}=".to_sym}

  if attribute_getters.include?(method_name)
    instance_variable_get("@#{method_name}")
  elsif attribute_setters.include?(method_name)
    name = method_name.to_s.delete("=").to_sym
    instance_variable_set("@#{name}", arguments[0])
  else
    super
  end
end

Class Method Details

.allObject



139
140
141
142
143
144
145
# File 'lib/tango_orm/model/base.rb', line 139

def self.all
  db.exec("SELECT * FROM #{table_name}") do |result|
    result.map do |row|
      new_from_db(row)
    end
  end
end

.columnsObject



19
20
21
22
23
24
25
26
27
# File 'lib/tango_orm/model/base.rb', line 19

def self.columns
  sql = <<-SQL
        SELECT *
        FROM information_schema.columns
        WHERE table_name = '#{table_name}';
      SQL
  names = db.exec(sql)
  names.map{|name| name["column_name"].to_sym}
end

.create(options) ⇒ Object



105
106
107
108
# File 'lib/tango_orm/model/base.rb', line 105

def self.create(options)
  new_song = new(options)
  new_song.save
end

.create_table(options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tango_orm/model/base.rb', line 55

def self.create_table(options)
  formatted_options = {}
  options.each do |key, value|
    formatted_options[key] = value.upcase.gsub(/[,_]/, " ")
  end

  column_config = ""
  formatted_options.each do |key, value|
    line = "#{key.to_s} #{value}, "
    column_config << line
  end

  column_config = column_config.chomp(", ")

  sql = <<-SQL
    CREATE TABLE #{table_name} (
      id SERIAL PRIMARY KEY,
      #{column_config}
      );
    SQL

  db.exec(sql)
  puts "Table '#{table_name}' created successfully"
end

.dbObject



11
12
13
# File 'lib/tango_orm/model/base.rb', line 11

def self.db
  @@db ||= establish_connection
end

.drop_tableObject



80
81
82
83
84
# File 'lib/tango_orm/model/base.rb', line 80

def self.drop_table
  sql = "DROP TABLE #{table_name}"
  db.exec(sql)
  puts "Table '#{table_name}' dropped successfully"
end

.establish_connectionObject



15
16
17
# File 'lib/tango_orm/model/base.rb', line 15

def self.establish_connection
  DBConnection.set_connection
end

.find_by_id(id) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/tango_orm/model/base.rb', line 129

def self.find_by_id(id)
  sql = "SELECT * FROM #{table_name} WHERE id = $1"

  db.exec(sql, [id]) do |result|
    result.map do |row|
      new_from_db(row)
    end
  end.first
end

.table_nameObject



7
8
9
# File 'lib/tango_orm/model/base.rb', line 7

def self.table_name
  self.name.downcase.pluralize
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/tango_orm/model/base.rb', line 48

def respond_to_missing?(method_name, include_private = false)
  getters = self.class.columns
  setters = getters.map{|getter| "#{getter}=".to_sym}

  getters.include?(method_name) || setters.include?(method_name) || super
end

#saveObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tango_orm/model/base.rb', line 86

def save
  db = self.class.db
  db_table = self.class.table_name
  variable_names = attributes.join(", ")
  variable_numbers = (1..attributes.count).map{|i| "$#{i}" }.join(", ")
  variable_values = attributes.map{|var| send(var)}

  sql = <<~SQL
    INSERT INTO #{db_table} (#{variable_names})
    VALUES (#{variable_numbers})
    RETURNING id;
  SQL

  db.exec(sql, variable_values) do |result|
    self.id = result[0]["id"].to_i
  end
  self
end

#updateObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tango_orm/model/base.rb', line 110

def update
  db = self.class.db
  db_table = self.class.table_name
  numbered_var_names = attributes.map.with_index {|attr, i| "#{attr} = $#{i + 1}"}.join(", ")
  variable_values = attributes.map{|var| send(var)} + [self.id]
  id_value = "$#{variable_values.count}"

  sql = <<~SQL
    UPDATE #{db_table}
    SET #{numbered_var_names}
    WHERE id = #{id_value}
    RETURNING *;
  SQL

  db.exec(sql, variable_values) do |result|
    self.class.new_from_db(result[0])
  end
end