Skip to content

Commit

Permalink
RightAws: ActiveSdb: make explicit columns return a single value (by …
Browse files Browse the repository at this point in the history
…rubysolo)
  • Loading branch information
Konstantin committed Mar 31, 2010
1 parent 0ad3fbe commit 7483d6e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
50 changes: 47 additions & 3 deletions lib/sdb/active_sdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,17 @@ def create_domain
def delete_domain
connection.delete_domain(domain)
end


def columns(&block)
@columns ||= ColumnSet.new
@columns.instance_eval(&block) if block
@columns
end

def column?(col_name)
columns.include?(col_name)
end

# Perform a find request.
#
# Single record:
Expand Down Expand Up @@ -679,7 +689,8 @@ def domain
# puts item['Cat'].inspect #=> ["Jons socks", "clew", "mice"]
#
def [](attribute)
@attributes[attribute.to_s]
raw = @attributes[attribute.to_s]
self.class.column?(attribute) ? raw.first : raw # TODO : deserialization procs per data type
end

# Updates the attribute identified by +attribute+ with the specified +values+.
Expand All @@ -690,7 +701,14 @@ def [](attribute)
#
def []=(attribute, values)
attribute = attribute.to_s
@attributes[attribute] = attribute == 'id' ? values.to_s : Array(values).uniq
@attributes[attribute] = case
when attribute == 'id'
values.to_s
when self.class.column?(attribute)
values.to_s # TODO : serialization procs per data type
else
Array(values).uniq
end
end

# Reload attributes from SDB. Replaces in-memory attributes.
Expand Down Expand Up @@ -943,5 +961,31 @@ def uniq_values(attributes=nil) # :nodoc:
attrs
end
end

class ColumnSet
attr_accessor :columns
def initialize
@columns = {}
end

def column(col_name)
@columns[col_name.to_s]
end
alias_method :include?, :column

def type_of(col_name)
column(col_name) && column(col_name)[:type]
end

def default(col_name)
column(col_name) && column(col_name)[:default]
end

def method_missing(method_sym, *args)
data_type = args.shift || :String
options = args.shift || {}
@columns[method_sym.to_s] = options.merge( :type => data_type )
end
end
end
end
48 changes: 41 additions & 7 deletions test/sdb/test_active_sdb.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
require File.dirname(__FILE__) + '/test_helper.rb'

class TestSdb < Test::Unit::TestCase

DOMAIN_NAME = 'right_sdb_awesome_test_domain'

DOMAIN_PREFIX = 'right_sdb_awesome_test'
CLIENT_DOMAIN = "#{DOMAIN_PREFIX}_client"
PERSON_DOMAIN = "#{DOMAIN_PREFIX}_person"

class Client < RightAws::ActiveSdb::Base
set_domain_name DOMAIN_NAME
set_domain_name CLIENT_DOMAIN
end

class Person < RightAws::ActiveSdb::Base
set_domain_name PERSON_DOMAIN

columns do
name
email
registered_at :DateTime
created_at :DateTime, :default => lambda{ Time.now }
end
end

def setup
Expand All @@ -17,6 +29,13 @@ def setup
{ 'name' => 'Mary', 'country' => 'USA', 'gender' => 'female', 'hobby' => ['patchwork', 'bundle jumping'] },
{ '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) }
]
RightAws::ActiveSdb.establish_connection(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
end

Expand All @@ -37,18 +56,20 @@ def wait(delay, msg='')
#---------------------------

def test_00_delete_domain
assert RightAws::ActiveSdb.delete_domain(DOMAIN_NAME)
assert RightAws::ActiveSdb.delete_domain(CLIENT_DOMAIN)
assert RightAws::ActiveSdb.delete_domain(PERSON_DOMAIN)
wait SDB_DELAY, 'test 00: after domain deletion'
end

def test_01_create_domain
# check that domain does not exist
assert !RightAws::ActiveSdb.domains.include?(DOMAIN_NAME)
assert !RightAws::ActiveSdb.domains.include?(CLIENT_DOMAIN)
# create domain
assert Client.create_domain
assert Person.create_domain
wait SDB_DELAY, 'test 01: after domain creation'
# check that we have received new domain from Amazin
assert RightAws::ActiveSdb.domains.include?(DOMAIN_NAME)
assert RightAws::ActiveSdb.domains.include?(CLIENT_DOMAIN)
end

def test_02_create_items
Expand Down Expand Up @@ -299,8 +320,21 @@ def test_14_dynamic_attribute_accessors
assert_raise(NoMethodError) { bush.flarble }
end

def test_15_column_emulation
@people.each do |person|
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
end

def test_999_delete_domain
assert Client.delete_domain
assert Person.delete_domain
wait SDB_DELAY, 'test 999: after delete domain'
assert_raise(Rightscale::AwsError) do
Client.find :all
Expand Down

0 comments on commit 7483d6e

Please sign in to comment.