Class: Trix51::Tuple
Overview
Represents a single record from the database.
Constant Summary
Constants inherited from Trix51
KEY_JOIN, META_PREFIX, RECORD_PREFIX, SEQ_PREFIX
Instance Attribute Summary collapse
-
#data ⇒ Object
Accessors for the tuple class.
-
#table ⇒ Object
Accessors for the tuple class.
-
#urid ⇒ Object
Accessors for the tuple class.
Instance Method Summary collapse
-
#has_updates? ⇒ Boolean
Returns true or false if the record has been updated.
-
#initialize(table, urid, anonhash = nil) ⇒ Tuple
constructor
Create a new tuple object.
-
#method_missing(methodId, *args) ⇒ Object
:nodoc:.
-
#update ⇒ Object
post any record updates, update any indexes and keys.
Methods inherited from Trix51
add_connection, add_helper, class_to_database, class_to_table, connections, create_helper_code, debug, defer_classref, defer_classref=, error, fatal, info, table_to_database, warn
Constructor Details
#initialize(table, urid, anonhash = nil) ⇒ Tuple
Create a new tuple object
Arguments:
- table
-
A Trix51::Table object
- urid
-
A unique record identifier
- anonhash
-
An optional anonymous hash containing the data for the tuple.
363 364 365 366 367 368 369 |
# File 'lib/trix51db.rb', line 363 def initialize( table, urid, anonhash=nil ) @table = table @data = Marshal.load( @table.dbref[urid] ) if anonhash.nil? @data = anonhash if not anonhash.nil? @newdata = {} @urid = urid end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(methodId, *args) ⇒ Object
:nodoc:
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 424 425 |
# File 'lib/trix51db.rb', line 376 def method_missing( methodId, *args ) #:nodoc: name = methodId.to_s basename = name.gsub( '=', '' ).to_sym if not @table.structure[basename].nil? then #return @data[name] if name.match( '=' ) then @newdata[basename] = args.shift #puts "setting value of #{basename} to #{@newdata[basename]}.." else return @newdata[basename] || @data[basename] if ['string', 'integer', 'datetime'].include?( self.table.structure[basename][:datatype]) # derived fields dt = self.table.structure[basename][:datatype] "------------------------------" if dt == 'record' then # handle one to one #pp self.table.structure[basename][:table] dbref = Trix51.table_to_database( self.table.structure[basename][:table] ) #pp dbref tblref = dbref.get_table( self.table.structure[basename][:table] ) srcref = self.table.structure[basename][:source] dstref = self.table.structure[basename][:dest] return tblref.first( dstref => @data[srcref] ) end if dt == 'resultset' then dbref = Trix51.table_to_database( self.table.structure[basename][:table] ) tblref = dbref.get_table( self.table.structure[basename][:table] ) srcref = self.table.structure[basename][:source] dstref = self.table.structure[basename][:dest] #puts "#{tblref.tablename}.#{dstref} = #{@data[srcref]}" h = { dstref => @data[srcref].to_s } return tblref.select_hash( h ) end if dt == 'calculated' then c = self.table.structure[basename][:calculation] return eval( c ).to_s end end else raise Trix51::NotFoundError, "No such field #{methodId}" end end |
Instance Attribute Details
#data ⇒ Object
Accessors for the tuple class.
473 474 475 |
# File 'lib/trix51db.rb', line 473 def data @data end |
#table ⇒ Object
Accessors for the tuple class.
473 474 475 |
# File 'lib/trix51db.rb', line 473 def table @table end |
#urid ⇒ Object
Accessors for the tuple class.
473 474 475 |
# File 'lib/trix51db.rb', line 473 def urid @urid end |
Instance Method Details
#has_updates? ⇒ Boolean
Returns true or false if the record has been updated.
372 373 374 |
# File 'lib/trix51db.rb', line 372 def has_updates? return (@newdata.keys.length > 0) end |
#update ⇒ Object
post any record updates, update any indexes and keys.
428 429 430 431 432 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 |
# File 'lib/trix51db.rb', line 428 def update ref = @data.merge( @newdata ) { |key, oldval, newval| (not newval.nil?) ? newval : oldval } # ref now represents the new record newurid = self.table.get_hash_key( ref ) @newdata.each_pair do |fieldname,fieldvalue| raise Trix51::ConstraintError, "Update would violate unique constraint on field #{fieldname}" unless self.table.index_chk_unique_update( fieldname, fieldvalue, @urid ) end if newurid == @urid then self.table.index_update( @urid, newurid, @newdata ) # simple update self.table.dbref[ @urid ] = Marshal.dump( ref ) @data = ref @newdata = {} #puts "Key is unchanged" return true end # if we are here the key has changed # make sure the new one does not exist if not self.table.dbref[ newurid ].nil? then @newdata = {} raise Trix51::ConstraintError, "Update to record would violate unique contraints" end # if we are here, add the new record self.table.index_update( @urid, newurid, @newdata ) #puts "Re-keying the record due to a change in unique key" self.table.dbref[ newurid ] = Marshal.dump( ref ) @data = @newdata @newdata = {} @urid = newurid return true end |