Skip to content

Commit

Permalink
add dynamic attribute accessors for ActiveSDB objects (by rubysolo)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin committed Mar 31, 2010
1 parent 4dc7b46 commit 0ad3fbe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
17 changes: 15 additions & 2 deletions lib/sdb/active_sdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,21 @@ def mark_as_old # :nodoc:
@new_record = false
end

private

# support accessing attribute values via method call
def method_missing(method_sym, *args)
method_name = method_sym.to_s
setter = method_name[-1,1] == '='
method_name.chop! if setter

if @attributes.has_key? method_name
setter ? self[method_name] = args.first : self[method_name]
else
super
end
end

private

def raise_on_id_absence
raise ActiveSdbError.new('Unknown record id') unless id
end
Expand Down
21 changes: 16 additions & 5 deletions test/sdb/test_active_sdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,23 @@ def test_13_delete
wait SDB_DELAY, 'test 11: after delete item'
assert_nil Client.find_by_name('Putin')
end

def test_14_delete_domain

def test_14_dynamic_attribute_accessors
bush = Client.find_by_name('Bush')
bush.reload
assert_nothing_raised {
assert_equal ['male'], bush.gender
bush.name = 'George'
assert_equal ['George'], bush.name
}
assert_raise(NoMethodError) { bush.flarble }
end

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

Expand Down

0 comments on commit 0ad3fbe

Please sign in to comment.