Class: BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/models/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fieldsObject



13
14
15
# File 'lib/models/base.rb', line 13

def self.fields
	JSON.parse(self.new({}).as_json.to_json).keys
end

.registerObject



9
10
11
# File 'lib/models/base.rb', line 9

def self.register
	$map_models[table_name]  = self.name
end

.table_nameObject



5
6
7
# File 'lib/models/base.rb', line 5

def self.table_name
	return ""
end

.verify_keysObject



17
18
19
# File 'lib/models/base.rb', line 17

def self.verify_keys
	return {}
end

Instance Method Details

#as_jsonObject



21
22
23
# File 'lib/models/base.rb', line 21

def as_json
	{}
end

#verifyObject



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
# File 'lib/models/base.rb', line 25

def verify
	json_data = as_json
	verify_keys = self.class.verify_keys

	verify_keys.each do |key,fns|
		value = json_data[key.to_sym]
		if fns.class.name == "Array"
			fns.each do |fn|
				if fn == "empty"
					if !verify_empty(value)
						puts "ERROR: #{key} cannot be empty!"
						return false
					end
				elsif fn == "json"
					if !verify_json(value)
						puts "ERROR: #{key} json string parse fail!"
						return false
					end
				elsif fn == "string"
					if !verify_string(value)
						puts "ERROR: #{key} field type is not string!"
						return false
					end
				elsif fn == "int"
					if !verify_int(value)
						puts "ERROR: #{key} field type is not int!"
						return false
					end
				elsif fn.include?("regex")
					if !verify_regex(fn,value)
						puts "ERROR: #{key} regex #{fn} match error"
						return false
					end
				elsif fn.include?("length")
					if !verify_length(fn,value)
						puts "ERROR: #{key} length must be #{fn}"
						return false
					end
				elsif fn.include?("fields")
					if !verify_fields(fn,value)
						puts "ERROR: #{key}:#{fn} not Exist!"
						return false
					end
				end
			end
		end
	end
end

#verify_empty(v) ⇒ Object



74
75
76
77
78
79
# File 'lib/models/base.rb', line 74

def verify_empty(v)
	if v == nil || v == "" 
		return false
	end
	return true
end

#verify_fields(fn, v) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/models/base.rb', line 160

def verify_fields(fn,v)
	if fn.class.name != "String"
		return false
	end		
	fields_arr = fn.split(":")
	if fields_arr.size < 2
		return false
	end
	fields_str = fields_arr[1]

	fields = fields_str.split(",")

	begin
		datas = JSON.parse(v)
		datas.each do |data|
			fields.each do |field|
				if data[field] == nil
					puts "ERROR #{field} not Exist!"
					return false
				end
			end
		end
	rescue StandardError => e
		puts e
		return false
	end

	return true
end

#verify_int(v) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/models/base.rb', line 101

def verify_int(v)
	if v == nil
		return false
	end

	if  v.class.name == "Integer"
		return true
	end
	return false
end

#verify_json(v) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/models/base.rb', line 81

def verify_json(v)
	begin
		JSON.parse(v)
		return true
	rescue StandardError => e
		return false
	end
	return true
end

#verify_length(fn, v) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/models/base.rb', line 136

def verify_length(fn,v)
	if fn.class.name != "String"
		return false
	end		
	length_arr = fn.split(":")
	if length_arr.size < 2
		return false
	end
	length = length_arr[1].to_i
	if length == 0
		return false
	end		

	if v == nil
		return false
	end

	if v.to_s.size == length
		return true
	end

	return false
end

#verify_regex(fn, v) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/models/base.rb', line 112

def verify_regex(fn,v)
	if fn.class.name != "String"
		return false
	end		
	regex_arr = fn.split(":")
	if regex_arr.size < 2
		return false
	end
	regex_str = regex_arr[1]
	if regex_str.size == 0
		return false
	end

	if v == nil || v == ""
		return false
	end

	if v =~ Regexp.new(regex_str)
	  return true
	else
		return false
	end
end

#verify_string(v) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/models/base.rb', line 91

def verify_string(v)
	if v == nil
		return false
	end
	if v.class.name == "String"
		return true
	end
	return false
end