baza 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +157 -0
- data/Gemfile +11 -9
- data/Gemfile.lock +41 -9
- data/README.md +1 -1
- data/Rakefile +19 -16
- data/VERSION +1 -1
- data/baza.gemspec +31 -7
- data/config/best_project_practice_rubocop.yml +2 -0
- data/config/best_project_practice_rubocop_todo.yml +157 -0
- data/lib/baza/base_sql_driver.rb +79 -20
- data/lib/baza/cloner.rb +2 -2
- data/lib/baza/column.rb +6 -0
- data/lib/baza/database.rb +33 -0
- data/lib/baza/database_model.rb +26 -0
- data/lib/baza/database_model_functionality.rb +10 -0
- data/lib/baza/database_model_name.rb +16 -0
- data/lib/baza/db.rb +210 -413
- data/lib/baza/dbtime.rb +20 -20
- data/lib/baza/driver.rb +3 -3
- data/lib/baza/drivers/active_record/columns.rb +1 -1
- data/lib/baza/drivers/active_record/indexes.rb +1 -1
- data/lib/baza/drivers/active_record/result.rb +3 -5
- data/lib/baza/drivers/active_record/tables.rb +2 -2
- data/lib/baza/drivers/active_record.rb +30 -16
- data/lib/baza/drivers/mysql/column.rb +51 -48
- data/lib/baza/drivers/mysql/columns.rb +8 -8
- data/lib/baza/drivers/mysql/database.rb +28 -0
- data/lib/baza/drivers/mysql/databases.rb +35 -0
- data/lib/baza/drivers/mysql/index.rb +32 -22
- data/lib/baza/drivers/mysql/result.rb +24 -24
- data/lib/baza/drivers/mysql/sqlspecs.rb +1 -1
- data/lib/baza/drivers/mysql/table.rb +80 -91
- data/lib/baza/drivers/mysql/tables.rb +30 -32
- data/lib/baza/drivers/mysql/unbuffered_result.rb +5 -5
- data/lib/baza/drivers/mysql.rb +127 -220
- data/lib/baza/drivers/mysql2/database.rb +2 -0
- data/lib/baza/drivers/mysql2/databases.rb +2 -0
- data/lib/baza/drivers/mysql2/result.rb +3 -5
- data/lib/baza/drivers/mysql2.rb +21 -106
- data/lib/baza/drivers/mysql_java/database.rb +2 -0
- data/lib/baza/drivers/mysql_java/databases.rb +2 -0
- data/lib/baza/drivers/mysql_java.rb +16 -12
- data/lib/baza/drivers/sqlite3/column.rb +33 -33
- data/lib/baza/drivers/sqlite3/columns.rb +6 -6
- data/lib/baza/drivers/sqlite3/database.rb +2 -0
- data/lib/baza/drivers/sqlite3/databases.rb +25 -0
- data/lib/baza/drivers/sqlite3/index.rb +20 -6
- data/lib/baza/drivers/sqlite3/result.rb +16 -17
- data/lib/baza/drivers/sqlite3/sqlspecs.rb +1 -1
- data/lib/baza/drivers/sqlite3/table.rb +99 -100
- data/lib/baza/drivers/sqlite3/tables.rb +30 -23
- data/lib/baza/drivers/sqlite3/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3.rb +27 -14
- data/lib/baza/drivers/sqlite3_java/database.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3_java.rb +12 -10
- data/lib/baza/drivers/sqlite3_rhodes.rb +9 -7
- data/lib/baza/dump.rb +55 -55
- data/lib/baza/errors.rb +5 -0
- data/lib/baza/idquery.rb +25 -25
- data/lib/baza/index.rb +6 -0
- data/lib/baza/jdbc_driver.rb +6 -8
- data/lib/baza/jdbc_result.rb +1 -1
- data/lib/baza/model.rb +299 -309
- data/lib/baza/model_custom.rb +74 -72
- data/lib/baza/model_handler.rb +244 -255
- data/lib/baza/model_handler_sqlhelper.rb +122 -132
- data/lib/baza/mysql_base_driver.rb +112 -0
- data/lib/baza/query_buffer.rb +23 -23
- data/lib/baza/result_base.rb +2 -2
- data/lib/baza/revision.rb +67 -67
- data/lib/baza/row.rb +82 -84
- data/lib/baza/sqlspecs.rb +1 -1
- data/lib/baza/table.rb +31 -0
- data/lib/baza.rb +43 -10
- data/shippable.yml +1 -0
- data/spec/cloner_spec.rb +4 -4
- data/spec/drivers/active_record_mysql2_spec.rb +3 -3
- data/spec/drivers/active_record_mysql_spec.rb +2 -2
- data/spec/drivers/active_record_sqlite3_spec.rb +2 -2
- data/spec/drivers/mysql2_spec.rb +1 -0
- data/spec/drivers/mysql_spec.rb +1 -0
- data/spec/drivers/sqlite3_spec.rb +4 -5
- data/spec/info_active_record_example.rb +4 -6
- data/spec/info_active_record_mysql.rb +6 -7
- data/spec/info_active_record_mysql2.rb +2 -4
- data/spec/info_active_record_mysql2_shippable.rb +2 -4
- data/spec/info_active_record_mysql_shippable.rb +2 -4
- data/spec/info_active_record_sqlite3.rb +5 -6
- data/spec/info_mysql2_example.rb +1 -1
- data/spec/info_mysql2_shippable.rb +1 -1
- data/spec/info_mysql_example.rb +1 -1
- data/spec/info_mysql_shippable.rb +1 -1
- data/spec/info_sqlite3.rb +4 -3
- data/spec/model_handler_spec.rb +102 -115
- data/spec/spec_helper.rb +5 -5
- data/spec/support/driver_collection.rb +98 -95
- data/spec/support/driver_columns_collection.rb +18 -22
- data/spec/support/driver_databases_collection.rb +29 -0
- data/spec/support/driver_indexes_collection.rb +7 -6
- data/spec/support/driver_tables_collection.rb +38 -26
- metadata +49 -3
data/lib/baza/model.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#This class helps create models in a framework with Baza::Db and Baza::ModelHandler.
|
1
|
+
# This class helps create models in a framework with Baza::Db and Baza::ModelHandler.
|
2
2
|
#===Examples
|
3
3
|
# db = Baza::Db.new(type: :sqlite3, path: "somepath.sqlite3")
|
4
4
|
# ob = Baza::ModelHandler.new(db: db, datarow: true, path: "path_of_model_class_files")
|
@@ -6,97 +6,101 @@
|
|
6
6
|
class Baza::Model
|
7
7
|
@@refs = {}
|
8
8
|
|
9
|
-
#Returns the Baza::ModelHandler which handels this model.
|
9
|
+
# Returns the Baza::ModelHandler which handels this model.
|
10
10
|
def ob
|
11
|
-
|
11
|
+
self.class.ob
|
12
12
|
end
|
13
13
|
|
14
|
-
#Returns the Baza::Db which handels this model.
|
14
|
+
# Returns the Baza::Db which handels this model.
|
15
15
|
def db
|
16
|
-
|
16
|
+
self.class.db
|
17
17
|
end
|
18
18
|
|
19
|
-
#Returns the 'Baza::ModelHandler'-object that handels this class.
|
20
|
-
|
21
|
-
|
19
|
+
# Returns the 'Baza::ModelHandler'-object that handels this class.
|
20
|
+
class << self
|
21
|
+
attr_reader :ob
|
22
22
|
end
|
23
23
|
|
24
|
-
#Returns the 'Baza::Db'-object that handels this class.
|
25
|
-
|
26
|
-
|
24
|
+
# Returns the 'Baza::Db'-object that handels this class.
|
25
|
+
class << self
|
26
|
+
attr_reader :db
|
27
27
|
end
|
28
28
|
|
29
|
-
#This is used by 'Baza::ModelHandler' to find out what data is required for this class. Returns the array that tells about required data.
|
29
|
+
# This is used by 'Baza::ModelHandler' to find out what data is required for this class. Returns the array that tells about required data.
|
30
30
|
#===Examples
|
31
|
-
#When adding a new user, this can fail if the ':group_id' is not given, or the ':group_id' doesnt refer to a valid group-row in the db.
|
31
|
+
# When adding a new user, this can fail if the ':group_id' is not given, or the ':group_id' doesnt refer to a valid group-row in the db.
|
32
32
|
# class Models::User < Baza::Datarow
|
33
33
|
# has_one [
|
34
|
-
# {:
|
34
|
+
# {class: :Group, col: :group_id, method: :group, required: true}
|
35
35
|
# ]
|
36
36
|
# end
|
37
37
|
def self.required_data
|
38
|
-
@required_data = []
|
39
|
-
|
38
|
+
@required_data = [] unless @required_data
|
39
|
+
@required_data
|
40
40
|
end
|
41
41
|
|
42
|
-
#This is used by 'Baza::ModelHandler' to find out what other objects this class depends on. Returns the array that tells about depending data.
|
42
|
+
# This is used by 'Baza::ModelHandler' to find out what other objects this class depends on. Returns the array that tells about depending data.
|
43
43
|
#===Examples
|
44
|
-
#This will tell Baza::ModelHandler that files depends on users. It can prevent the user from being deleted, if any files depend on it.
|
44
|
+
# This will tell Baza::ModelHandler that files depends on users. It can prevent the user from being deleted, if any files depend on it.
|
45
45
|
# class Models::User < Baza::Datarow
|
46
46
|
# has_many [
|
47
|
-
# {:
|
47
|
+
# {class: :File, col: :user_id, method: :files, depends: true}
|
48
48
|
# ]
|
49
49
|
# end
|
50
|
-
|
51
|
-
|
50
|
+
class << self
|
51
|
+
attr_reader :depending_data
|
52
52
|
end
|
53
53
|
|
54
|
-
#Returns true if this class has been initialized.
|
54
|
+
# Returns true if this class has been initialized.
|
55
55
|
def self.initialized?
|
56
|
-
|
57
|
-
|
56
|
+
false unless @columns_sqlhelper_args
|
57
|
+
true
|
58
58
|
end
|
59
59
|
|
60
|
-
#This is used by 'Baza::ModelHandler' to find out which other objects should be deleted when an object of this class is deleted automatically. Returns the array that tells about autodelete data.
|
60
|
+
# This is used by 'Baza::ModelHandler' to find out which other objects should be deleted when an object of this class is deleted automatically. Returns the array that tells about autodelete data.
|
61
61
|
#===Examples
|
62
|
-
#This will trigger Baza::ModelHandler to automatically delete all the users pictures, when deleting the current user.
|
62
|
+
# This will trigger Baza::ModelHandler to automatically delete all the users pictures, when deleting the current user.
|
63
63
|
# class Models::User < Baza::Datarow
|
64
64
|
# has_many [
|
65
|
-
# {:
|
65
|
+
# {class: :Picture, col: :user_id, method: :pictures, autodelete: true}
|
66
66
|
# ]
|
67
67
|
# end
|
68
|
-
|
69
|
-
|
68
|
+
class << self
|
69
|
+
attr_reader :autodelete_data
|
70
70
|
end
|
71
71
|
|
72
|
-
#Returns the autozero-data (if any).
|
73
|
-
|
74
|
-
|
72
|
+
# Returns the autozero-data (if any).
|
73
|
+
class << self
|
74
|
+
attr_reader :autozero_data
|
75
75
|
end
|
76
76
|
|
77
|
-
|
77
|
+
class << self
|
78
|
+
attr_accessor :classname
|
79
|
+
end
|
80
|
+
|
81
|
+
# This helps various parts of the framework determine if this is a datarow class without requiring it.
|
78
82
|
#===Examples
|
79
83
|
# print "This is a knj-object." if obj.respond_to?("is_knj?")
|
80
|
-
def
|
81
|
-
|
84
|
+
def knj?
|
85
|
+
true
|
82
86
|
end
|
83
87
|
|
84
|
-
#This tests if a certain string is a date-null-stamp.
|
88
|
+
# This tests if a certain string is a date-null-stamp.
|
85
89
|
#===Examples
|
86
90
|
# time_str = dbrow[:date]
|
87
91
|
# print "No valid date on the row." if Baza::Datarow.is_nullstamp?(time_str)
|
88
|
-
def self.
|
89
|
-
return true if !stamp
|
90
|
-
|
92
|
+
def self.nullstamp?(stamp)
|
93
|
+
return true if !stamp || stamp == "0000-00-00 00:00:00" || stamp == "0000-00-00"
|
94
|
+
false
|
91
95
|
end
|
92
96
|
|
93
|
-
#This is used to define datarows that this object can have a lot of.
|
97
|
+
# This is used to define datarows that this object can have a lot of.
|
94
98
|
#===Examples
|
95
|
-
#This will define the method "pictures" on 'Models::User' that will return all pictures for the users and take possible Objects-sql-arguments. It will also enabling joining pictures when doing Objects-sql-lookups.
|
99
|
+
# This will define the method "pictures" on 'Models::User' that will return all pictures for the users and take possible Objects-sql-arguments. It will also enabling joining pictures when doing Objects-sql-lookups.
|
96
100
|
# class Models::User < Baza::Datarow
|
97
101
|
# has_many [
|
98
102
|
# [:Picture, :user_id, :pictures],
|
99
|
-
# {:
|
103
|
+
# {class: :File, col: :user_id, method: :files}
|
100
104
|
# ]
|
101
105
|
# end
|
102
106
|
def self.has_many(arr)
|
@@ -104,75 +108,77 @@ class Baza::Model
|
|
104
108
|
if val.is_a?(Array)
|
105
109
|
classname, colname, methodname = *val
|
106
110
|
elsif val.is_a?(Hash)
|
107
|
-
classname
|
111
|
+
classname = nil
|
112
|
+
colname = nil
|
113
|
+
methodname = nil
|
108
114
|
|
109
115
|
val.each do |hkey, hval|
|
110
116
|
case hkey
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
117
|
+
when :class
|
118
|
+
classname = hval
|
119
|
+
when :col
|
120
|
+
colname = hval
|
121
|
+
when :method
|
122
|
+
methodname = hval
|
123
|
+
when :depends, :autodelete, :autozero, :where
|
124
|
+
# Ignore
|
125
|
+
else
|
126
|
+
raise "Invalid key for 'has_many': '#{hkey}'."
|
121
127
|
end
|
122
128
|
end
|
123
129
|
|
124
|
-
colname = "#{
|
130
|
+
colname = "#{name.to_s.split("::").last.to_s.downcase}_id" if colname.to_s.empty?
|
125
131
|
|
126
132
|
if val[:depends]
|
127
|
-
@depending_data = []
|
133
|
+
@depending_data = [] unless @depending_data
|
128
134
|
@depending_data << {
|
129
|
-
:
|
130
|
-
:
|
135
|
+
colname: colname,
|
136
|
+
classname: classname
|
131
137
|
}
|
132
138
|
end
|
133
139
|
|
134
140
|
if val[:autodelete]
|
135
|
-
@autodelete_data = []
|
141
|
+
@autodelete_data = [] unless @autodelete_data
|
136
142
|
@autodelete_data << {
|
137
|
-
:
|
138
|
-
:
|
143
|
+
colname: colname,
|
144
|
+
classname: classname
|
139
145
|
}
|
140
146
|
end
|
141
147
|
|
142
148
|
if val[:autozero]
|
143
|
-
@autozero_data = []
|
149
|
+
@autozero_data = [] unless @autozero_data
|
144
150
|
@autozero_data << {
|
145
|
-
:
|
146
|
-
:
|
151
|
+
colname: colname,
|
152
|
+
classname: classname
|
147
153
|
}
|
148
154
|
end
|
149
155
|
else
|
150
156
|
raise "Unknown argument: '#{val.class.name}'."
|
151
157
|
end
|
152
158
|
|
153
|
-
raise "No classname given."
|
154
|
-
methodname = "#{StringCases.camel_to_snake(classname)}s"
|
155
|
-
raise "No column was given for '#{
|
159
|
+
raise "No classname given." unless classname
|
160
|
+
methodname = "#{StringCases.camel_to_snake(classname)}s" unless methodname
|
161
|
+
raise "No column was given for '#{name}' regarding has-many-class: '#{classname}'." unless colname
|
156
162
|
|
157
|
-
if val.is_a?(Hash)
|
163
|
+
if val.is_a?(Hash) && val.key?(:where)
|
158
164
|
where_args = val[:where]
|
159
165
|
else
|
160
166
|
where_args = nil
|
161
167
|
end
|
162
168
|
|
163
|
-
|
169
|
+
define_many_methods(classname, methodname, colname, where_args)
|
164
170
|
|
165
|
-
|
171
|
+
joined_tables(
|
166
172
|
classname => {
|
167
|
-
:
|
168
|
-
colname.to_s => {:
|
173
|
+
where: {
|
174
|
+
colname.to_s => {type: :col, name: :id}
|
169
175
|
}
|
170
176
|
}
|
171
177
|
)
|
172
178
|
end
|
173
179
|
end
|
174
180
|
|
175
|
-
#This define is this object has one element of another datarow-class. It define various methods and joins based on that.
|
181
|
+
# This define is this object has one element of another datarow-class. It define various methods and joins based on that.
|
176
182
|
#===Examples
|
177
183
|
# class Models::User < Baza::Datarow
|
178
184
|
# has_one [
|
@@ -180,7 +186,7 @@ class Baza::Model
|
|
180
186
|
# :Group,
|
181
187
|
#
|
182
188
|
# #Defines the method 'type', which returns a 'Type'-object by the column 'type_id'.
|
183
|
-
# {:
|
189
|
+
# {class: :Type, col: :type_id, method: :type}
|
184
190
|
# ]
|
185
191
|
# end
|
186
192
|
def self.has_one(arr)
|
@@ -198,49 +204,51 @@ class Baza::Model
|
|
198
204
|
elsif val.is_a?(Array)
|
199
205
|
classname, colname, methodname = *val
|
200
206
|
elsif val.is_a?(Hash)
|
201
|
-
classname
|
207
|
+
classname = nil
|
208
|
+
colname = nil
|
209
|
+
methodname = nil
|
202
210
|
|
203
211
|
val.each do |hkey, hval|
|
204
212
|
case hkey
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
213
|
+
when :class
|
214
|
+
classname = hval
|
215
|
+
when :col
|
216
|
+
colname = hval
|
217
|
+
when :method
|
218
|
+
methodname = hval
|
219
|
+
when :required
|
220
|
+
# Ignore
|
221
|
+
else
|
222
|
+
raise "Invalid key for class '#{name}' functionality 'has_many': '#{hkey}'."
|
215
223
|
end
|
216
224
|
end
|
217
225
|
|
218
226
|
if val[:required]
|
219
|
-
colname = "#{classname.to_s.downcase}_id".to_sym
|
220
|
-
|
221
|
-
:
|
222
|
-
:
|
227
|
+
colname = "#{classname.to_s.downcase}_id".to_sym unless colname
|
228
|
+
required_data << {
|
229
|
+
col: colname,
|
230
|
+
class: classname
|
223
231
|
}
|
224
232
|
end
|
225
233
|
else
|
226
234
|
raise "Unknown argument-type: '#{arr.class.name}'."
|
227
235
|
end
|
228
236
|
|
229
|
-
methodname = StringCases.camel_to_snake(classname)
|
230
|
-
colname = "#{classname.to_s.downcase}_id".to_sym
|
231
|
-
|
237
|
+
methodname = StringCases.camel_to_snake(classname) unless methodname
|
238
|
+
colname = "#{classname.to_s.downcase}_id".to_sym unless colname
|
239
|
+
define_one_methods(classname, methodname, colname)
|
232
240
|
|
233
|
-
|
241
|
+
joined_tables(
|
234
242
|
classname => {
|
235
|
-
:
|
236
|
-
"id" => {:
|
243
|
+
where: {
|
244
|
+
"id" => {type: :col, name: colname}
|
237
245
|
}
|
238
246
|
}
|
239
247
|
)
|
240
248
|
end
|
241
249
|
end
|
242
250
|
|
243
|
-
#This method initializes joins, sets methods to update translations and makes the translations automatically be deleted when the object is deleted.
|
251
|
+
# This method initializes joins, sets methods to update translations and makes the translations automatically be deleted when the object is deleted.
|
244
252
|
#===Examples
|
245
253
|
# class Models::Article < Baza::Datarow
|
246
254
|
# #Defines methods such as: 'title', 'title=', 'content', 'content='. When used with Knjappserver these methods will change what they return and set based on the current language of the session.
|
@@ -252,7 +260,7 @@ class Baza::Model
|
|
252
260
|
#
|
253
261
|
# article.title = 'Title in english if the language is english'
|
254
262
|
def self.has_translation(arr)
|
255
|
-
@translations = []
|
263
|
+
@translations = [] unless @translations
|
256
264
|
|
257
265
|
arr.each do |val|
|
258
266
|
@translations << val
|
@@ -263,10 +271,10 @@ class Baza::Model
|
|
263
271
|
joined_tables(
|
264
272
|
table_name => {
|
265
273
|
where: {
|
266
|
-
"object_class" =>
|
274
|
+
"object_class" => name,
|
267
275
|
"object_id" => {type: :col, name: :id},
|
268
276
|
"key" => val.to_s,
|
269
|
-
"locale" => proc{|
|
277
|
+
"locale" => proc { |_d| _session[:locale] }
|
270
278
|
},
|
271
279
|
parent_table: :Translation,
|
272
280
|
datarow: Knj::Translations::Translation,
|
@@ -274,53 +282,53 @@ class Baza::Model
|
|
274
282
|
}
|
275
283
|
)
|
276
284
|
|
277
|
-
|
285
|
+
define_translation_methods(val: val, val_dc: val_dc)
|
278
286
|
end
|
279
287
|
end
|
280
288
|
|
281
|
-
#This returns all translations for this datarow-class.
|
282
|
-
|
283
|
-
|
289
|
+
# This returns all translations for this datarow-class.
|
290
|
+
class << self
|
291
|
+
attr_reader :translations
|
284
292
|
end
|
285
293
|
|
286
|
-
#Returns data about joined tables for this class.
|
294
|
+
# Returns data about joined tables for this class.
|
287
295
|
def self.joined_tables(hash)
|
288
|
-
@columns_joined_tables = {}
|
296
|
+
@columns_joined_tables = {} unless @columns_joined_tables
|
289
297
|
@columns_joined_tables.merge!(hash)
|
290
298
|
end
|
291
299
|
|
292
|
-
#Returns various data for the objects-sql-helper. This can be used to view various informations about the columns and more.
|
300
|
+
# Returns various data for the objects-sql-helper. This can be used to view various informations about the columns and more.
|
293
301
|
def self.columns_sqlhelper_args
|
294
|
-
raise "No SQLHelper arguments has been spawned yet."
|
295
|
-
|
302
|
+
raise "No SQLHelper arguments has been spawned yet." unless @columns_sqlhelper_args
|
303
|
+
@columns_sqlhelper_args
|
296
304
|
end
|
297
305
|
|
298
|
-
#Called by Baza::ModelHandler to initialize the model and load column-data on-the-fly.
|
306
|
+
# Called by Baza::ModelHandler to initialize the model and load column-data on-the-fly.
|
299
307
|
def self.load_columns(d)
|
300
308
|
@ob = d.ob
|
301
309
|
@db = d.db
|
302
310
|
|
303
|
-
@classname =
|
304
|
-
@table = @classname
|
305
|
-
@mutex = Monitor.new
|
311
|
+
@classname = name.split("::").last.to_sym unless @classname
|
312
|
+
@table = @classname unless @table
|
313
|
+
@mutex = Monitor.new unless @mutex
|
306
314
|
|
307
|
-
#Cache these to avoid method-lookups.
|
315
|
+
# Cache these to avoid method-lookups.
|
308
316
|
@sep_col = @db.sep_col
|
309
317
|
@sep_table = @db.sep_table
|
310
|
-
@table_str = "#{@sep_table}#{@db.
|
318
|
+
@table_str = "#{@sep_table}#{@db.escape_table(@table)}#{@sep_table}"
|
311
319
|
|
312
320
|
@mutex.synchronize do
|
313
|
-
inst_methods =
|
321
|
+
inst_methods = instance_methods(false)
|
314
322
|
|
315
323
|
sqlhelper_args = {
|
316
|
-
:
|
317
|
-
:
|
318
|
-
:
|
319
|
-
:
|
320
|
-
:
|
321
|
-
:
|
322
|
-
:
|
323
|
-
:
|
324
|
+
db: @db,
|
325
|
+
table: @table,
|
326
|
+
cols_bools: [],
|
327
|
+
cols_date: [],
|
328
|
+
cols_dbrows: [],
|
329
|
+
cols_num: [],
|
330
|
+
cols_str: [],
|
331
|
+
cols: {}
|
324
332
|
}
|
325
333
|
|
326
334
|
sqlhelper_args[:table] = @table
|
@@ -329,44 +337,42 @@ class Baza::Model
|
|
329
337
|
col_name = col_obj.name.to_s
|
330
338
|
col_name_sym = col_name.to_sym
|
331
339
|
col_type = col_obj.type
|
332
|
-
col_type = :int if col_type == :bigint
|
340
|
+
col_type = :int if col_type == :bigint || col_type == :tinyint || col_type == :mediumint || col_type == :smallint
|
333
341
|
sqlhelper_args[:cols][col_name] = true
|
334
342
|
|
335
|
-
|
343
|
+
define_bool_methods(inst_methods, col_name)
|
336
344
|
|
337
|
-
if col_type == :enum
|
345
|
+
if col_type == :enum && col_obj.maxlength == "'0','1'"
|
338
346
|
sqlhelper_args[:cols_bools] << col_name
|
339
|
-
elsif col_type == :int
|
347
|
+
elsif col_type == :int && col_name.slice(-3, 3) == "_id"
|
340
348
|
sqlhelper_args[:cols_dbrows] << col_name
|
341
|
-
elsif col_type == :int
|
349
|
+
elsif col_type == :int || col_type == :decimal
|
342
350
|
sqlhelper_args[:cols_num] << col_name
|
343
|
-
elsif col_type == :varchar
|
351
|
+
elsif col_type == :varchar || col_type == :text || col_type == :enum
|
344
352
|
sqlhelper_args[:cols_str] << col_name
|
345
|
-
elsif col_type == :date
|
353
|
+
elsif col_type == :date || col_type == :datetime
|
346
354
|
sqlhelper_args[:cols_date] << col_name
|
347
|
-
|
355
|
+
define_date_methods(inst_methods, col_name_sym)
|
348
356
|
end
|
349
357
|
|
350
|
-
if col_type == :int
|
351
|
-
|
358
|
+
if col_type == :int || col_type == :decimal
|
359
|
+
define_numeric_methods(inst_methods, col_name_sym)
|
352
360
|
end
|
353
361
|
|
354
|
-
if col_type == :int
|
355
|
-
|
362
|
+
if col_type == :int || col_type == :varchar
|
363
|
+
define_text_methods(inst_methods, col_name_sym)
|
356
364
|
end
|
357
365
|
|
358
|
-
if col_type == :time
|
359
|
-
self.define_time_methods(inst_methods, col_name_sym)
|
360
|
-
end
|
366
|
+
define_time_methods(inst_methods, col_name_sym) if col_type == :time
|
361
367
|
end
|
362
368
|
|
363
369
|
if @columns_joined_tables
|
364
370
|
@columns_joined_tables.each do |table_name, table_data|
|
365
|
-
table_data[:where].each do |
|
366
|
-
val[:table] = @table if val.is_a?(Hash)
|
371
|
+
table_data[:where].each do |_key, val|
|
372
|
+
val[:table] = @table if val.is_a?(Hash) && !val.key?(:table) && val[:type].to_sym == :col
|
367
373
|
end
|
368
374
|
|
369
|
-
table_data[:datarow] = @ob.args[:module].const_get(table_name.to_sym)
|
375
|
+
table_data[:datarow] = @ob.args[:module].const_get(table_name.to_sym) unless table_data.key?(:datarow)
|
370
376
|
end
|
371
377
|
|
372
378
|
sqlhelper_args[:joined_tables] = @columns_joined_tables
|
@@ -375,10 +381,10 @@ class Baza::Model
|
|
375
381
|
@columns_sqlhelper_args = sqlhelper_args
|
376
382
|
end
|
377
383
|
|
378
|
-
|
384
|
+
init_class(d) if self.respond_to?(:init_class)
|
379
385
|
end
|
380
386
|
|
381
|
-
#This method helps returning objects and supports various arguments. It should be called by Object#list.
|
387
|
+
# This method helps returning objects and supports various arguments. It should be called by Object#list.
|
382
388
|
#===Examples
|
383
389
|
# ob.list(:User, {"username_lower" => "john doe"}) do |user|
|
384
390
|
# print user.id
|
@@ -402,27 +408,27 @@ class Baza::Model
|
|
402
408
|
end
|
403
409
|
|
404
410
|
qargs = nil
|
405
|
-
ret =
|
411
|
+
ret = list_helper(d)
|
406
412
|
|
407
413
|
sql << " FROM #{@table_str}"
|
408
414
|
sql << ret[:sql_joins]
|
409
415
|
sql << " WHERE 1=1"
|
410
416
|
sql << ret[:sql_where]
|
411
417
|
|
412
|
-
args.
|
418
|
+
args.each_key do |key|
|
413
419
|
case key
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
+
when "return_sql"
|
421
|
+
# Ignore
|
422
|
+
when :cloned_ubuf
|
423
|
+
qargs = {cloned_ubuf: true}
|
424
|
+
else
|
425
|
+
raise "Invalid key: '#{key}' for '#{name}'. Valid keys are: '#{@columns_sqlhelper_args[:cols].keys.sort}'. Date-keys: '#{@columns_sqlhelper_args[:cols_date]}'."
|
420
426
|
end
|
421
427
|
end
|
422
428
|
|
423
|
-
#The count will bug if there is a group-by-statement.
|
429
|
+
# The count will bug if there is a group-by-statement.
|
424
430
|
grp_shown = false
|
425
|
-
if !count
|
431
|
+
if !count && !ret[:sql_groupby]
|
426
432
|
sql << " GROUP BY #{@table_str}.#{@sep_col}id#{@sep_col}"
|
427
433
|
grp_shown = true
|
428
434
|
end
|
@@ -463,36 +469,26 @@ class Baza::Model
|
|
463
469
|
return 0
|
464
470
|
end
|
465
471
|
|
466
|
-
|
472
|
+
@ob.list_bysql(classname, sql, qargs, &block)
|
467
473
|
end
|
468
474
|
|
469
|
-
#Helps call 'sqlhelper' on Baza::ModelHandler to generate SQL-strings.
|
475
|
+
# Helps call 'sqlhelper' on Baza::ModelHandler to generate SQL-strings.
|
470
476
|
def self.list_helper(d)
|
471
|
-
|
477
|
+
load_columns(d) unless @columns_sqlhelper_args
|
472
478
|
@columns_sqlhelper_args[:table] = @table
|
473
|
-
|
474
|
-
end
|
475
|
-
|
476
|
-
#Returns the classname of the object without any subclasses.
|
477
|
-
def self.classname
|
478
|
-
return @classname
|
479
|
+
@ob.sqlhelper(d.args, @columns_sqlhelper_args)
|
479
480
|
end
|
480
481
|
|
481
|
-
#
|
482
|
-
def self.classname=(newclassname)
|
483
|
-
@classname = newclassname
|
484
|
-
end
|
485
|
-
|
486
|
-
#Returns the table-name that should be used for this datarow.
|
482
|
+
# Returns the table-name that should be used for this datarow.
|
487
483
|
#===Examples
|
488
484
|
# db.query("SELECT * FROM `#{Models::User.table}` WHERE username = 'John Doe'") do |data|
|
489
485
|
# print data[:id]
|
490
486
|
# end
|
491
|
-
|
492
|
-
|
487
|
+
class << self
|
488
|
+
attr_reader :table
|
493
489
|
end
|
494
490
|
|
495
|
-
#This can be used to manually set the table-name. Useful when meta-programming classes that extends the datarow-class.
|
491
|
+
# This can be used to manually set the table-name. Useful when meta-programming classes that extends the datarow-class.
|
496
492
|
#===Examples
|
497
493
|
# Models::User.table = "prefix_User"
|
498
494
|
def self.table=(newtable)
|
@@ -500,21 +496,21 @@ class Baza::Model
|
|
500
496
|
@columns_sqlhelper_args[:table] = @table if @columns_sqlhelper_args.is_a?(Hash)
|
501
497
|
end
|
502
498
|
|
503
|
-
#Returns the class-name but without having to call the class-table-method. To make code look shorter.
|
499
|
+
# Returns the class-name but without having to call the class-table-method. To make code look shorter.
|
504
500
|
#===Examples
|
505
|
-
# user = ob.get_by(:User, {:
|
501
|
+
# user = ob.get_by(:User, {username: 'John Doe'})
|
506
502
|
# db.query("SELECT * FROM `#{user.table}` WHERE username = 'John Doe'") do |data|
|
507
503
|
# print data[:id]
|
508
504
|
# end
|
509
505
|
def table
|
510
|
-
|
506
|
+
self.class.table
|
511
507
|
end
|
512
508
|
|
513
|
-
#Initializes the object. This should be called from 'Baza::ModelHandler' and not manually.
|
509
|
+
# Initializes the object. This should be called from 'Baza::ModelHandler' and not manually.
|
514
510
|
#===Examples
|
515
511
|
# user = ob.get(:User, 3)
|
516
512
|
def initialize(data, args = nil)
|
517
|
-
if data.is_a?(Hash)
|
513
|
+
if data.is_a?(Hash) && data.key?(:id)
|
518
514
|
@data = data
|
519
515
|
@id = @data[:id].to_i
|
520
516
|
elsif data
|
@@ -522,13 +518,13 @@ class Baza::Model
|
|
522
518
|
|
523
519
|
classname = self.class.classname.to_sym
|
524
520
|
if self.class.ob.ids_cache_should.key?(classname)
|
525
|
-
#ID caching is enabled for this model - dont reload until first use.
|
526
|
-
raise Errno::ENOENT, "ID was not found in cache: '#{id}'."
|
521
|
+
# ID caching is enabled for this model - dont reload until first use.
|
522
|
+
raise Errno::ENOENT, "ID was not found in cache: '#{id}'." unless self.class.ob.ids_cache[classname].key?(@id)
|
527
523
|
@should_reload = true
|
528
524
|
else
|
529
|
-
#ID caching is not enabled - reload now to check if row exists. Else set 'should_reload'-variable if 'skip_reload' is set.
|
530
|
-
if !args
|
531
|
-
|
525
|
+
# ID caching is not enabled - reload now to check if row exists. Else set 'should_reload'-variable if 'skip_reload' is set.
|
526
|
+
if !args || !args[:skip_reload]
|
527
|
+
reload
|
532
528
|
else
|
533
529
|
@should_reload = true
|
534
530
|
end
|
@@ -537,24 +533,24 @@ class Baza::Model
|
|
537
533
|
raise ArgumentError, "Could not figure out the data from '#{data.class.name}'."
|
538
534
|
end
|
539
535
|
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
536
|
+
return unless @id.to_i <= 0
|
537
|
+
|
538
|
+
raise "Invalid ID: '#{@id}' from '#{@data}'." if @data
|
539
|
+
raise "Invalid ID: '#{@id}'."
|
544
540
|
end
|
545
541
|
|
546
|
-
#Reloads the data from the database.
|
542
|
+
# Reloads the data from the database.
|
547
543
|
#===Examples
|
548
544
|
# old_username = user[:username]
|
549
545
|
# user.reload
|
550
546
|
# print "The username changed in the database!" if user[:username] != old_username
|
551
547
|
def reload
|
552
|
-
@data = self.class.db.single(self.class.table,
|
553
|
-
raise Errno::ENOENT, "Could not find any data for the object with ID: '#{@id}' in the table '#{self.class.table}'."
|
548
|
+
@data = self.class.db.single(self.class.table, id: @id)
|
549
|
+
raise Errno::ENOENT, "Could not find any data for the object with ID: '#{@id}' in the table '#{self.class.table}'." unless @data
|
554
550
|
@should_reload = false
|
555
551
|
end
|
556
552
|
|
557
|
-
#Tells the object that it should reloads its data because it has changed. It wont reload before it is required though, which may save you a couple of SQL-calls.
|
553
|
+
# Tells the object that it should reloads its data because it has changed. It wont reload before it is required though, which may save you a couple of SQL-calls.
|
558
554
|
#===Examples
|
559
555
|
# obj = _ob.get(:User, 5)
|
560
556
|
# obj.should_reload
|
@@ -563,99 +559,99 @@ class Baza::Model
|
|
563
559
|
@data = nil
|
564
560
|
end
|
565
561
|
|
566
|
-
#Returns the data-hash that contains all the data from the database.
|
562
|
+
# Returns the data-hash that contains all the data from the database.
|
567
563
|
def data
|
568
|
-
|
569
|
-
|
564
|
+
reload if @should_reload
|
565
|
+
@data
|
570
566
|
end
|
571
567
|
|
572
|
-
#Writes/updates new data for the object.
|
568
|
+
# Writes/updates new data for the object.
|
573
569
|
#===Examples
|
574
|
-
# user.update(:
|
570
|
+
# user.update(username: 'New username', date_changed: Time.now)
|
575
571
|
def update(newdata)
|
576
|
-
self.class.db.update(self.class.table, newdata,
|
577
|
-
|
572
|
+
self.class.db.update(self.class.table, newdata, id: @id)
|
573
|
+
should_reload
|
578
574
|
self.class.ob.call("object" => self, "signal" => "update")
|
579
575
|
end
|
580
576
|
|
581
|
-
#Forcefully destroys the object. This is done after deleting it and should not be called manually.
|
577
|
+
# Forcefully destroys the object. This is done after deleting it and should not be called manually.
|
582
578
|
def destroy
|
583
579
|
@id = nil
|
584
580
|
@data = nil
|
585
581
|
@should_reload = nil
|
586
582
|
end
|
587
583
|
|
588
|
-
#Returns true if that key exists on the object.
|
584
|
+
# Returns true if that key exists on the object.
|
589
585
|
#===Examples
|
590
586
|
# print "Looks like the user has a name." if user.key?(:name)
|
591
587
|
def key?(key)
|
592
|
-
|
593
|
-
|
588
|
+
reload if @should_reload
|
589
|
+
@data.key?(key.to_sym)
|
594
590
|
end
|
595
|
-
|
591
|
+
alias_method :has_key?, :key?
|
596
592
|
|
597
|
-
#Returns true if the object has been deleted.
|
593
|
+
# Returns true if the object has been deleted.
|
598
594
|
#===Examples
|
599
595
|
# print "That user is deleted." if user.deleted?
|
600
596
|
def deleted?
|
601
|
-
return true if !@data
|
602
|
-
|
597
|
+
return true if !@data && !@id
|
598
|
+
false
|
603
599
|
end
|
604
600
|
|
605
|
-
#Returns true if the given object no longer exists in the database. Also destroys the data on the object and sets it to deleted-status, if it no longer exists.
|
601
|
+
# Returns true if the given object no longer exists in the database. Also destroys the data on the object and sets it to deleted-status, if it no longer exists.
|
606
602
|
#===Examples
|
607
603
|
# print "That user is deleted." if user.deleted_from_db?
|
608
604
|
def deleted_from_db?
|
609
|
-
#Try to avoid db-query if object is already deleted.
|
605
|
+
# Try to avoid db-query if object is already deleted.
|
610
606
|
return true if self.deleted?
|
611
607
|
|
612
|
-
#Try to reload data. Destroy object and return true if the row is gone from the database.
|
608
|
+
# Try to reload data. Destroy object and return true if the row is gone from the database.
|
613
609
|
begin
|
614
|
-
|
610
|
+
reload
|
615
611
|
return false
|
616
612
|
rescue Errno::ENOENT
|
617
|
-
|
613
|
+
destroy
|
618
614
|
return true
|
619
615
|
end
|
620
616
|
end
|
621
617
|
|
622
|
-
#Returns a specific data from the object by key.
|
618
|
+
# Returns a specific data from the object by key.
|
623
619
|
# print "Username: #{user[:username]}\n"
|
624
620
|
# print "ID: #{user[:id]}\n"
|
625
621
|
# print "ID again: #{user.id}\n"
|
626
622
|
def [](key)
|
627
|
-
raise "Key was not a symbol: '#{key.class.name}'."
|
628
|
-
return @id if !@data
|
629
|
-
|
630
|
-
raise "No data was loaded on the object? Maybe you are trying to call a deleted object? (#{self.class.classname}(#{@id}), #{@should_reload})"
|
623
|
+
raise "Key was not a symbol: '#{key.class.name}'." unless key.is_a?(Symbol)
|
624
|
+
return @id if !@data && key == :id && @id
|
625
|
+
reload if @should_reload
|
626
|
+
raise "No data was loaded on the object? Maybe you are trying to call a deleted object? (#{self.class.classname}(#{@id}), #{@should_reload})" unless @data
|
631
627
|
return @data[key] if @data.key?(key)
|
632
628
|
raise "No such key: '#{key}' on '#{self.class.name}' (#{@data.keys.join(", ")}) (#{@should_reload})."
|
633
629
|
end
|
634
630
|
|
635
|
-
#Writes/updates a keys value on the object.
|
631
|
+
# Writes/updates a keys value on the object.
|
636
632
|
# user = ob.get_by(:User, {"username" => "John Doe"})
|
637
633
|
# user[:username] = 'New username'
|
638
634
|
def []=(key, value)
|
639
|
-
|
640
|
-
|
635
|
+
update(key.to_sym => value)
|
636
|
+
should_reload
|
641
637
|
end
|
642
638
|
|
643
|
-
#Returns the objects ID.
|
639
|
+
# Returns the objects ID.
|
644
640
|
def id
|
645
|
-
raise Errno::ENOENT, "This object has been deleted." if
|
646
|
-
raise "No ID on object."
|
647
|
-
|
641
|
+
raise Errno::ENOENT, "This object has been deleted." if deleted?
|
642
|
+
raise "No ID on object." unless @id
|
643
|
+
@id
|
648
644
|
end
|
649
645
|
|
650
|
-
#This enable Wref to not return the wrong object.
|
646
|
+
# This enable Wref to not return the wrong object.
|
651
647
|
def __object_unique_id__
|
652
648
|
return 0 if self.deleted?
|
653
|
-
|
649
|
+
id
|
654
650
|
end
|
655
651
|
|
656
|
-
#Tries to figure out, and returns, the possible name or title for the object.
|
652
|
+
# Tries to figure out, and returns, the possible name or title for the object.
|
657
653
|
def name
|
658
|
-
|
654
|
+
reload if @should_reload
|
659
655
|
|
660
656
|
if @data.key?(:title)
|
661
657
|
return @data[:title]
|
@@ -665,74 +661,70 @@ class Baza::Model
|
|
665
661
|
|
666
662
|
obj_methods = self.class.instance_methods(false)
|
667
663
|
[:name, :title].each do |method_name|
|
668
|
-
return
|
664
|
+
return method(method_name).call if obj_methods.index(method_name)
|
669
665
|
end
|
670
666
|
|
671
667
|
raise "Couldnt figure out the title/name of the object on class #{self.class.name}."
|
672
668
|
end
|
673
669
|
|
674
|
-
#Calls the name-method and returns a HTML-escaped value. Also "[no name]" if the name is empty.
|
670
|
+
# Calls the name-method and returns a HTML-escaped value. Also "[no name]" if the name is empty.
|
675
671
|
def name_html
|
676
672
|
name_str = name.to_s
|
677
673
|
name_str = "[no name]" if name_str.empty?
|
678
|
-
|
674
|
+
name_str
|
679
675
|
end
|
680
676
|
|
681
|
-
|
677
|
+
alias_method :title, :name
|
682
678
|
|
683
|
-
#Loops through the data on the object.
|
679
|
+
# Loops through the data on the object.
|
684
680
|
#===Examples
|
685
681
|
# user = ob.get(:User, 1)
|
686
682
|
# user.each do |key, val|
|
687
683
|
# print "#{key}: #{val}\n" #=> username: John Doe
|
688
684
|
# end
|
689
685
|
def each(*args, &block)
|
690
|
-
|
691
|
-
|
686
|
+
reload if @should_reload
|
687
|
+
@data.each(*args, &block)
|
692
688
|
end
|
693
689
|
|
694
|
-
#Hash-compatible.
|
690
|
+
# Hash-compatible.
|
695
691
|
def to_hash
|
696
|
-
|
697
|
-
|
692
|
+
reload if @should_reload
|
693
|
+
@data.clone
|
698
694
|
end
|
699
695
|
|
700
|
-
#Returns a default-URL to show the object.
|
696
|
+
# Returns a default-URL to show the object.
|
701
697
|
def url
|
702
698
|
cname = self.class.classname.to_s.downcase
|
703
|
-
|
699
|
+
"?show=#{cname}_show&#{cname}_id=#{id}"
|
704
700
|
end
|
705
701
|
|
706
|
-
#Returns the URL for editting the object.
|
702
|
+
# Returns the URL for editting the object.
|
707
703
|
def url_edit
|
708
704
|
cname = self.class.classname.to_s.downcase
|
709
|
-
|
705
|
+
"?show=#{cname}_edit&#{cname}_id=#{id}"
|
710
706
|
end
|
711
707
|
|
712
|
-
#Returns the HTML for making a link to the object.
|
708
|
+
# Returns the HTML for making a link to the object.
|
713
709
|
def html(args = nil)
|
714
|
-
if args
|
715
|
-
url =
|
710
|
+
if args && args[:edit]
|
711
|
+
url = url_edit
|
716
712
|
else
|
717
713
|
url = self.url
|
718
714
|
end
|
719
715
|
|
720
|
-
|
716
|
+
"<a href=\"#{Knj::Web.ahref_parse(url)}\">#{name_html}</a>"
|
721
717
|
end
|
722
718
|
|
723
|
-
|
719
|
+
private
|
724
720
|
|
725
|
-
#Various methods to define methods based on the columns for the datarow.
|
721
|
+
# Various methods to define methods based on the columns for the datarow.
|
726
722
|
def self.define_translation_methods(args)
|
727
723
|
define_method("#{args[:val_dc]}=") do |newtransval|
|
728
724
|
begin
|
729
|
-
_hb.trans_set(self,
|
730
|
-
args[:val] => newtransval
|
731
|
-
})
|
725
|
+
_hb.trans_set(self, args[:val] => newtransval)
|
732
726
|
rescue NameError
|
733
|
-
_kas.trans_set(self,
|
734
|
-
args[:val] => newtransval
|
735
|
-
})
|
727
|
+
_kas.trans_set(self, args[:val] => newtransval)
|
736
728
|
end
|
737
729
|
end
|
738
730
|
|
@@ -751,135 +743,133 @@ class Baza::Model
|
|
751
743
|
str = _kas.trans(self, args[:val])
|
752
744
|
end
|
753
745
|
|
754
|
-
if str.to_s.strip.empty?
|
755
|
-
return "[no translation for #{args[:val]}]"
|
756
|
-
end
|
746
|
+
return "[no translation for #{args[:val]}]" if str.to_s.strip.empty?
|
757
747
|
|
758
748
|
return str
|
759
749
|
end
|
760
750
|
end
|
761
751
|
|
762
|
-
#Defines the boolean-methods based on enum-columns.
|
752
|
+
# Defines the boolean-methods based on enum-columns.
|
763
753
|
def self.define_bool_methods(inst_methods, col_name)
|
764
|
-
#Spawns a method on the class which returns true if the data is 1.
|
765
|
-
if
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
754
|
+
# Spawns a method on the class which returns true if the data is 1.
|
755
|
+
return if inst_methods.include?("#{col_name}?".to_sym)
|
756
|
+
|
757
|
+
define_method("#{col_name}?") do
|
758
|
+
return true if self[col_name.to_sym].to_s == "1"
|
759
|
+
return false
|
770
760
|
end
|
771
761
|
end
|
772
762
|
|
773
|
-
#Defines date- and time-columns based on datetime- and date-columns.
|
763
|
+
# Defines date- and time-columns based on datetime- and date-columns.
|
774
764
|
def self.define_date_methods(inst_methods, col_name)
|
775
|
-
if
|
776
|
-
define_method("#{col_name}_str") do |*method_args|
|
777
|
-
if Datet.is_nullstamp?(self[col_name])
|
778
|
-
return self.class.ob.events.call(:no_date, self.class.name)
|
779
|
-
end
|
765
|
+
return if inst_methods.include?("#{col_name}_str".to_sym)
|
780
766
|
|
781
|
-
|
767
|
+
define_method("#{col_name}_str") do |*method_args|
|
768
|
+
if Datet.is_nullstamp?(self[col_name])
|
769
|
+
return self.class.ob.events.call(:no_date, self.class.name)
|
782
770
|
end
|
771
|
+
|
772
|
+
return Datet.in(self[col_name]).out(*method_args)
|
783
773
|
end
|
784
774
|
|
785
|
-
if
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
775
|
+
return if inst_methods.include?(col_name)
|
776
|
+
|
777
|
+
define_method(col_name) do |*_method_args|
|
778
|
+
return false if Datet.is_nullstamp?(self[col_name])
|
779
|
+
return Datet.in(self[col_name])
|
790
780
|
end
|
791
781
|
end
|
792
782
|
|
793
|
-
#Define various methods based on integer-columns.
|
783
|
+
# Define various methods based on integer-columns.
|
794
784
|
def self.define_numeric_methods(inst_methods, col_name)
|
795
|
-
if
|
796
|
-
|
797
|
-
|
798
|
-
|
785
|
+
return if inst_methods.include?("#{col_name}_format".to_sym)
|
786
|
+
|
787
|
+
define_method("#{col_name}_format") do |*method_args|
|
788
|
+
return Knj::Locales.number_out(self[col_name], *method_args)
|
799
789
|
end
|
800
790
|
end
|
801
791
|
|
802
|
-
#Define methods to look up objects directly.
|
792
|
+
# Define methods to look up objects directly.
|
803
793
|
#===Examples
|
804
794
|
# user = Models::User.by_username('John Doe')
|
805
795
|
# print user.id
|
806
796
|
def self.define_text_methods(inst_methods, col_name)
|
807
|
-
if
|
808
|
-
|
809
|
-
|
810
|
-
|
797
|
+
return if inst_methods.include?("by_#{col_name}".to_sym) && RUBY_VERSION.to_s.slice(0, 3) != "1.8"
|
798
|
+
|
799
|
+
define_singleton_method("by_#{col_name}") do |arg|
|
800
|
+
return self.class.ob.get_by(self.class.table, col_name.to_s => arg)
|
811
801
|
end
|
812
802
|
end
|
813
803
|
|
814
|
-
#Defines dbtime-methods based on time-columns.
|
804
|
+
# Defines dbtime-methods based on time-columns.
|
815
805
|
def self.define_time_methods(inst_methods, col_name)
|
816
|
-
if
|
817
|
-
|
818
|
-
|
819
|
-
|
806
|
+
return if inst_methods.include?("#{col_name}_dbt".to_sym)
|
807
|
+
|
808
|
+
define_method("#{col_name}_dbt") do
|
809
|
+
return Baza::Db::Dbtime.new(self[col_name.to_sym])
|
820
810
|
end
|
821
811
|
end
|
822
812
|
|
823
|
-
#Memory friendly helper method that defines methods for 'has_many'.
|
813
|
+
# Memory friendly helper method that defines methods for 'has_many'.
|
824
814
|
def self.define_many_methods(classname, methodname, colname, where_args)
|
825
815
|
define_method(methodname) do |*args, &block|
|
826
|
-
if args
|
816
|
+
if args && args[0]
|
827
817
|
list_args = args[0]
|
828
818
|
else
|
829
819
|
list_args = {}
|
830
820
|
end
|
831
821
|
|
832
822
|
list_args.merge!(where_args) if where_args
|
833
|
-
list_args[colname.to_s] =
|
823
|
+
list_args[colname.to_s] = id
|
834
824
|
|
835
|
-
|
825
|
+
self.class.ob.list(classname, list_args, &block)
|
836
826
|
end
|
837
827
|
|
838
828
|
define_method("#{methodname}_count".to_sym) do |*args|
|
839
|
-
list_args = args[0] if args
|
840
|
-
list_args = {}
|
841
|
-
list_args[colname.to_s] =
|
829
|
+
list_args = args[0] if args && args[0]
|
830
|
+
list_args = {} unless list_args
|
831
|
+
list_args[colname.to_s] = id
|
842
832
|
list_args["count"] = true
|
843
833
|
|
844
|
-
|
834
|
+
self.class.ob.list(classname, list_args)
|
845
835
|
end
|
846
836
|
|
847
837
|
define_method("#{methodname}_last".to_sym) do |args|
|
848
|
-
args = {}
|
849
|
-
|
838
|
+
args = {} unless args
|
839
|
+
self.class.ob.list(classname, {"orderby" => [["id", "desc"]], "limit" => 1}.merge(args))
|
850
840
|
end
|
851
841
|
end
|
852
842
|
|
853
|
-
#Memory friendly helper method that defines methods for 'has_one'.
|
843
|
+
# Memory friendly helper method that defines methods for 'has_one'.
|
854
844
|
def self.define_one_methods(classname, methodname, colname)
|
855
845
|
define_method(methodname) do
|
856
|
-
|
846
|
+
self.class.ob.get_try(self, colname, classname)
|
857
847
|
end
|
858
848
|
|
859
849
|
methodname_html = "#{methodname}_html".to_sym
|
860
850
|
define_method(methodname_html) do |*args|
|
861
|
-
obj =
|
862
|
-
return self.class.ob.events.call(:no_html, classname)
|
851
|
+
obj = __send__(methodname)
|
852
|
+
return self.class.ob.events.call(:no_html, classname) unless obj
|
863
853
|
|
864
|
-
raise "Class '#{classname}' does not have a 'html'-method."
|
865
|
-
|
854
|
+
raise "Class '#{classname}' does not have a 'html'-method." unless obj.respond_to?(:html)
|
855
|
+
obj.html(*args)
|
866
856
|
end
|
867
857
|
|
868
858
|
methodname_name = "#{methodname}_name".to_sym
|
869
859
|
define_method(methodname_name) do |*args|
|
870
|
-
obj =
|
871
|
-
return self.class.ob.events.call(:no_name, classname)
|
872
|
-
|
860
|
+
obj = __send__(methodname)
|
861
|
+
return self.class.ob.events.call(:no_name, classname) unless obj
|
862
|
+
obj.name(*args)
|
873
863
|
end
|
874
864
|
end
|
875
865
|
|
876
|
-
#Returns a hash reflection the current ActiveRecord model and its current values (not like .attributes which reflects the old values).
|
866
|
+
# Returns a hash reflection the current ActiveRecord model and its current values (not like .attributes which reflects the old values).
|
877
867
|
def self.activerecord_to_hash(model)
|
878
868
|
attrs = {}
|
879
869
|
model.attribute_names.each do |name|
|
880
870
|
attrs[name] = model.__send__(name)
|
881
871
|
end
|
882
872
|
|
883
|
-
|
873
|
+
attrs
|
884
874
|
end
|
885
|
-
end
|
875
|
+
end
|