forked from sf-wdi-21/hacktive_record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhacktive_record.rb
59 lines (45 loc) · 1.01 KB
/
hacktive_record.rb
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
47
48
49
50
51
52
53
54
55
56
57
58
59
## This simplifed ORM will give your models the ability to
## "save", "create", and get "all" instances of a model,
## by inheriting from HacktiveRecord::Base.
class HacktiveRecord
class Base
####################
# Instance Methods #
####################
attr_reader :id
def initialize(**kwargs)
kwargs.each_pair do |name, arg|
define_getter(name)
define_setter(name, arg)
end
end
def save
assign_id
self.class.all << self
self
end
#################
# Class Methods #
#################
def self.all
@records ||= []
end
def self.create(**kwargs)
self.new(kwargs).save
end
private
def assign_id
@id = self.id || last_id + 1
end
def last_id
last = self.class.all.last
last && last.id || 0
end
def define_getter(name)
instance_variable_get("@#{name}")
end
def define_setter(name, arg)
instance_variable_set("@#{name}", arg)
end
end
end