forked from owningrails/patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_record.rb
More file actions
46 lines (38 loc) · 895 Bytes
/
active_record.rb
File metadata and controls
46 lines (38 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require "connection_adapter"
module ActiveRecord
class Base < Object
@@connection = SqliteAdapter.new
def initialize(attributes)
@attributes = attributes
end
def method_missing(name, *args)
columns = @@connection.columns(self.class.table_name)
if columns.include?(name)
@attributes[name]
else
super
end
end
def id
@attributes[:id]
end
def self.find(id)
attributes = @@connection.find(id, table_name)
new(attributes)
end
def self.all
@@connection.find_all(table_name).map do |attributes|
new(attributes)
end
# Whitout `map`
# array = []
# @@connection.find_all(table_name).each do |attributes|
# array << new(attributes)
# end
# array
end
def self.table_name
name.downcase + "s"
end
end
end