Skip to content

Commit

Permalink
RightAws: ActiveSdb: simple typecasting support. naïve, actually. (by…
Browse files Browse the repository at this point in the history
… rubysolo)
  • Loading branch information
Konstantin committed Mar 31, 2010
1 parent 7483d6e commit b5e7ed8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
71 changes: 68 additions & 3 deletions lib/sdb/active_sdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ def column?(col_name)
columns.include?(col_name)
end

def type_of(col_name)
columns.type_of(col_name)
end

def serialize(attribute, value)
s = serialization_for_type(type_of(attribute))
s ? s.serialize(value) : value.to_s
end

def deserialize(attribute, value)
s = serialization_for_type(type_of(attribute))
s ? s.deserialize(value) : value
end

# Perform a find request.
#
# Single record:
Expand Down Expand Up @@ -604,6 +618,13 @@ def build_conditions(conditions) # :nodoc:
end
end

def serialization_for_type(type)
@serializations ||= {}
unless @serializations.has_key? type
@serializations[type] = ::RightAws::ActiveSdb.const_get("#{type}Serialization") rescue false
end
@serializations[type]
end
end

public
Expand Down Expand Up @@ -690,7 +711,7 @@ def domain
#
def [](attribute)
raw = @attributes[attribute.to_s]
self.class.column?(attribute) ? raw.first : raw # TODO : deserialization procs per data type
self.class.column?(attribute) ? self.class.deserialize(attribute, raw.first) : raw
end

# Updates the attribute identified by +attribute+ with the specified +values+.
Expand All @@ -705,7 +726,7 @@ def []=(attribute, values)
when attribute == 'id'
values.to_s
when self.class.column?(attribute)
values.to_s # TODO : serialization procs per data type
self.class.serialize(attribute, values)
else
Array(values).uniq
end
Expand Down Expand Up @@ -955,7 +976,14 @@ def uniq_values(attributes=nil) # :nodoc:
attrs = {}
attributes.each do |attribute, values|
attribute = attribute.to_s
attrs[attribute] = attribute == 'id' ? values.to_s : Array(values).uniq
attrs[attribute] = case
when attribute == 'id'
values.to_s
when self.class.column?(attribute)
values.is_a?(String) ? values : self.class.serialize(attribute, values)
else
Array(values).uniq
end
attrs.delete(attribute) if values.blank?
end
attrs
Expand Down Expand Up @@ -987,5 +1015,42 @@ def method_missing(method_sym, *args)
@columns[method_sym.to_s] = options.merge( :type => data_type )
end
end

class DateTimeSerialization
class << self
def serialize(date)
date.strftime('%Y-%m-%dT%H:%M:%S%z')
end

def deserialize(string)
r = DateTime.parse(string) rescue nil
end
end
end

class BooleanSerialization
class << self
def serialize(boolean)
boolean ? 'T' : 'F'
end

def deserialize(string)
string == 'T'
end
end
end

class IntegerSerialization
class << self
def serialize(int)
int.to_s
end

def deserialize(string)
string.to_i
end
end
end

end
end
20 changes: 14 additions & 6 deletions test/sdb/test_active_sdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Person < RightAws::ActiveSdb::Base
columns do
name
email
score :Integer
is_active :Boolean
registered_at :DateTime
created_at :DateTime, :default => lambda{ Time.now }
end
Expand All @@ -30,11 +32,11 @@ def setup
{ 'name' => 'Sandy', 'country' => 'Russia', 'gender' => 'female', 'hobby' => ['flowers', 'cats', 'cooking'] },
{ 'name' => 'Mary', 'country' => 'Russia', 'gender' => 'female', 'hobby' => ['flowers', 'cats', 'cooking'] } ]
@people = [
{ :name => 'Yetta E. Andrews', :email => 'nulla.facilisis@metus.com', :registered_at => Time.local(2000, 1, 1) },
{ :name => 'Sybill O. Olson', :email => 'nisi.Aenean.eget@urna.com', :registered_at => Time.local(2008, 7, 6) },
{ :name => 'Isabelle K. Flynn', :email => 'velit@amet.com', :registered_at => Time.local(2003, 5, 20) },
{ :name => 'Juliet H. Witt', :email => 'egestas@pretiumaliquet.ca', :registered_at => Time.local(2007, 2, 28) },
{ :name => 'Lucy N. Christensen', :email => 'lacus.v12@stu.edu', :registered_at => Time.local(2005, 10, 26) }
{ :name => 'Yetta E. Andrews', :email => 'nulla.facilisis@metus.com', :score => 100, :is_active => true, :registered_at => Time.local(2000, 1, 1) },
{ :name => 'Sybill O. Olson', :email => 'nisi.Aenean.eget@urna.com', :score => 87, :is_active => true, :registered_at => Time.local(2008, 7, 6) },
{ :name => 'Isabelle K. Flynn', :email => 'velit@amet.com', :score => 98, :is_active => false, :registered_at => Time.local(2003, 5, 20) },
{ :name => 'Juliet H. Witt', :email => 'egestas@pretiumaliquet.ca', :score => 72, :is_active => true, :registered_at => Time.local(2007, 2, 28) },
{ :name => 'Lucy N. Christensen', :email => 'lacus.v12@stu.edu', :score => 94, :is_active => false, :registered_at => Time.local(2005, 10, 26) }
]
RightAws::ActiveSdb.establish_connection(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
end
Expand Down Expand Up @@ -325,11 +327,17 @@ def test_15_column_emulation
Person.create person
end
wait SDB_DELAY, 'test 15: after people creation'

person = Person.find_by_email 'nulla.facilisis@metus.com'
person.reload

assert_equal 'Yetta E. Andrews', person.name
assert_equal DateTime, person.registered_at.class
assert person['registered_at'].is_a?(DateTime)
assert person[:registered_at].is_a?(DateTime)

assert person.is_active

assert_equal 100, person.score
end

def test_999_delete_domain
Expand Down

0 comments on commit b5e7ed8

Please sign in to comment.