Class: SdbDal::DomainObject
- Inherits:
-
Object
- Object
- SdbDal::DomainObject
- Defined in:
- lib/sdb_dal/domain_object.rb
Constant Summary collapse
- @@is_encrypted =
false
- @@items_to_commit =
nil
- @@transaction_depth =
0
Instance Attribute Summary collapse
-
#sdb_key ⇒ Object
Returns the value of attribute sdb_key.
Class Method Summary collapse
- .arrayify(x) ⇒ Object
-
.AttributeDescription(name) ⇒ Object
end.
- .belongs_to(domain_class, foreign_key_attribute = nil, accesser_attribute_name = nil, options = {}) ⇒ Object
- .ColumnDescription(name) ⇒ Object
- .data_attribute(name, type, options = {}) ⇒ Object
- .encrypt_me ⇒ Object
- .find(*arguments) ⇒ Object
- .find_by_index(values, more_options) ⇒ Object
- .find_every(options) ⇒ Object
- .find_list(primary_keys, options = {}) ⇒ Object
- .find_one(options) ⇒ Object
- .find_recent_with_exponential_expansion(time_column, how_many, options = {}) ⇒ Object
- .find_single(id, options) ⇒ Object
- .has_many(domain_class, reflecting_key_attributes = nil, accesser_attribute_name = nil, options = {}) ⇒ Object
- .index(index_name, columns, options = {}) ⇒ Object
- .index_descriptions ⇒ Object
- .index_names ⇒ Object
- .is_encrypted? ⇒ Boolean
- .istantiate(sdb_attributes, repository) ⇒ Object
- .many_to_many(domain_class, through_class, reflecting_key_attributes = nil, foriegn_key_attribute = nil, accesser_attribute_name = nil, options = {}) ⇒ Object
- .module_name ⇒ Object
- .primary_key_attribute_names ⇒ Object
- .query_ids(options = {}) ⇒ Object
- .repository(options = {}) ⇒ Object
- .sort_result(results, sort_by, order = :ascending) ⇒ Object
- .table_name ⇒ Object
- .transaction ⇒ Object
Instance Method Summary collapse
- #add_to_tracked_list(tracking_attribute, other_class, reflecting_attribute, value) ⇒ Object
- #attributes ⇒ Object
- #copy_attributes(hash) ⇒ Object
- #destroy(options = {}) ⇒ Object
- #find_tracked_list(tracking_attribute, other_class, reflecting_attribute) ⇒ Object
- #get_attribute(name) ⇒ Object
- #get_sortable_attribute(attr_name) ⇒ Object
- #id ⇒ Object
- #index_values ⇒ Object
-
#initialize(options = {}) ⇒ DomainObject
constructor
A new instance of DomainObject.
-
#method_missing(method_symbol, *arguments) ⇒ Object
:nodoc:.
- #primary_key ⇒ Object
- #primary_key=(value) ⇒ Object
- #primary_key_hash ⇒ Object
-
#repository(options = nil) ⇒ Object
def find.
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
- #set_attribute(name, value) ⇒ Object
- #set_map(keys, values) ⇒ Object
- #table_name ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ DomainObject
Returns a new instance of DomainObject.
14 15 16 17 18 19 20 21 |
# File 'lib/sdb_dal/domain_object.rb', line 14 def initialize(={}) @attribute_values=HashWithIndifferentAccess.new copy_attributes() @accessor_cache={} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
:nodoc:
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/sdb_dal/domain_object.rb', line 560 def method_missing(method_symbol, *arguments) #:nodoc: method_name = method_symbol.to_s case method_name.last when "=" if @attribute_values.has_key?(method_name.first(-1)) @attribute_values[method_name.first(-1)] = arguments.first else super end when "?" @attribute_values[method_name.first(-1)] else @attribute_values.has_key?(method_name) ? @attribute_values[method_name] : super end end |
Instance Attribute Details
#sdb_key ⇒ Object
Returns the value of attribute sdb_key.
13 14 15 |
# File 'lib/sdb_dal/domain_object.rb', line 13 def sdb_key @sdb_key end |
Class Method Details
.arrayify(x) ⇒ Object
794 795 796 797 798 799 800 |
# File 'lib/sdb_dal/domain_object.rb', line 794 def arrayify(x) unless x.is_a?(Array) x=[x] end return x end |
.AttributeDescription(name) ⇒ Object
end
530 531 532 |
# File 'lib/sdb_dal/domain_object.rb', line 530 def self.AttributeDescription(name) return attribute_descriptions[name] end |
.belongs_to(domain_class, foreign_key_attribute = nil, accesser_attribute_name = nil, options = {}) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/sdb_dal/domain_object.rb', line 288 def self.belongs_to(domain_class,foreign_key_attribute=nil,accesser_attribute_name=nil,={}) foreign_key_attribute ||= "#{domain_class.to_s.downcase}_id".to_sym accesser_attribute_name ||=domain_class.to_s.downcase if foreign_key_attribute==:without_prefix # #send all attributes and let the other class figure it out} class_eval <<-GETTERDONE def #{accesser_attribute_name} #{module_name+domain_class.to_s}.find(@attribute_values) end GETTERDONE elsif foreign_key_attribute==:with_prefix # #the column names that start with the name of the other class are the # fkeys fkeys=[] self.class.attribute_descriptions.each do |attribute_name,description| if attribute_name.index(domain_class.to_s.downcase)==0 fkeys<<attribute_name fkey_names<<attribute_name.to_s end end index(fkey_names.join("_"),keys) class_eval <<-GETTERDONE def #{accesser_attribute_name} mapped_values={} prefix='#{domain_class.to_s.downcase}' ['#{fkey_names.join("','")}'].each |key| mapped_values[key.splice(prefix.length)]=@attribute_values[key] end #{module_name+domain_class.to_s}.find(mapped_values) end GETTERDONE else class_eval <<-GETTERDONE def #{accesser_attribute_name} #{module_name+domain_class.to_s}.find(self.#{foreign_key_attribute}) end GETTERDONE index("#{foreign_key_attribute}_index".to_sym,[foreign_key_attribute]) end end |
.ColumnDescription(name) ⇒ Object
533 534 535 |
# File 'lib/sdb_dal/domain_object.rb', line 533 def self.ColumnDescription(name) return column_descriptions[name] end |
.data_attribute(name, type, options = {}) ⇒ Object
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 181 182 183 184 185 186 187 188 189 190 191 192 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/sdb_dal/domain_object.rb', line 143 def self.data_attribute(name,type,={}) class_eval <<-GETTERDONE @@attribute_descriptions||=HashWithIndifferentAccess.new @@non_clob_attribute_names||=[] @@clob_attribute_names||=[] @@on_destroy_blocks||=[] def self.on_destroy_blocks @@on_destroy_blocks || [] end def self.attribute_descriptions @@attribute_descriptions end def self.non_clob_attribute_names @@non_clob_attribute_names end def self.clob_attribute_names @@clob_attribute_names end def is_dirty(attribute_name) if @attribute_values[attribute_name]!= nil && @attribute_values[attribute_name].respond_to?(:value) return @attribute_values[attribute_name].is_dirty else return true end end GETTERDONE return if self.attribute_descriptions.has_key?(name) attribute_description=DomainAttributeDescription.new(name,type,self.is_encrypted?,) self.attribute_descriptions[name] = attribute_description @primary_key_attribute_names||=[] if attribute_description.is_primary_key @primary_key_attribute_names<<name pk_code="[:#{@primary_key_attribute_names.join(",:")}]" class_eval <<-GETTERDONE def self.primary_key_attribute_names #{pk_code} end def self.exists?(primary_key) return self.find(primary_key)!=nil end GETTERDONE end if attribute_description.value_type==:clob clob_attribute_names<<attribute_description.name else non_clob_attribute_names<<attribute_description.name end scope=":all" if([:unique] && [:unique]==true) scope=":first" end if type==:reference_set class_eval <<-GETTERDONE def #{attribute_description.name} result=@attribute_values[:#{attribute_description.name}] || [] result=result.sort_by{|item|item.index} end def add_to_#{attribute_description.name}(item,index=-1) @attribute_values[:#{attribute_description.name}] ||=[] @attribute_values[:#{attribute_description.name}] <<SdbDal::Reference.new(:target=> item ,:index=>index) end def remove_from_#{attribute_description.name}(item) @attribute_values[:#{attribute_description.name}] ||=[] @attribute_values.each do |reference| if reference.targets?(item) @attribute_values.remove(reference) return end end end GETTERDONE elsif type==:clob class_eval <<-GETTERDONE def #{attribute_description.name} if @attribute_values[:#{attribute_description.name}]!= nil && @attribute_values[:#{attribute_description.name}].respond_to?(:value) return @attribute_values[:#{attribute_description.name}].value else return @attribute_values[:#{attribute_description.name}] end end GETTERDONE else class_eval <<-GETTERDONE def #{attribute_description.name} return @attribute_values[:#{attribute_description.name}] end GETTERDONE end class_eval <<-GETTERDONE def #{attribute_description.name}=(xvalue) @attribute_values[:#{attribute_description.name}] =xvalue end def #{attribute_description.name}? return @attribute_values[:#{attribute_description.name}] end def self.find_by_#{attribute_description.name}(#{attribute_description.name.to_s.downcase},options={}) options[:params]={:#{attribute_description.name}=>#{attribute_description.name.to_s.downcase}} find(#{scope},options) end GETTERDONE if .has_key?(:enum) attribute_name=attribute_description.name.to_s x="" enum_val=1 [:enum].each { |enum| x<<"#{enum.to_s.upcase} = #{enum_val}\n" enum_val+=1 class_eval <<-GETTERDONE def is_#{attribute_name}_#{enum.to_s.downcase}? #{attribute_name} == #{attribute_name.capitalize}::#{enum.to_s.upcase} end GETTERDONE } class_eval <<-GETTERDONE class #{attribute_name.capitalize} #{x} end GETTERDONE end end |
.encrypt_me ⇒ Object
138 139 140 141 142 |
# File 'lib/sdb_dal/domain_object.rb', line 138 def self.encrypt_me class_eval <<-GETTERDONE @@is_encrypted=true GETTERDONE end |
.find(*arguments) ⇒ Object
746 747 748 749 750 751 752 753 754 755 |
# File 'lib/sdb_dal/domain_object.rb', line 746 def find(*arguments) scope = arguments.slice!(0) = arguments.slice!(0) || {} case scope when :all then return find_every() when :first then return find_one() else return find_single(scope, ) end end |
.find_by_index(values, more_options) ⇒ Object
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
# File 'lib/sdb_dal/domain_object.rb', line 728 def find_by_index(values,) index_descriptions.each do |name,desc| if desc.keys_match?(values) ={} [:params]={desc.name=>desc.format_index_entry(self.attribute_descriptions,values)} [:index]=desc.name [:index_value]=desc.format_index_entry(self.attribute_descriptions,values) .merge!() return find(:all,) end end ={} [:params]={} .merge!() values.each{|key,value|[:params][key]=value} return find_every() end |
.find_every(options) ⇒ Object
760 761 762 763 764 765 766 767 768 769 770 |
# File 'lib/sdb_dal/domain_object.rb', line 760 def find_every() results=[] untyped_results=self.repository.query(self.table_name,attribute_descriptions,) untyped_results.each do |item_attributes| results<<istantiate(item_attributes,self.repository()) end return results end |
.find_list(primary_keys, options = {}) ⇒ Object
771 772 773 774 775 776 777 778 |
# File 'lib/sdb_dal/domain_object.rb', line 771 def find_list(primary_keys,={}) result=[] primary_keys.each do |key| item=find_single(key,) result<<item if item end return result end |
.find_one(options) ⇒ Object
756 757 758 759 |
# File 'lib/sdb_dal/domain_object.rb', line 756 def find_one() [:limit]=1 find_every().first end |
.find_recent_with_exponential_expansion(time_column, how_many, options = {}) ⇒ Object
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 |
# File 'lib/sdb_dal/domain_object.rb', line 821 def find_recent_with_exponential_expansion(time_column,how_many,={}) found=[] tries=0 timespan=[:timespan_hint] || 60*60*4 params = [:params] || {} while found.length<how_many && tries<4 time=Time.now.gmtime-timespan params[time_column] =AttributeRange.new(:greater_than=>time) found= find(:all,:limit=>how_many,:order=>:descending,:order_by => time_column, :params=>params) tries=tries+1 timespan=timespan*3 end return found end |
.find_single(id, options) ⇒ Object
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
# File 'lib/sdb_dal/domain_object.rb', line 801 def find_single(id, ) return nil if id==nil if id.is_a?(Hash) id_as_array=[] @primary_key_attribute_names.each do |key_name| id_as_array << id[key_name] end id=id_as_array end id=arrayify(id) attributes=self.repository().find_one(self.table_name,id,attribute_descriptions) if attributes && attributes.length>0 @primary_key_attribute_names.each do |key_name| attributes[key_name]=id.shift end attributes[:repository]=self.repository() return istantiate(attributes,self.repository()) end return nil end |
.has_many(domain_class, reflecting_key_attributes = nil, accesser_attribute_name = nil, options = {}) ⇒ Object
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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/sdb_dal/domain_object.rb', line 448 def self.has_many(domain_class, reflecting_key_attributes=nil, accesser_attribute_name=nil, ={}) if reflecting_key_attributes==:without_prefix reflecting_key_attributes = primary_key_attribute_names.map{|x|x.to_s} end reflecting_key_attributes ||= primary_key_attribute_names.map{|x|self.name.downcase+"_"+x.to_s} reflecting_key_attributes =arrayify(reflecting_key_attributes) reflecting_array_code="[:"+reflecting_key_attributes.join(",:")+"]" accesser_attribute_name ||=domain_class.to_s.downcase+'s' order="" if [:order_by] order="{:order_by=>:#{[:order_by]}" end if [:order] if order.length>0 order << "," else order<<"{" end order<<":order=>:#{[:order]}" end if order.length==0 order << "{}" else order<<"}" end if [:dependent] class_eval <<-XXDONE on_destroy_blocks<<"#{accesser_attribute_name}.each{|item|item.destroy}" XXDONE end class_eval <<-XXDONE def create_child_#{domain_class.to_s.downcase}(options=nil) result=#{module_name+domain_class.to_s}.new(options) result.copy_attributes(self.primary_key_hash) result end XXDONE # if options[:tracking] # #add add_xxx method # #add to list of tracker attributes # # class_eval <<-XXDONE # # @@attribute_descriptions[ :#{accesser_attribute_name}_tracker]=TrackerDescription.new(:#{accesser_attribute_name}_tracker,:#{domain_class},:#{reflecting_key_attribute}) # # def #{accesser_attribute_name} # find_tracked_list(:#{accesser_attribute_name}_tracker,#{domain_class},:#{reflecting_key_attribute}) # # end # def add_to_#{accesser_attribute_name}(item) # # item.save! # add_to_tracked_list(:#{accesser_attribute_name}_tracker,#{domain_class.to_s},:#{reflecting_key_attribute},item.primary_key) # item.set_attribute(:#{reflecting_key_attribute},self.primary_key) # # # # end # XXDONE # else class_eval <<-XXDONE def #{accesser_attribute_name} if !@accessor_cache.has_key? :#{accesser_attribute_name} h={} pkey=DomainObject::arrayify(self.primary_key) #{reflecting_array_code}.each do |key| h[key]=pkey.shift end result= #{module_name+domain_class.to_s}.find_by_index(h,#{order}) @accessor_cache[:#{accesser_attribute_name}]=result || [] end return @accessor_cache[:#{accesser_attribute_name}] end XXDONE # end end |
.index(index_name, columns, options = {}) ⇒ Object
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sdb_dal/domain_object.rb', line 67 def self.index(index_name,columns,={}) @index_descriptions||={} return if self.index_descriptions[index_name] $columns_xxx=columns class_eval <<-GETTERDONE unless @index_descriptions.has_key?(:#{index_name}) @index_names||=[] @index_names<<:#{index_name} def self.index_names @index_names end attribute_description=SdbDal::DomainAttributeDescription.new(:#{index_name},:string,{}) @index_descriptions[:#{index_name}]=SdbDal::IndexDescription.new(:#{index_name},$columns_xxx,self.is_encrypted?) end GETTERDONE getter_params=[] finder_code="" params=[] columns.each do |column| if column.respond_to?(:transform) finder_code<<"h[:#{column.name}]=#{column.name.to_s.downcase}\n" getter_params<<"@attribute_values[:#{column.source_column}]" params<<"#{column.name.to_s.downcase}" else finder_code<<"h[:#{column}]=#{column.to_s.downcase}\n" getter_params<<"@attribute_values[:#{column}]" params<<"#{column.to_s.downcase}" end end find_scope=":all" if [:unique] find_scope=":first" end class_eval <<-GETTERDONE def #{index_name} return self.class.calculate_#{index_name}(#{getter_params.join(",")}) end def self.calculate_#{index_name}(#{params.join(",")}) index_description=index_descriptions[:#{index_name}] h={} #{finder_code} index_description.format_index_entry(@@attribute_descriptions,h) end def self.find_by_#{index_name}(#{params.join(",")},options={}) options[:params]={:#{index_name}=>self.calculate_#{index_name}(#{params.join(",")})} options[:index]=:#{index_name} options[:index_value]=self.calculate_#{index_name}(#{params.join(",")}) find(#{find_scope},options) end GETTERDONE end |
.index_descriptions ⇒ Object
131 132 133 |
# File 'lib/sdb_dal/domain_object.rb', line 131 def self.index_descriptions @index_descriptions || {} end |
.index_names ⇒ Object
63 64 65 |
# File 'lib/sdb_dal/domain_object.rb', line 63 def self.index_names [] end |
.is_encrypted? ⇒ Boolean
135 136 137 |
# File 'lib/sdb_dal/domain_object.rb', line 135 def self.is_encrypted? return @@is_encrypted || false end |
.istantiate(sdb_attributes, repository) ⇒ Object
782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/sdb_dal/domain_object.rb', line 782 def istantiate(sdb_attributes,repository) this_result=new(sdb_attributes||{}) get_clob_proc=nil clob_attribute_names.each do |clob_attribute_name| get_clob_proc= proc { repository.get_clob(self.table_name,this_result.primary_key,clob_attribute_name) } this_result.set_attribute(clob_attribute_name, LazyLoadingText.new(get_clob_proc)) end this_result end |
.many_to_many(domain_class, through_class, reflecting_key_attributes = nil, foriegn_key_attribute = nil, accesser_attribute_name = nil, options = {}) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 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 |
# File 'lib/sdb_dal/domain_object.rb', line 339 def self.many_to_many(domain_class, through_class, reflecting_key_attributes=nil, foriegn_key_attribute=nil, accesser_attribute_name=nil, ={}) reflecting_key_attributes ||= primary_key_attribute_names.map{|x|self.name+"_"+x.to_s} reflecting_key_attributes =arrayify(reflecting_key_attributes) reflecting_array_code="[:"+reflecting_key_attributes.join(",:")+"]" accesser_attribute_name ||=domain_class.to_s.downcase+'s' order="" if [:order_by] if [:order] order="result=DomainObject.sort_result(result,:#{[:order_by]},:#{[:order]})" else order="result=DomainObject.sort_result(result,:#{[:order_by]},:ascending)" end end foriegn_key_attributes=[:foriegn_key_attribute] || "#{domain_class.to_s.downcase}_id".to_sym foriegn_key_attributes=arrayify(foriegn_key_attributes) fkey_array_code="[:"+foriegn_key_attributes.join(",:")+"]" class_eval <<-XXDONE def #{accesser_attribute_name} #unless @accessor_cache.has_key? :#{accesser_attribute_name} through_results= #{module_name+through_class.to_s}.find(:all,:map=>{:keys=>#{reflecting_array_code},:values=>DomainObject.arrayify(self.primary_key)}) result=[] through_results.each do |through_result| item= #{module_name+domain_class.to_s}.find(through_result.#{foriegn_key_attribute}) result<< item if item end #{order} # @accessor_cache[:#{accesser_attribute_name}]=result return result #end #return @accessor_cache[:#{accesser_attribute_name}] end def connect_#{domain_class.to_s.downcase}(#{domain_class.to_s.downcase}) connector=#{module_name+through_class.to_s}.new connector.set_map(#{fkey_array_code},DomainObject.arrayify(#{domain_class.to_s.downcase}.primary_key)) connector.set_map(#{reflecting_array_code},DomainObject.arrayify(self.primary_key)) connector.save end XXDONE end |
.module_name ⇒ Object
842 843 844 845 846 |
# File 'lib/sdb_dal/domain_object.rb', line 842 def self.module_name result="" result=self.name.slice(0, self.name.rindex(":")+1) if self.name.rindex(":") result end |
.primary_key_attribute_names ⇒ Object
43 44 45 |
# File 'lib/sdb_dal/domain_object.rb', line 43 def self.primary_key_attribute_names return @primary_key_attribute_names end |
.query_ids(options = {}) ⇒ Object
779 780 781 |
# File 'lib/sdb_dal/domain_object.rb', line 779 def query_ids(={}) self.repository.query_ids(self.table_name,attribute_descriptions,) end |
.repository(options = {}) ⇒ Object
719 720 721 722 723 724 725 726 727 |
# File 'lib/sdb_dal/domain_object.rb', line 719 def repository(={}) if .has_key?(:repository) return [:repository] end return RepositoryFactory.instance() end |
.sort_result(results, sort_by, order = :ascending) ⇒ Object
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/sdb_dal/domain_object.rb', line 415 def DomainObject.sort_result(results,sort_by,order=:ascending) non_null_results=[] null_results=[] results.each { |i| sorter_value=i.get_attribute(sort_by) if sorter_value non_null_results<<i else null_results<<i end } sorted_results=non_null_results.sort{ |a,b| a_val= a.get_sortable_attribute(sort_by) b_val= b.get_sortable_attribute(sort_by) a_val <=> b_val } if order!=:ascending sorted_results.reverse! end sorted_results.concat( null_results) sorted_results end |
.table_name ⇒ Object
696 697 698 |
# File 'lib/sdb_dal/domain_object.rb', line 696 def DomainObject.table_name return self.name end |
.transaction ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/sdb_dal/domain_object.rb', line 393 def DomainObject.transaction @@items_to_commit||=[] @@transaction_depth+=1 begin yield rescue # catch all raise $! # rethrow ensure @@transaction_depth-=1 end if @@transaction_depth==0 @@items_to_commit.uniq.each do |item | item.save! end @@items_to_commit=[] end end |
Instance Method Details
#add_to_tracked_list(tracking_attribute, other_class, reflecting_attribute, value) ⇒ Object
645 646 647 648 649 650 651 652 653 654 |
# File 'lib/sdb_dal/domain_object.rb', line 645 def add_to_tracked_list(tracking_attribute,other_class,reflecting_attribute,value) list=@attribute_values[tracking_attribute] if !list list=other_class.query_ids(:params=>{reflecting_attribute=>self.primary_key}) end list<<value @attribute_values[tracking_attribute]=list.uniq end |
#attributes ⇒ Object
621 622 623 624 625 626 627 628 629 630 |
# File 'lib/sdb_dal/domain_object.rb', line 621 def attributes result={} # #todo refactor to avoid this copy self.class.attribute_descriptions.values.each do |description| if !description.is_clob || is_dirty(description.name) result[description.name]=@attribute_values[description.name] end end return result end |
#copy_attributes(hash) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sdb_dal/domain_object.rb', line 24 def copy_attributes(hash) self.class.attribute_descriptions.each do |attribute_name,description| if hash.has_key?(attribute_name) or hash.has_key?(attribute_name.intern) value=hash[attribute_name] value=value || hash[attribute_name.intern] if !description.is_collection && value.respond_to?(:flatten) && value.length==1 value=value[0] end @attribute_values[attribute_name]=value else @attribute_values[attribute_name]=nil unless @attribute_values.has_key?(attribute_name) end end end |
#destroy(options = {}) ⇒ Object
607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'lib/sdb_dal/domain_object.rb', line 607 def destroy(={}) if self.class.on_destroy_blocks self.class.on_destroy_blocks.each do |block| instance_eval <<-XXDONE #{block} XXDONE end end if self.primary_key self.repository().destroy(self.table_name,self.primary_key) end end |
#find_tracked_list(tracking_attribute, other_class, reflecting_attribute) ⇒ Object
632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/sdb_dal/domain_object.rb', line 632 def find_tracked_list(tracking_attribute,other_class,reflecting_attribute) # #if the attribute has no value do a query list=@attribute_values[tracking_attribute] if !list list=other_class.query_ids(:params=>{reflecting_attribute=>self.primary_key}) @attribute_values[tracking_attribute]=list self.save! end return other_class.find_list(list) end |
#get_attribute(name) ⇒ Object
588 589 590 |
# File 'lib/sdb_dal/domain_object.rb', line 588 def get_attribute(name) @attribute_values[name] end |
#get_sortable_attribute(attr_name) ⇒ Object
439 440 441 442 443 444 445 446 447 |
# File 'lib/sdb_dal/domain_object.rb', line 439 def get_sortable_attribute(attr_name) a_val= self.get_attribute(attr_name) return 0 unless a_val if a_val.class== Time return a_val.to_f else return a_val end end |
#id ⇒ Object
48 49 50 51 |
# File 'lib/sdb_dal/domain_object.rb', line 48 def id return @attribute_values[:id] if self.class.attribute_descriptions[:id] return base.id end |
#index_values ⇒ Object
576 577 578 579 580 581 582 583 584 |
# File 'lib/sdb_dal/domain_object.rb', line 576 def index_values result={} self.class.index_names.each do |name| result[name]= self.send("#{name}") end result end |
#primary_key ⇒ Object
536 537 538 539 540 541 542 543 544 |
# File 'lib/sdb_dal/domain_object.rb', line 536 def primary_key result=[] self.class.primary_key_attribute_names.each do |key_part| result<<@attribute_values[key_part ] end return result[0] if result.length==1 return result end |
#primary_key=(value) ⇒ Object
553 554 555 556 557 558 559 |
# File 'lib/sdb_dal/domain_object.rb', line 553 def primary_key=(value) key=DomainObject::arrayify(value) self.class.primary_key_attribute_names.each do |key_part| @attribute_values[ key_part]=key.shift end end |
#primary_key_hash ⇒ Object
545 546 547 548 549 550 551 552 |
# File 'lib/sdb_dal/domain_object.rb', line 545 def primary_key_hash result={} self.class.primary_key_attribute_names.each do |key_part| result[key_part]=@attribute_values[key_part ] end return result end |
#repository(options = nil) ⇒ Object
def find
attributes=repository.find(self.class.name,self.primary_key,@@non_clob_attribute_names,@@clob_attribute_names)
attributes.each do |key,value|
@attribute_values[key]=value
end
end
706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/sdb_dal/domain_object.rb', line 706 def repository(=nil) if and .has_key?(:repository) return [:repository] end if @repository return @repository end return self.class.repository end |
#save(options = {}) ⇒ Object
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/sdb_dal/domain_object.rb', line 658 def save(={}) if !self.primary_key self.primary_key= SdbDal::UUID.generate end if @@transaction_depth>0 # #we are in a transaction. wait to commit @@items_to_commit<<self else temp_accessor_cache=@accessor_cache @accessor_cache=nil # $service.put_attributes(class_object.table_name,item.send(name_column),attributes,true) attributes={} # #todo refactor to avoid this copy self.class.attribute_descriptions.values.each do |description| if !description.is_clob || is_dirty(description.name) value=@attribute_values[description.name] attributes[description]=value end end self.index_values.each do |name,value| attributes[self.class.index_descriptions[name]]=value end self.repository().save(self.table_name,self.primary_key,attributes,self.class.index_descriptions) @accessor_cache=temp_accessor_cache end end |
#save!(options = {}) ⇒ Object
655 656 657 |
# File 'lib/sdb_dal/domain_object.rb', line 655 def save!(={}) save() end |
#set_attribute(name, value) ⇒ Object
585 586 587 |
# File 'lib/sdb_dal/domain_object.rb', line 585 def set_attribute(name,value) @attribute_values[name]=value end |
#set_map(keys, values) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/sdb_dal/domain_object.rb', line 52 def set_map(keys,values) (0..keys.length-1).each do |i| key=keys[i] value=values[i] @attribute_values[key]=value end end |
#table_name ⇒ Object
693 694 695 |
# File 'lib/sdb_dal/domain_object.rb', line 693 def table_name return self.class.table_name end |
#to_xml ⇒ Object
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/sdb_dal/domain_object.rb', line 591 def to_xml result= "<#{self.class.table_name}>" attributes.each do |key,value| if value.respond_to?(:flatten) result<<"<#{key}>" value.each do |sub_value| result<<"<value>#{h sub_value.to_s}</value>" end result<<"</#{key}>" else result<<"<#{key}>#{h value.to_s}</#{key}>" end end result<< "</#{self.class.table_name}>" result end |