-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystorefinal.rb
98 lines (85 loc) · 2.09 KB
/
mystorefinal.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
module MyObjectStore
def self.included(klass)
class << klass
attr_accessor :saved_objects, :validators
def count
saved_objects.length
end
def collect
saved_objects
end
def validate_presence_of(*args)
args.each do |name|
method_name = "check_presence_of_#{ name }"
validators << method_name
define_method(method_name) do
errors[name] << "#{ name } must exist" unless public_send(name)
end
end
end
def validate_numericality_of(*args)
args.each do |name|
method_name = "check_numericalilty_of_#{ name }"
validators << method_name
define_method(method_name) do
if public_send(name)
errors[name] << 'must be an integer' unless public_send(name).is_a?(Integer)
end
end
end
end
end
klass.singleton_class.class_eval do
klass::DYNAMIC_FINDERS.each do |param|
define_method("find_by_#{ param }") do |value|
saved_objects.find_all { |object| object.public_send(param).eql? value }
end
end
end
end
def save
validate
check_errors_and_save
end
private
def check_errors_and_save
if errors.empty?
self.class.saved_objects << self
"created object #{ self.inspect }"
else
errors
end
end
end
class Play
DYNAMIC_FINDERS = [:fname, :lname, :age, :email]
@saved_objects = []
@validators = []
include MyObjectStore
attr_accessor :fname, :lname, :age, :email, :errors
validate_presence_of :fname, :age
validate_numericality_of :age
def initialize
self.errors = Hash.new { |hash, key| hash[key] = [] }
end
def validate
errors.clear
self.class.validators.each do |method_name|
public_send(method_name)
end
end
end
p2 = Play.new
p2.fname = 'abc'
p2.lname = 'def'
p2.email = '[email protected]'
puts p2.save
p3 = Play.new
p3.fname = 'himanshu'
p3.lname = 'mishra'
p3.email = '[email protected]'
p3.age = 1
puts p3.save
p Play.collect
p Play.count
Play.find_by_fname('xyz')