simple_record 1.4.15 → 1.4.18
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/simple_record/active_sdb.rb +82 -68
- data/lib/simple_record/attributes.rb +12 -0
- data/lib/simple_record/translations.rb +11 -0
- data/test/conversions_test.rb +44 -0
- data/test/simple_record_test.rb +1 -1
- metadata +5 -5
- data/test/conversions.rb +0 -16
|
@@ -263,7 +263,7 @@ module SimpleRecord
|
|
|
263
263
|
# See select(), original find with QUERY syntax is deprecated so now find and select are synonyms.
|
|
264
264
|
#
|
|
265
265
|
def find(*args)
|
|
266
|
-
|
|
266
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
267
267
|
case args.first
|
|
268
268
|
when nil then
|
|
269
269
|
raise "Invalid parameters passed to find: nil."
|
|
@@ -285,11 +285,11 @@ module SimpleRecord
|
|
|
285
285
|
# Same as find, but will return SimpleDB metadata like :request_id and :box_usage
|
|
286
286
|
#
|
|
287
287
|
def find_with_metadata(*args)
|
|
288
|
-
|
|
288
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
289
289
|
case args.first
|
|
290
290
|
when nil then
|
|
291
291
|
raise "Invalid parameters passed to find: nil."
|
|
292
|
-
when :all
|
|
292
|
+
when :all then
|
|
293
293
|
sql_select(options)
|
|
294
294
|
when :first then
|
|
295
295
|
sql_select(options.merge(:limit => 1))
|
|
@@ -363,23 +363,23 @@ module SimpleRecord
|
|
|
363
363
|
# Select
|
|
364
364
|
|
|
365
365
|
def select_from_ids(args, options) # :nodoc:
|
|
366
|
-
cond
|
|
366
|
+
cond = []
|
|
367
367
|
# detect amount of records requested
|
|
368
368
|
bunch_of_records_requested = args.size > 1 || args.first.is_a?(Array)
|
|
369
369
|
# flatten ids
|
|
370
|
-
args
|
|
370
|
+
args = args.to_a.flatten
|
|
371
371
|
args.each { |id| cond << "itemName() = #{self.connection.escape(id)}" }
|
|
372
|
-
ids_cond
|
|
372
|
+
ids_cond = "(#{cond.join(' OR ')})"
|
|
373
373
|
# user defined :conditions to string (if it was defined)
|
|
374
|
-
options[:conditions]
|
|
374
|
+
options[:conditions] = build_conditions(options[:conditions])
|
|
375
375
|
# join ids condition and user defined conditions
|
|
376
|
-
options[:conditions]
|
|
376
|
+
options[:conditions] = options[:conditions].blank? ? ids_cond : "(#{options[:conditions]}) AND #{ids_cond}"
|
|
377
377
|
#puts 'options=' + options.inspect
|
|
378
|
-
result
|
|
378
|
+
result = sql_select(options)
|
|
379
379
|
#puts 'select_from_ids result=' + result.inspect
|
|
380
380
|
# if one record was requested then return it
|
|
381
381
|
unless bunch_of_records_requested
|
|
382
|
-
record
|
|
382
|
+
record = result[:items].first
|
|
383
383
|
# railse if nothing was found
|
|
384
384
|
raise ActiveSdbError.new("Couldn't find #{name} with ID #{args}") unless record
|
|
385
385
|
result[:single] = record
|
|
@@ -388,7 +388,7 @@ module SimpleRecord
|
|
|
388
388
|
# and return as an array
|
|
389
389
|
unless args.size == result[:items].size
|
|
390
390
|
# todo: might make sense to return the array but with nil values in the slots where an item wasn't found?
|
|
391
|
-
id_list = args.map{|i| "'#{i}'"}.join(',')
|
|
391
|
+
id_list = args.map { |i| "'#{i}'" }.join(',')
|
|
392
392
|
raise ActiveSdbError.new("Couldn't find all #{name} with IDs (#{id_list}) (found #{result[:items].size} results, but was looking for #{args.size})")
|
|
393
393
|
else
|
|
394
394
|
result
|
|
@@ -398,37 +398,37 @@ module SimpleRecord
|
|
|
398
398
|
end
|
|
399
399
|
|
|
400
400
|
def sql_select(options) # :nodoc:
|
|
401
|
-
count
|
|
401
|
+
count = options[:count] || false
|
|
402
402
|
#puts 'count? ' + count.to_s
|
|
403
|
-
@next_token
|
|
404
|
-
@consistent_read
|
|
403
|
+
@next_token = options[:next_token]
|
|
404
|
+
@consistent_read = options[:consistent_read]
|
|
405
405
|
select_expression = build_select(options)
|
|
406
406
|
# request items
|
|
407
|
-
query_result
|
|
407
|
+
query_result = self.connection.select(select_expression, @next_token, @consistent_read)
|
|
408
408
|
# puts 'QR=' + query_result.inspect
|
|
409
|
-
ret
|
|
409
|
+
ret = {}
|
|
410
410
|
if count
|
|
411
411
|
ret[:count] = query_result.delete(:items)[0]["Domain"]["Count"][0].to_i
|
|
412
412
|
ret.merge!(query_result)
|
|
413
413
|
return ret
|
|
414
414
|
end
|
|
415
|
-
@next_token
|
|
416
|
-
items
|
|
415
|
+
@next_token = query_result[:next_token]
|
|
416
|
+
items = query_result.delete(:items).map do |hash|
|
|
417
417
|
id, attributes = hash.shift
|
|
418
|
-
new_item = self.new(
|
|
419
|
-
new_item.initialize_from_db(attributes.merge({
|
|
418
|
+
new_item = self.new()
|
|
419
|
+
new_item.initialize_from_db(attributes.merge({'id' => id}))
|
|
420
420
|
new_item.mark_as_old
|
|
421
421
|
new_item
|
|
422
422
|
end
|
|
423
|
-
ret[:items]
|
|
423
|
+
ret[:items] = items
|
|
424
424
|
ret.merge!(query_result)
|
|
425
425
|
ret
|
|
426
426
|
end
|
|
427
427
|
|
|
428
428
|
# select_by helpers
|
|
429
429
|
def select_all_by_(format_str, args, options) # :nodoc:
|
|
430
|
-
fields
|
|
431
|
-
conditions
|
|
430
|
+
fields = format_str.to_s.sub(/^select_(all_)?by_/, '').split('_and_')
|
|
431
|
+
conditions = fields.map { |field| "#{field}=?" }.join(' AND ')
|
|
432
432
|
options[:conditions] = [conditions, *args]
|
|
433
433
|
find(:all, options)
|
|
434
434
|
end
|
|
@@ -468,7 +468,7 @@ module SimpleRecord
|
|
|
468
468
|
# :sort_option nil | string "name desc|asc"
|
|
469
469
|
#
|
|
470
470
|
def query(options) # :nodoc:
|
|
471
|
-
@next_token
|
|
471
|
+
@next_token = options[:next_token]
|
|
472
472
|
@consistent_read = options[:consistent_read]
|
|
473
473
|
query_expression = build_conditions(options[:query_expression])
|
|
474
474
|
# add sort_options to the query_expression
|
|
@@ -477,7 +477,7 @@ module SimpleRecord
|
|
|
477
477
|
sort_query_expression = "['#{sort_by}' starts-with '']"
|
|
478
478
|
sort_by_expression = " sort '#{sort_by}' #{sort_order}"
|
|
479
479
|
# make query_expression to be a string (it may be null)
|
|
480
|
-
query_expression
|
|
480
|
+
query_expression = query_expression.to_s
|
|
481
481
|
# quote from Amazon:
|
|
482
482
|
# The sort attribute must be present in at least one of the predicates of the query expression.
|
|
483
483
|
if query_expression.blank?
|
|
@@ -485,12 +485,12 @@ module SimpleRecord
|
|
|
485
485
|
elsif !query_attributes(query_expression).include?(sort_by)
|
|
486
486
|
query_expression += " intersection #{sort_query_expression}"
|
|
487
487
|
end
|
|
488
|
-
query_expression
|
|
488
|
+
query_expression += sort_by_expression
|
|
489
489
|
end
|
|
490
490
|
# request items
|
|
491
|
-
query_result
|
|
492
|
-
@next_token
|
|
493
|
-
items
|
|
491
|
+
query_result = self.connection.query(domain, query_expression, options[:max_number_of_items], @next_token, @consistent_read)
|
|
492
|
+
@next_token = query_result[:next_token]
|
|
493
|
+
items = query_result[:items].map do |name|
|
|
494
494
|
new_item = self.new('id' => name)
|
|
495
495
|
new_item.mark_as_old
|
|
496
496
|
reload_if_exists(record) if options[:auto_load]
|
|
@@ -509,33 +509,33 @@ module SimpleRecord
|
|
|
509
509
|
end
|
|
510
510
|
|
|
511
511
|
def find_every(options) # :nodoc:
|
|
512
|
-
records = query(
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
512
|
+
records = query(:query_expression => options[:conditions],
|
|
513
|
+
:max_number_of_items => options[:limit],
|
|
514
|
+
:next_token => options[:next_token],
|
|
515
|
+
:sort_option => options[:sort] || options[:order],
|
|
516
|
+
:consistent_read => options[:consistent_read])
|
|
517
517
|
options[:auto_load] ? reload_all_records(records) : records
|
|
518
518
|
end
|
|
519
519
|
|
|
520
520
|
def find_initial(options) # :nodoc:
|
|
521
521
|
options[:limit] = 1
|
|
522
|
-
record
|
|
522
|
+
record = find_every(options).first
|
|
523
523
|
options[:auto_load] ? reload_all_records(record).first : record
|
|
524
524
|
end
|
|
525
525
|
|
|
526
526
|
def find_from_ids(args, options) # :nodoc:
|
|
527
|
-
cond
|
|
527
|
+
cond = []
|
|
528
528
|
# detect amount of records requested
|
|
529
529
|
bunch_of_records_requested = args.size > 1 || args.first.is_a?(Array)
|
|
530
530
|
# flatten ids
|
|
531
|
-
args
|
|
531
|
+
args = args.to_a.flatten
|
|
532
532
|
args.each { |id| cond << "'id'=#{self.connection.escape(id)}" }
|
|
533
|
-
ids_cond
|
|
533
|
+
ids_cond = "[#{cond.join(' OR ')}]"
|
|
534
534
|
# user defined :conditions to string (if it was defined)
|
|
535
|
-
options[:conditions]
|
|
535
|
+
options[:conditions] = build_conditions(options[:conditions])
|
|
536
536
|
# join ids condition and user defined conditions
|
|
537
|
-
options[:conditions]
|
|
538
|
-
result
|
|
537
|
+
options[:conditions] = options[:conditions].blank? ? ids_cond : "#{options[:conditions]} intersection #{ids_cond}"
|
|
538
|
+
result = find_every(options)
|
|
539
539
|
# if one record was requested then return it
|
|
540
540
|
unless bunch_of_records_requested
|
|
541
541
|
record = result.first
|
|
@@ -546,7 +546,7 @@ module SimpleRecord
|
|
|
546
546
|
# if a bunch of records was requested then return check that we found all of them
|
|
547
547
|
# and return as an array
|
|
548
548
|
unless args.size == result.size
|
|
549
|
-
id_list = args.map{|i| "'#{i}'"}.join(',')
|
|
549
|
+
id_list = args.map { |i| "'#{i}'" }.join(',')
|
|
550
550
|
raise ActiveSdbError.new("Couldn't find all #{name} with IDs (#{id_list}) (found #{result.size} results, but was looking for #{args.size})")
|
|
551
551
|
else
|
|
552
552
|
options[:auto_load] ? reload_all_records(result) : result
|
|
@@ -556,8 +556,8 @@ module SimpleRecord
|
|
|
556
556
|
|
|
557
557
|
# find_by helpers
|
|
558
558
|
def find_all_by_(format_str, args, options) # :nodoc:
|
|
559
|
-
fields
|
|
560
|
-
conditions
|
|
559
|
+
fields = format_str.to_s.sub(/^find_(all_)?by_/, '').split('_and_')
|
|
560
|
+
conditions = fields.map { |field| "['#{field}'=?]" }.join(' intersection ')
|
|
561
561
|
options[:conditions] = [conditions, *args]
|
|
562
562
|
find(:all, options)
|
|
563
563
|
end
|
|
@@ -580,7 +580,7 @@ module SimpleRecord
|
|
|
580
580
|
# puts 'CONVERTED ' + $1 + " to " + to_send_to
|
|
581
581
|
end
|
|
582
582
|
|
|
583
|
-
options
|
|
583
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
584
584
|
__send__(to_send_to, attributes, args, options)
|
|
585
585
|
else
|
|
586
586
|
super(method, *args)
|
|
@@ -588,17 +588,23 @@ module SimpleRecord
|
|
|
588
588
|
end
|
|
589
589
|
|
|
590
590
|
def build_select(options) # :nodoc:
|
|
591
|
-
select
|
|
592
|
-
select
|
|
591
|
+
select = options[:select] || '*'
|
|
592
|
+
select = options[:count] ? "count(*)" : select
|
|
593
593
|
#puts 'select=' + select.to_s
|
|
594
|
-
from
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
594
|
+
from = options[:from] || domain
|
|
595
|
+
condition_fields = parse_condition_fields(options[:conditions])
|
|
596
|
+
conditions = options[:conditions] ? " WHERE #{build_conditions(options[:conditions])}" : ''
|
|
597
|
+
order = options[:order] ? " ORDER BY #{options[:order]}" : ''
|
|
598
|
+
limit = options[:limit] ? " LIMIT #{options[:limit]}" : ''
|
|
598
599
|
# mix sort by argument (it must present in response)
|
|
599
600
|
unless order.blank?
|
|
600
601
|
sort_by, sort_order = sort_options(options[:order])
|
|
601
|
-
|
|
602
|
+
if condition_fields.nil? || !condition_fields.include?(sort_by)
|
|
603
|
+
conditions << (conditions.blank? ? " WHERE " : " AND ") << "(#{sort_by} IS NOT NULL)"
|
|
604
|
+
else
|
|
605
|
+
# puts 'skipping is not null on sort because already there.'
|
|
606
|
+
end
|
|
607
|
+
|
|
602
608
|
end
|
|
603
609
|
"SELECT #{select} FROM `#{from}`#{conditions}#{order}#{limit}"
|
|
604
610
|
end
|
|
@@ -607,13 +613,21 @@ module SimpleRecord
|
|
|
607
613
|
case
|
|
608
614
|
when conditions.is_a?(Array) then
|
|
609
615
|
connection.query_expression_from_array(conditions)
|
|
610
|
-
when conditions.is_a?(Hash)
|
|
616
|
+
when conditions.is_a?(Hash) then
|
|
611
617
|
connection.query_expression_from_hash(conditions)
|
|
612
618
|
else
|
|
613
619
|
conditions
|
|
614
620
|
end
|
|
615
621
|
end
|
|
616
622
|
|
|
623
|
+
# This will currently return and's, or's and betweens. Doesn't hurt anything, but could remove.
|
|
624
|
+
def parse_condition_fields(conditions)
|
|
625
|
+
return nil unless conditions && conditions.present?
|
|
626
|
+
rx = /\b(\w*)[\s|>=|<=|!=|=|>|<|like]/
|
|
627
|
+
fields = conditions[0].scan(rx)
|
|
628
|
+
# puts 'condition_fields = ' + fields.inspect
|
|
629
|
+
end
|
|
630
|
+
|
|
617
631
|
end
|
|
618
632
|
|
|
619
633
|
public
|
|
@@ -684,7 +698,7 @@ module SimpleRecord
|
|
|
684
698
|
# puts item.attributes.inspect #=> {"name"=>["Birds"], "id"=>"blah-blah", "toys"=>["seeds", "dogs tail"]}
|
|
685
699
|
#
|
|
686
700
|
def attributes=(attrs)
|
|
687
|
-
old_id
|
|
701
|
+
old_id = @attributes['id']
|
|
688
702
|
@attributes = uniq_values(attrs)
|
|
689
703
|
@attributes['id'] = old_id if @attributes['id'].blank? && !old_id.blank?
|
|
690
704
|
self.attributes
|
|
@@ -714,7 +728,7 @@ module SimpleRecord
|
|
|
714
728
|
# puts item['Cat'].inspect #=> ["Whiskas", "chicken"]
|
|
715
729
|
#
|
|
716
730
|
def []=(attribute, values)
|
|
717
|
-
attribute
|
|
731
|
+
attribute = attribute.to_s
|
|
718
732
|
@attributes[attribute] = attribute == 'id' ? values.to_s : values.is_a?(Array) ? values.uniq : [values]
|
|
719
733
|
|
|
720
734
|
end
|
|
@@ -726,8 +740,8 @@ module SimpleRecord
|
|
|
726
740
|
#
|
|
727
741
|
def reload
|
|
728
742
|
raise_on_id_absence
|
|
729
|
-
old_id
|
|
730
|
-
attrs
|
|
743
|
+
old_id = id
|
|
744
|
+
attrs = connection.get_attributes(domain, id)[:attributes]
|
|
731
745
|
@attributes = {}
|
|
732
746
|
unless attrs.blank?
|
|
733
747
|
attrs.each { |attribute, values| @attributes[attribute] = values }
|
|
@@ -749,12 +763,12 @@ module SimpleRecord
|
|
|
749
763
|
#
|
|
750
764
|
def reload_attributes(*attrs_list)
|
|
751
765
|
raise_on_id_absence
|
|
752
|
-
attrs_list = attrs_list.flatten.map{ |attribute| attribute.to_s }
|
|
766
|
+
attrs_list = attrs_list.flatten.map { |attribute| attribute.to_s }
|
|
753
767
|
attrs_list.delete('id')
|
|
754
|
-
result
|
|
768
|
+
result = {}
|
|
755
769
|
attrs_list.flatten.uniq.each do |attribute|
|
|
756
770
|
attribute = attribute.to_s
|
|
757
|
-
values
|
|
771
|
+
values = connection.get_attributes(domain, id, attribute)[:attributes][attribute]
|
|
758
772
|
unless values.blank?
|
|
759
773
|
@attributes[attribute] = result[attribute] = values
|
|
760
774
|
else
|
|
@@ -783,10 +797,10 @@ module SimpleRecord
|
|
|
783
797
|
def put
|
|
784
798
|
@attributes = uniq_values(@attributes)
|
|
785
799
|
prepare_for_update
|
|
786
|
-
attrs
|
|
800
|
+
attrs = @attributes.dup
|
|
787
801
|
attrs.delete('id')
|
|
788
802
|
connection.put_attributes(domain, id, attrs) unless attrs.blank?
|
|
789
|
-
connection.put_attributes(domain, id, {
|
|
803
|
+
connection.put_attributes(domain, id, {'id' => id}, :replace)
|
|
790
804
|
mark_as_old
|
|
791
805
|
@attributes
|
|
792
806
|
end
|
|
@@ -805,7 +819,7 @@ module SimpleRecord
|
|
|
805
819
|
attrs.delete('id')
|
|
806
820
|
# add new values to all attributes from list
|
|
807
821
|
connection.put_attributes(domain, id, attrs) unless attrs.blank?
|
|
808
|
-
connection.put_attributes(domain, id, {
|
|
822
|
+
connection.put_attributes(domain, id, {'id' => id}, :replace)
|
|
809
823
|
attrs.each do |attribute, values|
|
|
810
824
|
@attributes[attribute] ||= []
|
|
811
825
|
@attributes[attribute] += values
|
|
@@ -848,7 +862,7 @@ module SimpleRecord
|
|
|
848
862
|
dirty_atts = options[:dirty_atts]
|
|
849
863
|
atts_to_save.delete_if { |key, value| !dirty_atts.has_key?(key) }
|
|
850
864
|
end
|
|
851
|
-
dom
|
|
865
|
+
dom = options[:domain] || domain
|
|
852
866
|
#puts 'atts_to_save2=' + atts_to_save.inspect
|
|
853
867
|
connection.put_attributes(dom, id, atts_to_save, :replace)
|
|
854
868
|
apres_save2
|
|
@@ -925,7 +939,7 @@ module SimpleRecord
|
|
|
925
939
|
#
|
|
926
940
|
def delete_attributes(*attrs_list)
|
|
927
941
|
raise_on_id_absence
|
|
928
|
-
attrs_list = attrs_list.flatten.map{ |attribute| attribute.to_s }
|
|
942
|
+
attrs_list = attrs_list.flatten.map { |attribute| attribute.to_s }
|
|
929
943
|
attrs_list.delete('id')
|
|
930
944
|
unless attrs_list.blank?
|
|
931
945
|
connection.delete_attributes(domain, id, attrs_list)
|
|
@@ -958,7 +972,7 @@ module SimpleRecord
|
|
|
958
972
|
@new_record
|
|
959
973
|
end
|
|
960
974
|
|
|
961
|
-
def mark_as_old
|
|
975
|
+
def mark_as_old # :nodoc:
|
|
962
976
|
@new_record = false
|
|
963
977
|
end
|
|
964
978
|
|
|
@@ -975,8 +989,8 @@ module SimpleRecord
|
|
|
975
989
|
def uniq_values(attributes=nil) # :nodoc:
|
|
976
990
|
attrs = {}
|
|
977
991
|
attributes.each do |attribute, values|
|
|
978
|
-
attribute
|
|
979
|
-
newval
|
|
992
|
+
attribute = attribute.to_s
|
|
993
|
+
newval = attribute == 'id' ? values.to_s : values.is_a?(Array) ? values.uniq : [values]
|
|
980
994
|
attrs[attribute] = newval
|
|
981
995
|
if newval.blank?
|
|
982
996
|
# puts "VALUE IS BLANK " + attribute.to_s + " val=" + values.inspect
|
|
@@ -86,6 +86,11 @@ module SimpleRecord
|
|
|
86
86
|
are_ints(*args)
|
|
87
87
|
end
|
|
88
88
|
|
|
89
|
+
def has_floats(*args)
|
|
90
|
+
has_attributes(*args)
|
|
91
|
+
are_floats(*args)
|
|
92
|
+
end
|
|
93
|
+
|
|
89
94
|
def has_dates(*args)
|
|
90
95
|
has_attributes(*args)
|
|
91
96
|
are_dates(*args)
|
|
@@ -103,6 +108,13 @@ module SimpleRecord
|
|
|
103
108
|
end
|
|
104
109
|
end
|
|
105
110
|
|
|
111
|
+
def are_floats(*args)
|
|
112
|
+
# puts 'calling are_ints: ' + args.inspect
|
|
113
|
+
args.each do |arg|
|
|
114
|
+
defined_attributes[arg].type = :float
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
106
118
|
def are_dates(*args)
|
|
107
119
|
args.each do |arg|
|
|
108
120
|
defined_attributes[arg].type = :date
|
|
@@ -131,11 +131,22 @@ module SimpleRecord
|
|
|
131
131
|
|
|
132
132
|
end
|
|
133
133
|
return x_str
|
|
134
|
+
elsif x.is_a? Float
|
|
135
|
+
from_float(x)
|
|
134
136
|
else
|
|
135
137
|
return x
|
|
136
138
|
end
|
|
137
139
|
end
|
|
138
140
|
|
|
141
|
+
# This conversion to a string is based on: http://tools.ietf.org/html/draft-wood-ldapext-float-00
|
|
142
|
+
# Java code sample is here: http://code.google.com/p/typica/source/browse/trunk/java/com/xerox/amazonws/simpledb/DataUtils.java
|
|
143
|
+
def self.from_float(x)
|
|
144
|
+
return x
|
|
145
|
+
# if x == 0.0
|
|
146
|
+
# return "3 000 0.0000000000000000"
|
|
147
|
+
# end
|
|
148
|
+
end
|
|
149
|
+
|
|
139
150
|
|
|
140
151
|
def wrap_if_required(arg, value, sdb_val)
|
|
141
152
|
return nil if value.nil?
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "yaml"
|
|
2
|
+
require 'aws'
|
|
3
|
+
|
|
4
|
+
require_relative "test_base"
|
|
5
|
+
require_relative "../lib/simple_record"
|
|
6
|
+
require_relative 'my_model'
|
|
7
|
+
require_relative 'my_child_model'
|
|
8
|
+
|
|
9
|
+
class ConversionsTest < TestBase
|
|
10
|
+
|
|
11
|
+
def test_ints
|
|
12
|
+
x = 0
|
|
13
|
+
puts SimpleRecord::Translations.pad_and_offset(x)
|
|
14
|
+
assert_equal "09223372036854775808", SimpleRecord::Translations.pad_and_offset(x)
|
|
15
|
+
|
|
16
|
+
x = 1
|
|
17
|
+
puts SimpleRecord::Translations.pad_and_offset(x)
|
|
18
|
+
assert_equal "09223372036854775809", SimpleRecord::Translations.pad_and_offset(x)
|
|
19
|
+
|
|
20
|
+
x = "09223372036854775838"
|
|
21
|
+
puts SimpleRecord::Translations.un_offset_int(x)
|
|
22
|
+
assert_equal 30, SimpleRecord::Translations.un_offset_int(x)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# All from examples here: http://tools.ietf.org/html/draft-wood-ldapext-float-00
|
|
26
|
+
def test_floats
|
|
27
|
+
zero = "3 000 0.0000000000000000"
|
|
28
|
+
assert_equal zero, SimpleRecord::Translations.pad_and_offset(0.0)
|
|
29
|
+
|
|
30
|
+
puts 'induced = ' + "3.25e5".to_f.to_s
|
|
31
|
+
|
|
32
|
+
assert_equal "5 005 3.2500000000000000", SimpleRecord::Translations.pad_and_offset("3.25e5".to_f)
|
|
33
|
+
assert_equal "4 994 8.4000000000000000", SimpleRecord::Translations.pad_and_offset("8.4e-5".to_f)
|
|
34
|
+
assert_equal "4 992 8.4000000000000000", SimpleRecord::Translations.pad_and_offset("8.4e-7".to_f)
|
|
35
|
+
assert_equal "3 000 0.0000000000000000", SimpleRecord::Translations.pad_and_offset("0.0e0".to_f)
|
|
36
|
+
assert_equal "2 004 5.7500000000000000", SimpleRecord::Translations.pad_and_offset("-4.25e-4".to_f)
|
|
37
|
+
assert_equal "2 004 3.6500000000000000", SimpleRecord::Translations.pad_and_offset("-6.35e-4".to_f)
|
|
38
|
+
assert_equal "2 003 3.6500000000000000", SimpleRecord::Translations.pad_and_offset("-6.35e-3".to_f)
|
|
39
|
+
assert_equal "1 895 6.0000000000000000", SimpleRecord::Translations.pad_and_offset("-4.0e105".to_f)
|
|
40
|
+
assert_equal "1 894 6.0000000000000000", SimpleRecord::Translations.pad_and_offset("-4.0e105".to_f)
|
|
41
|
+
assert_equal "1 894 4.0000000000000000", SimpleRecord::Translations.pad_and_offset("-6.0e105".to_f)
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
data/test/simple_record_test.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'test/unit'
|
|
2
2
|
require File.join(File.dirname(__FILE__), "/../lib/simple_record")
|
|
3
3
|
require File.join(File.dirname(__FILE__), "./test_helpers")
|
|
4
|
-
|
|
4
|
+
require_relative "test_base"
|
|
5
5
|
require "yaml"
|
|
6
6
|
require 'aws'
|
|
7
7
|
require_relative 'my_model'
|
metadata
CHANGED
|
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
|
5
5
|
segments:
|
|
6
6
|
- 1
|
|
7
7
|
- 4
|
|
8
|
-
-
|
|
9
|
-
version: 1.4.
|
|
8
|
+
- 18
|
|
9
|
+
version: 1.4.18
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Travis Reeder
|
|
@@ -16,7 +16,7 @@ autorequire:
|
|
|
16
16
|
bindir: bin
|
|
17
17
|
cert_chain: []
|
|
18
18
|
|
|
19
|
-
date: 2010-10-
|
|
19
|
+
date: 2010-10-24 00:00:00 -07:00
|
|
20
20
|
default_executable:
|
|
21
21
|
dependencies:
|
|
22
22
|
- !ruby/object:Gem::Dependency
|
|
@@ -55,7 +55,7 @@ files:
|
|
|
55
55
|
- lib/simple_record/stats.rb
|
|
56
56
|
- lib/simple_record/translations.rb
|
|
57
57
|
- README.markdown
|
|
58
|
-
- test/
|
|
58
|
+
- test/conversions_test.rb
|
|
59
59
|
- test/model_with_enc.rb
|
|
60
60
|
- test/my_base_model.rb
|
|
61
61
|
- test/my_child_model.rb
|
|
@@ -108,7 +108,7 @@ signing_key:
|
|
|
108
108
|
specification_version: 3
|
|
109
109
|
summary: Drop in replacement for ActiveRecord to Amazon SimpleDB instead.
|
|
110
110
|
test_files:
|
|
111
|
-
- test/
|
|
111
|
+
- test/conversions_test.rb
|
|
112
112
|
- test/model_with_enc.rb
|
|
113
113
|
- test/my_base_model.rb
|
|
114
114
|
- test/my_child_model.rb
|
data/test/conversions.rb
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
|
|
4
|
-
require "yaml"
|
|
5
|
-
require 'right_aws'
|
|
6
|
-
require 'my_model'
|
|
7
|
-
require 'my_child_model'
|
|
8
|
-
|
|
9
|
-
x = 0
|
|
10
|
-
puts SimpleRecord::Base.pad_and_offset(x)
|
|
11
|
-
|
|
12
|
-
x = 1
|
|
13
|
-
puts SimpleRecord::Base.pad_and_offset(x)
|
|
14
|
-
|
|
15
|
-
x = "09223372036854775838"
|
|
16
|
-
puts SimpleRecord::Base.un_offset_int(x)
|