Class: Knj::Datarow
Overview
This class helps create models in a framework with Knj::Db and Knj::Objects.
Examples
db = Knj::Db.new(:type => “sqlite3”, :path => “somepath.sqlite3”) ob = Knj::Objects.new(:db => db, :datarow => true, :path => “path_of_model_class_files”) user = ob.get(:User, 1) #=> <Models::User> that extends <Knj::Datarow>
Direct Known Subclasses
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the Knj::Db which handels this model.
-
#ob ⇒ Object
readonly
Returns the Knj::Objects which handels this model.
Class Method Summary collapse
-
.autodelete_data ⇒ Object
This is used by ‘Knj::Objects’ to find out which other objects should be deleted when an object of this class is deleted automatically.
-
.classname ⇒ Object
Returns the classname of the object without any subclasses.
-
.classname=(newclassname) ⇒ Object
Sets the classname to something specific in order to hack the behaviour.
-
.columns_sqlhelper_args ⇒ Object
Returns various data for the objects-sql-helper.
-
.depending_data ⇒ Object
This is used by ‘Knj::Objects’ to find out what other objects this class depends on.
-
.has_many(arr) ⇒ Object
This is used to define datarows that this object can have a lot of.
-
.has_one(arr) ⇒ Object
This define is this object has one element of another datarow-class.
-
.has_translation(arr) ⇒ Object
This method initializes joins, sets methods to update translations and makes the translations automatically be deleted when the object is deleted.
-
.initialized? ⇒ Boolean
Returns true if this class has been initialized.
-
.is_nullstamp?(stamp) ⇒ Boolean
This tests if a certain string is a date-null-stamp.
-
.joined_tables(hash) ⇒ Object
Returns data about joined tables for this class.
-
.list(d, &block) ⇒ Object
This method helps returning objects and supports various arguments.
-
.list_helper(d) ⇒ Object
Helps call ‘sqlhelper’ on Knj::Objects to generate SQL-strings.
-
.load_columns(d) ⇒ Object
Called by Knj::Objects to initialize the model and load column-data on-the-fly.
-
.ob ⇒ Object
Get the ‘Knj::Objects’-object that handels this class.
-
.required_data ⇒ Object
This is used by ‘Knj::Objects’ to find out what data is required for this class.
-
.table ⇒ Object
Returns the table-name that should be used for this datarow.
-
.table=(newtable) ⇒ Object
This can be used to manually set the table-name.
-
.translations ⇒ Object
This returns all translations for this datarow-class.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns a specific data from the object by key.
-
#[]=(key, value) ⇒ Object
Writes/updates a keys value on the object.
-
#__object_unique_id__ ⇒ Object
This enable Wref to not return the wrong object.
-
#data ⇒ Object
Returns the data-hash that contains all the data from the database.
-
#deleted? ⇒ Boolean
Returns true if the object has been deleted.
-
#destroy ⇒ Object
Forcefully destroys the object.
-
#each(*args, &block) ⇒ Object
Loops through the data on the object.
-
#id ⇒ Object
Returns the objects ID.
-
#initialize(data, args = nil) ⇒ Datarow
constructor
Initializes the object.
-
#is_knj? ⇒ Boolean
This helps various parts of the framework determine if this is a datarow class without requiring it.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Returns true if that key exists on the object.
-
#name ⇒ Object
(also: #title)
Tries to figure out, and returns, the possible name or title for the object.
-
#name_html ⇒ Object
Calls the name-method and returns a HTML-escaped value.
-
#reload ⇒ Object
Reloads the data from the database.
-
#should_reload ⇒ Object
Tells the object that it should reloads its data because it has changed.
-
#table ⇒ Object
Returns the class-name but without having to call the class-table-method.
-
#update(newdata) ⇒ Object
Writes/updates new data for the object.
Constructor Details
#initialize(data, args = nil) ⇒ Datarow
Initializes the object. This should be called from ‘Knj::Objects’ and not manually.
Examples
user = ob.get(:User, 3)
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/knj/datarow.rb', line 536 def initialize(data, args = nil) @ob = self.class.ob raise "No ob given." if !@ob @db = ob.db if data.is_a?(Hash) and data.key?(:id) @data = data @id = @data[:id].to_i elsif data @id = data.to_i classname = self.class.classname.to_sym if @ob.ids_cache_should.key?(classname) #ID caching is enabled for this model - dont reload until first use. raise Knj::Errors::NotFound, "ID was not found in cache: '#{id}'." if !@ob.ids_cache[classname].key?(@id) @should_reload = true else #ID caching is not enabled - reload now to check if row exists. Else set 'should_reload'-variable if 'skip_reload' is set. if !args or !args[:skip_reload] self.reload else @should_reload = true end end else raise Knj::Errors::InvalidData, "Could not figure out the data from '#{data.class.name}'." end if @id.to_i <= 0 raise "Invalid ID: '#{@id}' from '#{@data}'."if @data raise "Invalid ID: '#{@id}'." end end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the Knj::Db which handels this model.
17 18 19 |
# File 'lib/knj/datarow.rb', line 17 def db @db end |
#ob ⇒ Object (readonly)
Returns the Knj::Objects which handels this model.
14 15 16 |
# File 'lib/knj/datarow.rb', line 14 def ob @ob end |
Class Method Details
.autodelete_data ⇒ Object
This is used by ‘Knj::Objects’ 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.
Examples
This will trigger Knj::Objects to automatically delete all the users pictures, when deleting the current user. class Models::User < Knj::Datarow
has_many [
{:class => :Picture, :col => :user_id, :method => :pictures, :autodelete => true}
]
end
59 60 61 62 |
# File 'lib/knj/datarow.rb', line 59 def self.autodelete_data @autodelete_data = [] if !@autodelete_data return @autodelete_data end |
.classname ⇒ Object
Returns the classname of the object without any subclasses.
524 525 526 |
# File 'lib/knj/datarow.rb', line 524 def self.classname return @classname end |
.classname=(newclassname) ⇒ Object
Sets the classname to something specific in order to hack the behaviour.
529 530 531 |
# File 'lib/knj/datarow.rb', line 529 def self.classname=(newclassname) @classname = newclassname end |
.columns_sqlhelper_args ⇒ Object
Returns various data for the objects-sql-helper. This can be used to view various informations about the columns and more.
345 346 347 348 |
# File 'lib/knj/datarow.rb', line 345 def self.columns_sqlhelper_args raise "No SQLHelper arguments has been spawned yet." if !@columns_sqlhelper_args return @columns_sqlhelper_args end |
.depending_data ⇒ Object
This is used by ‘Knj::Objects’ to find out what other objects this class depends on. Returns the array that tells about depending data.
Examples
This will tell Knj::Objects that files depends on users. It can prevent the user from being deleted, if any files depend on it. class Models::User < Knj::Datarow
has_many [
{:class => :File, :col => :user_id, :method => :files, :depends => true}
]
end
40 41 42 43 |
# File 'lib/knj/datarow.rb', line 40 def self.depending_data @depending_data = [] if !@depending_data return @depending_data end |
.has_many(arr) ⇒ Object
This is used to define datarows that this object can have a lot of.
Examples
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. class Models::User < Knj::Datarow
has_many [
[:Picture, :user_id, :pictures],
{:class => :File, :col => :user_id, :method => :files}
]
end
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/knj/datarow.rb', line 94 def self.has_many(arr) arr.each do |val| if val.is_a?(Array) classname, colname, methodname = *val elsif val.is_a?(Hash) classname, colname, methodname = nil, nil, nil val.each do |hkey, hval| case hkey when :class classname = hval when :col colname = hval when :method methodname = hval when :depends, :autodelete, :where #ignore else raise "Invalid key for 'has_many': '#{hkey}'." end end colname = "#{self.name.to_s.split("::").last.to_s.downcase}_id".to_sym if colname.to_s.empty? if val[:depends] self.depending_data << { :colname => colname, :classname => classname } end if val[:autodelete] self.autodelete_data << { :colname => colname, :classname => classname } end else raise "Unknown argument: '#{val.class.name}'." end raise "No classname given." if !classname methodname = "#{classname.to_s.downcase}s".to_sym if !methodname raise "No column was given for '#{self.name}' regarding has-many-class: '#{classname}'." if !colname if val.is_a?(Hash) and val.key?(:where) where_args = val[:where] else where_args = nil end define_method(methodname) do |*args, &block| if args and args[0] list_args = args[0] else list_args = {} end list_args.merge!(where_args) if where_args list_args[colname.to_s] = self.id return @ob.list(classname, list_args, &block) end define_method("#{methodname}_count".to_sym) do |*args| list_args = args[0] if args and args[0] list_args = {} if !list_args list_args[colname.to_s] = self.id list_args["count"] = true return @ob.list(classname, list_args) end define_method("#{methodname}_last".to_sym) do |args| args = {} if !args return @ob.list(classname, {"orderby" => [["id", "desc"]], "limit" => 1}.merge(args)) end self.joined_tables( classname => { :where => { colname.to_s => {:type => :col, :name => :id} } } ) end end |
.has_one(arr) ⇒ Object
This define is this object has one element of another datarow-class. It define various methods and joins based on that.
Examples
class Models::User < Knj::Datarow
has_one [
#Defines the method 'group', which returns a 'Group'-object by the column 'group_id'.
:Group,
#Defines the method 'type', which returns a 'Type'-object by the column 'type_id'.
{:class => :Type, :col => :type_id, :method => :type}
]
end
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/knj/datarow.rb', line 193 def self.has_one(arr) arr.each do |val| methodname = nil colname = nil classname = nil if val.is_a?(Symbol) classname = val methodname = val.to_s.downcase.to_sym colname = "#{val.to_s.downcase}_id".to_sym elsif val.is_a?(Array) classname, colname, methodname = *val elsif val.is_a?(Hash) classname, colname, methodname = nil, nil, nil val.each do |hkey, hval| case hkey when :class classname = hval when :col colname = hval when :method methodname = hval when :required #ignore else raise "Invalid key for class '#{self.name}' functionality 'has_many': '#{hkey}'." end end if val[:required] colname = "#{classname.to_s.downcase}_id".to_sym if !colname self.required_data << { :col => colname, :class => classname } end else raise "Unknown argument-type: '#{arr.class.name}'." end methodname = classname.to_s.downcase if !methodname colname = "#{classname.to_s.downcase}_id".to_sym if !colname define_method(methodname) do return @ob.get_try(self, colname, classname) end methodname_html = "#{methodname}_html".to_sym define_method(methodname_html) do |*args| obj = self.__send__(methodname) return @ob.events.call(:no_html, classname) if !obj raise "Class '#{classname}' does not have a 'html'-method." if !obj.respond_to?(:html) return obj.html(*args) end methodname_name = "#{methodname}_name".to_sym define_method(methodname_name) do |*args| obj = self.__send__(methodname) return @ob.events.call(:no_name, classname) if !obj return obj.name(*args) end self.joined_tables( classname => { :where => { "id" => {:type => :col, :name => colname} } } ) end end |
.has_translation(arr) ⇒ Object
This method initializes joins, sets methods to update translations and makes the translations automatically be deleted when the object is deleted.
Examples
class Models::Article < Knj::Datarow
#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.
has_translation [:title, :content]
end
article = ob.get(:Article, 1) print “The title in the current language is: ‘#articlearticle.title’.”
article.title = ‘Title in english if the language is english’
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/knj/datarow.rb', line 278 def self.has_translation(arr) @translations = [] if !@translations arr.each do |val| @translations << val val_dc = val.to_s.downcase table_name = "Translation_#{val_dc}".to_sym joined_tables( table_name => { :where => { "object_class" => self.name, "object_id" => {:type => :col, :name => :id}, "key" => val.to_s, "locale" => proc{|d| _session[:locale]} }, :parent_table => :Translation, :datarow => Knj::Translations::Translation, :ob => @ob } ) self.define_translation_methods(:val => val, :val_dc => val_dc) end end |
.initialized? ⇒ Boolean
Returns true if this class has been initialized.
46 47 48 49 |
# File 'lib/knj/datarow.rb', line 46 def self.initialized? return false if !@ob or !@columns_sqlhelper_args return true end |
.is_nullstamp?(stamp) ⇒ Boolean
This tests if a certain string is a date-null-stamp.
Examples
time_str = dbrow print “No valid date on the row.” if Knj::Datarow.is_nullstamp?(time_str)
80 81 82 83 |
# File 'lib/knj/datarow.rb', line 80 def self.is_nullstamp?(stamp) return true if !stamp or stamp == "0000-00-00 00:00:00" or stamp == "0000-00-00" return false end |
.joined_tables(hash) ⇒ Object
Returns data about joined tables for this class.
311 312 313 314 |
# File 'lib/knj/datarow.rb', line 311 def self.joined_tables(hash) @columns_joined_tables = {} if !@columns_joined_tables @columns_joined_tables.merge!(hash) end |
.list(d, &block) ⇒ Object
This method helps returning objects and supports various arguments. It should be called by Object#list.
Examples
ob.list(:User, => “john doe”) do |user|
print user.id
end
array = ob.list(:User, => 1) print array.length
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/knj/datarow.rb', line 433 def self.list(d, &block) ec_col = d.db.enc_col ec_table = d.db.enc_table table_str = "#{ec_table}#{d.db.esc_table(self.table)}#{ec_table}" if d.args["count"] count = true d.args.delete("count") sql = "SELECT COUNT(#{table_str}.#{ec_col}id#{ec_col}) AS count" elsif d.args["select_col_as_array"] select_col_as_array = true sql = "SELECT #{table_str}.#{ec_col}#{d.args["select_col_as_array"]}#{ec_col} AS id" d.args.delete("select_col_as_array") else sql = "SELECT #{table_str}.*" end qargs = nil ret = self.list_helper(d) sql << " FROM #{table_str}" sql << ret[:sql_joins] sql << " WHERE 1=1" sql << ret[:sql_where] d.args.each do |key, val| case key when "return_sql" #ignore when :cloned_ubuf qargs = {:cloned_ubuf => true} else raise "Invalid key: '#{key}' for '#{self.name}'. Valid keys are: '#{@columns_sqlhelper_args[:cols].keys.sort}'. Date-keys: '#{@columns_sqlhelper_args[:cols_date]}'." end end #The count will bug if there is a group-by-statement. grp_shown = false if !count and !ret[:sql_groupby] sql << " GROUP BY #{table_str}.#{ec_col}id#{ec_col}" grp_shown = true end if ret[:sql_groupby] if !grp_shown sql << " GROUP BY" else sql << ", " end sql << ret[:sql_groupby] end sql << ret[:sql_order] sql << ret[:sql_limit] return sql.to_s if d.args["return_sql"] if select_col_as_array enum = Enumerator.new do |yielder| d.db.q(sql, qargs) do |data| yielder << data[:id] end end if block enum.each(&block) return nil elsif d.ob.args[:array_enum] return Array_enumerator.new(enum) else return enum.to_a end elsif count ret = d.db.query(sql).fetch return ret[:count].to_i if ret return 0 end return d.ob.list_bysql(self.classname, sql, qargs, &block) end |
.list_helper(d) ⇒ Object
Helps call ‘sqlhelper’ on Knj::Objects to generate SQL-strings.
517 518 519 520 521 |
# File 'lib/knj/datarow.rb', line 517 def self.list_helper(d) self.load_columns(d) if !@columns_sqlhelper_args @columns_sqlhelper_args[:table] = @table if @table return d.ob.sqlhelper(d.args, @columns_sqlhelper_args) end |
.load_columns(d) ⇒ Object
Called by Knj::Objects to initialize the model and load column-data on-the-fly.
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/knj/datarow.rb', line 351 def self.load_columns(d) @ob = d.ob if !@ob @classname = self.name.split("::").last if !@classname @mutex = Monitor.new if !@mutex @mutex.synchronize do inst_methods = self.instance_methods(false) sqlhelper_args = { :db => d.db, :table => table, :cols_bools => [], :cols_date => [], :cols_dbrows => [], :cols_num => [], :cols_str => [], :cols => {} } sqlhelper_args[:table] = @table if @table d.db.tables[table].columns do |col_obj| col_name = col_obj.name col_type = col_obj.type col_type = :int if col_type == :bigint or col_type == :tinyint or col_type == :mediumint or col_type == :smallint sqlhelper_args[:cols][col_name] = true self.define_bool_methods(:inst_methods => inst_methods, :col_name => col_name) if col_type == :enum and col_obj.maxlength == "'0','1'" sqlhelper_args[:cols_bools] << col_name elsif col_type == :int and col_name.slice(-3, 3) == "_id" sqlhelper_args[:cols_dbrows] << col_name elsif col_type == :int or col_type == :decimal sqlhelper_args[:cols_num] << col_name elsif col_type == :varchar or col_type == :text or col_type == :enum sqlhelper_args[:cols_str] << col_name elsif col_type == :date or col_type == :datetime sqlhelper_args[:cols_date] << col_name self.define_date_methods(:inst_methods => inst_methods, :col_name => col_name) end if col_type == :int or col_type == :decimal self.define_numeric_methods(:inst_methods => inst_methods, :col_name => col_name) end if col_type == :int or col_type == :varchar self.define_text_methods(:inst_methods => inst_methods, :col_name => col_name) end if col_type == :time self.define_time_methods(:inst_methods => inst_methods, :col_name => col_name) end end if @columns_joined_tables @columns_joined_tables.each do |table_name, table_data| table_data[:where].each do |key, val| val[:table] = self.table.to_sym if val.is_a?(Hash) and !val.key?(:table) and val[:type].to_sym == :col end table_data[:datarow] = @ob.args[:module].const_get(table_name.to_sym) if !table_data.key?(:datarow) end sqlhelper_args[:joined_tables] = @columns_joined_tables end @columns_sqlhelper_args = sqlhelper_args end self.init_class(d) if self.respond_to?(:init_class) end |
.ob ⇒ Object
Get the ‘Knj::Objects’-object that handels this class.
65 66 67 |
# File 'lib/knj/datarow.rb', line 65 def self.ob return @ob end |
.required_data ⇒ Object
This is used by ‘Knj::Objects’ to find out what data is required for this class. Returns the array that tells about required data.
Examples
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. class Models::User < Knj::Datarow
has_one [
{:class => :Group, :col => :group_id, :method => :group, :required => true}
]
end
27 28 29 30 |
# File 'lib/knj/datarow.rb', line 27 def self.required_data @required_data = [] if !@required_data return @required_data end |
.table ⇒ Object
Returns the table-name that should be used for this datarow.
Examples
db.query(“SELECT * FROM ‘#Models::User.table` WHERE username = ’John Doe’”) do |data|
print data[:id]
end
321 322 323 324 |
# File 'lib/knj/datarow.rb', line 321 def self.table return @table if @table return self.name.split("::").last end |
.table=(newtable) ⇒ Object
This can be used to manually set the table-name. Useful when meta-programming classes that extends the datarow-class.
Examples
Models::User.table = “prefix_User”
329 330 331 332 |
# File 'lib/knj/datarow.rb', line 329 def self.table=(newtable) @table = newtable @columns_sqlhelper_args[:table] = @table if @columns_sqlhelper_args.is_a?(Hash) end |
.translations ⇒ Object
This returns all translations for this datarow-class.
306 307 308 |
# File 'lib/knj/datarow.rb', line 306 def self.translations return @translations end |
Instance Method Details
#[](key) ⇒ Object
Returns a specific data from the object by key. print “Username: #:usernamen” print “ID: #:idn” print “ID again: #Knj::Datarow.useruser.idn”
629 630 631 632 633 634 635 636 |
# File 'lib/knj/datarow.rb', line 629 def [](key) raise "Key was not a symbol: '#{key.class.name}'." if !key.is_a?(Symbol) return @id if !@data and key == :id and @id self.reload if @should_reload raise "No data was loaded on the object? Maybe you are trying to call a deleted object? (#{self.class.classname}(#{@id}), #{@should_reload})" if !@data return @data[key] if @data.key?(key) raise "No such key: '#{key}' on '#{self.class.name}' (#{@data.keys.join(", ")}) (#{@should_reload})." end |
#[]=(key, value) ⇒ Object
Writes/updates a keys value on the object. user = ob.get_by(:User, => “John Doe”) user = ‘New username’
641 642 643 644 |
# File 'lib/knj/datarow.rb', line 641 def []=(key, value) self.update(key.to_sym => value) self.should_reload end |
#__object_unique_id__ ⇒ Object
This enable Wref to not return the wrong object.
654 655 656 657 |
# File 'lib/knj/datarow.rb', line 654 def __object_unique_id__ return 0 if self.deleted? return self.id end |
#data ⇒ Object
Returns the data-hash that contains all the data from the database.
8 9 10 11 |
# File 'lib/knj/datarow.rb', line 8 def data self.reload if @should_reload return @data end |
#deleted? ⇒ Boolean
Returns true if the object has been deleted.
Examples
print “That user is deleted.” if user.deleted?
620 621 622 623 |
# File 'lib/knj/datarow.rb', line 620 def deleted? return true if !@ob and !@data and !@id return false end |
#destroy ⇒ Object
Forcefully destroys the object. This is done after deleting it and should not be called manually.
600 601 602 603 604 605 606 |
# File 'lib/knj/datarow.rb', line 600 def destroy @id = nil @ob = nil @db = nil @data = nil @should_reload = nil end |
#each(*args, &block) ⇒ Object
Loops through the data on the object.
Examples
user = ob.get(:User, 1) user.each do |key, val|
print "#{key}: #{val}\n" #=> username: John Doe
end
692 693 694 695 |
# File 'lib/knj/datarow.rb', line 692 def each(*args, &block) self.reload if @should_reload return @data.each(*args, &block) end |
#id ⇒ Object
Returns the objects ID.
647 648 649 650 651 |
# File 'lib/knj/datarow.rb', line 647 def id raise "This object has been deleted." if self.deleted? raise "No ID on object." if !@id return @id end |
#is_knj? ⇒ Boolean
This helps various parts of the framework determine if this is a datarow class without requiring it.
Examples
print “This is a knj-object.” if obj.respond_to?(“is_knj?”)
72 73 74 |
# File 'lib/knj/datarow.rb', line 72 def is_knj? return true end |
#key?(key) ⇒ Boolean Also known as: has_key?
Returns true if that key exists on the object.
Examples
print “Looks like the user has a name.” if user.key?(:name)
611 612 613 614 |
# File 'lib/knj/datarow.rb', line 611 def key?(key) self.reload if @should_reload return @data.key?(key.to_sym) end |
#name ⇒ Object Also known as: title
Tries to figure out, and returns, the possible name or title for the object.
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
# File 'lib/knj/datarow.rb', line 660 def name self.reload if @should_reload if @data.key?(:title) return @data[:title] elsif @data.key?(:name) return @data[:name] end obj_methods = self.class.instance_methods(false) [:name, :title].each do |method_name| return self.method(method_name).call if obj_methods.index(method_name) end raise "Couldnt figure out the title/name of the object on class #{self.class.name}." end |
#name_html ⇒ Object
Calls the name-method and returns a HTML-escaped value. Also “[no name]” if the name is empty.
678 679 680 681 682 |
# File 'lib/knj/datarow.rb', line 678 def name_html name_str = self.name.to_s name_str = "[no name]" if name_str.length <= 0 return name_str end |
#reload ⇒ Object
575 576 577 578 579 |
# File 'lib/knj/datarow.rb', line 575 def reload @data = @db.single(self.table, {:id => @id}) raise Knj::Errors::NotFound, "Could not find any data for the object with ID: '#{@id}' in the table '#{self.table}'." if !@data @should_reload = false end |
#should_reload ⇒ Object
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.
Examples
obj = _ob.get(:User, 5) obj.should_reload
585 586 587 588 |
# File 'lib/knj/datarow.rb', line 585 def should_reload @should_reload = true @data = nil end |
#table ⇒ Object
Returns the class-name but without having to call the class-table-method. To make code look shorter.
Examples
user = ob.get_by(:User, => ‘John Doe’) db.query(“SELECT * FROM ‘#Knj::Datarow.useruser.table` WHERE username = ’John Doe’”) do |data|
print data[:id]
end
340 341 342 |
# File 'lib/knj/datarow.rb', line 340 def table return self.class.table end |
#update(newdata) ⇒ Object
Writes/updates new data for the object.
Examples
user.update(:username => ‘New username’, :date_changed => Time.now)
593 594 595 596 597 |
# File 'lib/knj/datarow.rb', line 593 def update(newdata) @db.update(self.table, newdata, {:id => @id}) self.should_reload @ob.call("object" => self, "signal" => "update") if @ob end |