Added better defaults for composed_of, so statements like composed_of...

Added better defaults for composed_of, so statements like composed_of :time_zone, :mapping => %w( time_zone time_zone ) can be written without the mapping part (it's now assumed)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@821 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 caf8976c
*SVN*
* Added better defaults for composed_of, so statements like composed_of :time_zone, :mapping => %w( time_zone time_zone ) can be written without the mapping part (it's now assumed)
* Added MacroReflection#macro which will return a symbol describing the macro used (like :composed_of or :has_many) #718, #248 [james@slashetc.com]
......
......@@ -118,12 +118,13 @@ module ClassMethods
# composed_of :temperature, :mapping => %w(reading celsius)
# composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
# composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
# composed_of :gps_location
def composed_of(part_id, options = {})
validate_options([ :class_name, :mapping ], options.keys)
name = part_id.id2name
class_name = options[:class_name] || name_to_class_name(name)
mapping = options[:mapping]
mapping = options[:mapping] || [ name, name ]
reader_method(name, class_name, mapping)
writer_method(name, class_name, mapping)
......
......@@ -2,10 +2,7 @@
require 'fixtures/customer'
class AggregationsTest < Test::Unit::TestCase
def setup
@customers = create_fixtures "customers"
@david = Customer.find(1)
end
fixtures :customers
def test_find_single_value_object
assert_equal 50, @david.balance.amount
......@@ -30,4 +27,21 @@ def test_immutable_value_objects
@david.balance = Money.new(100)
assert_raises(TypeError) { @david.balance.instance_eval { @amount = 20 } }
end
def test_inferred_mapping
assert_equal "35.544623640962634", @david.gps_location.latitude
assert_equal "-105.9309951055148", @david.gps_location.longitude
@david.gps_location = GpsLocation.new("39x-110")
assert_equal "39", @david.gps_location.latitude
assert_equal "-110", @david.gps_location.longitude
@david.save
@david.reload
assert_equal "39", @david.gps_location.latitude
assert_equal "-110", @david.gps_location.longitude
end
end
\ No newline at end of file
class Customer < ActiveRecord::Base
composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ]
composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
composed_of :gps_location
end
class Address
......@@ -27,4 +28,20 @@ def initialize(amount, currency = "USD")
def exchange_to(other_currency)
Money.new((amount * EXCHANGE_RATES["#{currency}_TO_#{other_currency}"]).floor, other_currency)
end
end
class GpsLocation
attr_reader :gps_location
def initialize(gps_location)
@gps_location = gps_location
end
def latitude
gps_location.split("x").first
end
def longitude
gps_location.split("x").last
end
end
\ No newline at end of file
......@@ -5,3 +5,4 @@ david:
address_street: Funny Street
address_city: Scary Town
address_country: Loony Land
gps_location: 35.544623640962634x-105.9309951055148
......@@ -58,6 +58,7 @@ CREATE TABLE customers (
address_street varchar(100) default NULL,
address_city varchar(100) default NULL,
address_country varchar(100) default NULL,
gps_location varchar(100) default NULL,
PRIMARY KEY (id)
);
......
......@@ -59,6 +59,7 @@ CREATE TABLE `customers` (
`address_street` varchar(100) default NULL,
`address_city` varchar(100) default NULL,
`address_country` varchar(100) default NULL,
`gps_location` varchar(100) default NULL,
PRIMARY KEY (`id`)
);
......
......@@ -83,6 +83,7 @@ create table customers (
address_street varchar(100) default null,
address_city varchar(100) default null,
address_country varchar(100) default null,
gps_location varchar(100) default null,
primary key (id)
);
......
......@@ -65,6 +65,7 @@ CREATE TABLE customers (
address_street character varying,
address_city character varying,
address_country character varying,
gps_location character varying,
PRIMARY KEY (id)
);
SELECT setval('customers_id_seq', 100);
......
......@@ -53,7 +53,8 @@ CREATE TABLE 'customers' (
'balance' INTEGER DEFAULT 0,
'address_street' TEXT DEFAULT NULL,
'address_city' TEXT DEFAULT NULL,
'address_country' TEXT DEFAULT NULL
'address_country' TEXT DEFAULT NULL,
'gps_location' TEXT DEFAULT NULL
);
CREATE TABLE 'movies' (
......
......@@ -57,6 +57,7 @@ CREATE TABLE customers (
address_street varchar(100) default NULL,
address_city varchar(100) default NULL,
address_country varchar(100) default NULL,
gps_location varchar(100) default NULL,
PRIMARY KEY (id)
);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册