-
Notifications
You must be signed in to change notification settings - Fork 0
/
testsingleton.rb
102 lines (87 loc) · 2.14 KB
/
testsingleton.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
99
100
101
102
module MyObjectStore
def self.included(klass)
class << klass
attr_accessor :array_object, :find_by, :validate, :validators
def count
array_object.length
end
def collect
array_object
end
def validate_presence_of(*args)
@validate = args # no need of this
args.each do |name|
@validators << "is_exist_#{name}"
# puts all methods in validators
define_method("is_exist_#{name}") do
return true if public_send(name.to_s)
false
end
end
end
end
klass.singleton_class.class_eval do
klass::FIND_BY.each do |param|
define_method("find_by_#{param}") do |value|
array_object.find_all { |x| x.public_send(param).eql? value}
end
end
end
end
def save
validate
if @errors.empty?
self.class.array_object << self
"created object #{self}"
else
@errors
end
end
end
class Play
FIND_BY = [:fname, :lname, :age, :email]
@array_object = []
@validators = []
# create a validator and puts all the validators
include MyObjectStore
attr_accessor :fname ,:lname ,:age ,:email
validate_presence_of :fname ,:age
# validate_numerical_of
def initialize
@errors = Hash.new(Array.new)
#use diff syntax
# study difference between both syntax
# study instance eval class eval self, singleton class
end
def validate
# call all functions in validators and it will set the errors
# here only validators will be called
# @errors[:age] += ['Age should be a integer'] unless age.is_a?(Integer)
self.class.validate.each do |param|
@errors[param] += ["#{param} must exist"] unless eval("is_exist_#{param}")
end
end
end
p2 = Play.new
p2.fname = "abc"
p2.lname = "def"
p2.email = "[email protected]"
# puts p2.save
p2.age = 1
puts p2.save
# puts Play.validators
# p3 = Play.new
# p3.fname = "bsd"
# p3.age = 12
# p4 = Play.new
# p4.fname ="bc"
# p5 = Play.new
# p5.age ="bc"
# puts p2.save
# puts p3.save
# puts p4.save
# puts p5.save
# p Play.collect
# p Play.count
p Play.find_by_fname('abc')
p Play.find_by_lname('def')