Class: MyActiveRecord::Base
- Inherits:
-
Object
- Object
- MyActiveRecord::Base
show all
- Extended by:
- ActiveSupport::DescendantsTracker, Associations
- Defined in:
- lib/my_active_record/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
belongs_to, has_many, has_one
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
10
11
12
13
14
15
|
# File 'lib/my_active_record/base.rb', line 10
def initialize(params = {})
params.each do |key, value|
self[key] = value
end
self
end
|
Class Method Details
.create(params) ⇒ Object
49
50
51
|
# File 'lib/my_active_record/base.rb', line 49
def create(params)
new(params).save
end
|
.fields ⇒ Object
29
30
31
|
# File 'lib/my_active_record/base.rb', line 29
def fields
@fields
end
|
.fields=(fields_list) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/my_active_record/base.rb', line 22
def fields=(fields_list)
fields_list.each do |field_name|
attr_accessor field_name
end
@fields = fields_list
end
|
.find(model_id) ⇒ Object
33
34
35
|
# File 'lib/my_active_record/base.rb', line 33
def find(model_id)
where(:id => model_id).first
end
|
.last ⇒ Object
53
54
55
|
# File 'lib/my_active_record/base.rb', line 53
def last
Database.last(table_name)
end
|
.load_data(data) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/my_active_record/base.rb', line 41
def load_data(data)
model = new
fields.each_with_index do |field_name,index|
model[field_name] = data[index]
end
model
end
|
.table_name ⇒ Object
18
19
20
|
# File 'lib/my_active_record/base.rb', line 18
def table_name
name.to_s.demodulize.underscore.pluralize
end
|
.where(params) ⇒ Object
37
38
39
|
# File 'lib/my_active_record/base.rb', line 37
def where(params)
Database.where(table_name, params)
end
|
Instance Method Details
#[](key) ⇒ Object
66
67
68
|
# File 'lib/my_active_record/base.rb', line 66
def [](key)
send(key)
end
|
#[]=(key, value) ⇒ Object
70
71
72
|
# File 'lib/my_active_record/base.rb', line 70
def []=(key, value)
send("#{key}=", value)
end
|
#save ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/my_active_record/base.rb', line 58
def save
if self.id
Database.update(self.class.table_name, self)
else
Database.insert(self.class.table_name, self)
end
end
|
#to_row ⇒ Object
74
75
76
77
78
|
# File 'lib/my_active_record/base.rb', line 74
def to_row
self.class.fields.map do |field|
self[field]
end
end
|