2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-02-10 00:12:31 +01:00
|
|
|
|
|
|
|
module AttributeAliasing
|
|
|
|
class Content
|
2007-12-21 08:48:59 +01:00
|
|
|
attr_accessor :title, :Data
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@title, @Data = nil, nil
|
|
|
|
end
|
|
|
|
|
2007-02-10 00:12:31 +01:00
|
|
|
def title?
|
|
|
|
!title.nil?
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def Data?
|
|
|
|
!self.Data.nil?
|
|
|
|
end
|
2007-02-10 00:12:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
class Email < Content
|
|
|
|
alias_attribute :subject, :title
|
2007-12-21 08:48:59 +01:00
|
|
|
alias_attribute :body, :Data
|
2007-02-10 00:12:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AttributeAliasingTest < Test::Unit::TestCase
|
|
|
|
def test_attribute_alias
|
|
|
|
e = AttributeAliasing::Email.new
|
|
|
|
|
|
|
|
assert !e.subject?
|
|
|
|
|
|
|
|
e.title = "Upgrade computer"
|
|
|
|
assert_equal "Upgrade computer", e.subject
|
|
|
|
assert e.subject?
|
|
|
|
|
|
|
|
e.subject = "We got a long way to go"
|
|
|
|
assert_equal "We got a long way to go", e.title
|
|
|
|
assert e.title?
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def test_aliasing_to_uppercase_attributes
|
|
|
|
# Although it's very un-Ruby, some people's AR-mapped tables have
|
|
|
|
# upper-case attributes, and when people want to alias those names
|
|
|
|
# to more sensible ones, everything goes *foof*.
|
|
|
|
e = AttributeAliasing::Email.new
|
|
|
|
|
|
|
|
assert !e.body?
|
|
|
|
assert !e.Data?
|
|
|
|
|
|
|
|
e.body = "No, really, this is not a joke."
|
|
|
|
assert_equal "No, really, this is not a joke.", e.Data
|
|
|
|
assert e.Data?
|
|
|
|
|
|
|
|
e.Data = "Uppercased methods are teh suck"
|
|
|
|
assert_equal "Uppercased methods are teh suck", e.body
|
|
|
|
assert e.body?
|
|
|
|
end
|
2007-02-10 00:12:31 +01:00
|
|
|
end
|