sdb_dal 0.0.6 → 0.0.7
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.
- data/lib/sdb_dal/domain_object.rb +71 -10
- data/lib/sdb_dal/repository.rb +1 -0
- metadata +1 -1
@@ -15,19 +15,24 @@ module SdbDal
|
|
15
15
|
def initialize(options={})
|
16
16
|
@attribute_values=HashWithIndifferentAccess.new
|
17
17
|
|
18
|
+
copy_attributes(options)
|
19
|
+
|
20
|
+
@accessor_cache={}
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def copy_attributes(hash)
|
18
25
|
self.class.attribute_descriptions.each do |attribute_name,description|
|
19
|
-
value=
|
20
|
-
value=value ||
|
26
|
+
value=hash[attribute_name]
|
27
|
+
value=value || hash[attribute_name.intern]
|
21
28
|
if !description.is_collection && value.respond_to?(:flatten) && value.length==1
|
22
29
|
value=value[0]
|
23
30
|
end
|
24
31
|
@attribute_values[attribute_name]=value
|
25
|
-
|
32
|
+
|
26
33
|
end
|
27
|
-
@accessor_cache={}
|
28
|
-
@repository=options[:repository]
|
29
|
-
end
|
30
34
|
|
35
|
+
end
|
31
36
|
|
32
37
|
def self.primary_key_attribute_names
|
33
38
|
return @primary_key_attribute_names
|
@@ -250,16 +255,44 @@ module SdbDal
|
|
250
255
|
end
|
251
256
|
|
252
257
|
def self.belongs_to(domain_class,foreign_key_attribute=nil,accesser_attribute_name=nil,options={})
|
253
|
-
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
254
262
|
foreign_key_attribute ||= "#{domain_class.to_s.downcase}_id".to_sym
|
255
263
|
accesser_attribute_name ||=domain_class.to_s.downcase
|
264
|
+
if foreign_key_attribute==:without_prefix
|
265
|
+
|
266
|
+
class_eval <<-GETTERDONE
|
267
|
+
def #{accesser_attribute_name}
|
268
|
+
#{module_name+domain_class.to_s}.find(@attribute_values)
|
269
|
+
end
|
270
|
+
GETTERDONE
|
271
|
+
|
256
272
|
|
273
|
+
elsif foreign_key_attribute==:with_prefix
|
274
|
+
|
257
275
|
class_eval <<-GETTERDONE
|
258
276
|
def #{accesser_attribute_name}
|
259
|
-
|
277
|
+
mapped_values={}
|
278
|
+
prefix='#{domain_class.to_s.downcase}_'
|
279
|
+
@attribute_values.each{|key,value|
|
280
|
+
if key.index(prefix)==0
|
281
|
+
mapped_values[key.splice(prefix.length)]=value
|
282
|
+
end
|
283
|
+
#{module_name+domain_class.to_s}.find(mapped_values)
|
284
|
+
end
|
285
|
+
GETTERDONE
|
286
|
+
|
287
|
+
else
|
288
|
+
class_eval <<-GETTERDONE
|
289
|
+
def #{accesser_attribute_name}
|
290
|
+
mapped_values={}
|
291
|
+
#{module_name+domain_class.to_s}.find(self.#{foreign_key_attribute})
|
260
292
|
end
|
261
|
-
|
262
293
|
GETTERDONE
|
294
|
+
|
295
|
+
end
|
263
296
|
end
|
264
297
|
def self.many_to_many(domain_class,
|
265
298
|
through_class,
|
@@ -374,6 +407,10 @@ module SdbDal
|
|
374
407
|
reflecting_key_attributes=nil,
|
375
408
|
accesser_attribute_name=nil,
|
376
409
|
options={})
|
410
|
+
if reflecting_key_attributes==:without_prefix
|
411
|
+
reflecting_key_attributes = primary_key_attribute_names.map{|x|x.to_s}
|
412
|
+
|
413
|
+
end
|
377
414
|
reflecting_key_attributes ||= primary_key_attribute_names.map{|x|self.name.downcase+"_"+x.to_s}
|
378
415
|
reflecting_key_attributes =arrayify(reflecting_key_attributes)
|
379
416
|
reflecting_array_code="[:"+reflecting_key_attributes.join(",:")+"]"
|
@@ -386,7 +423,8 @@ module SdbDal
|
|
386
423
|
if options[:order]
|
387
424
|
order<<",:order=>:#{options[:order]}"
|
388
425
|
end
|
389
|
-
|
426
|
+
|
427
|
+
|
390
428
|
if options[:dependent]
|
391
429
|
|
392
430
|
class_eval <<-XXDONE
|
@@ -394,6 +432,14 @@ module SdbDal
|
|
394
432
|
on_destroy_blocks<<"#{accesser_attribute_name}.each{|item|item.destroy}"
|
395
433
|
XXDONE
|
396
434
|
end
|
435
|
+
class_eval <<-XXDONE
|
436
|
+
def create_child_#{domain_class.to_s.downcase}(options=nil)
|
437
|
+
|
438
|
+
result=#{module_name+domain_class.to_s}.new(options)
|
439
|
+
result.copy_attributes(self.primary_key_hash)
|
440
|
+
result
|
441
|
+
end
|
442
|
+
XXDONE
|
397
443
|
# if options[:tracking]
|
398
444
|
# #add add_xxx method
|
399
445
|
# #add to list of tracker attributes
|
@@ -438,6 +484,14 @@ module SdbDal
|
|
438
484
|
end
|
439
485
|
return result[0] if result.length==1
|
440
486
|
return result
|
487
|
+
end
|
488
|
+
def primary_key_hash
|
489
|
+
|
490
|
+
result={}
|
491
|
+
self.class.primary_key_attribute_names.each do |key_part|
|
492
|
+
result[key_part]=@attribute_values[key_part ]
|
493
|
+
end
|
494
|
+
return result
|
441
495
|
end
|
442
496
|
def primary_key=(value)
|
443
497
|
key=DomainObject::arrayify(value)
|
@@ -672,6 +726,13 @@ module SdbDal
|
|
672
726
|
end
|
673
727
|
def find_single(id, options)
|
674
728
|
return nil if id==nil
|
729
|
+
if id.is_a?(Hash)
|
730
|
+
id_as_array=[]
|
731
|
+
@primary_key_attribute_names.each do |key_name|
|
732
|
+
id_as_array << id[key_name]
|
733
|
+
end
|
734
|
+
id=id_as_array
|
735
|
+
end
|
675
736
|
id=arrayify(id)
|
676
737
|
attributes=self.repository(options).find_one(self.table_name,id,attribute_descriptions)
|
677
738
|
if attributes && attributes.length>0
|
data/lib/sdb_dal/repository.rb
CHANGED
@@ -395,6 +395,7 @@ module SdbDal
|
|
395
395
|
parsed_attributes
|
396
396
|
end
|
397
397
|
def destroy(table_name, primary_key)
|
398
|
+
@session_cache.destroy(table_name,primary_key)
|
398
399
|
@sdb.delete_attributes(make_domain_name(table_name),make_cache_key(table_name, primary_key) )
|
399
400
|
# if @use_cache
|
400
401
|
# @storage.delete(@clob_bucket,make_cache_key(table_name,primary_key))
|