public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
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
dhh (author)
Tue Mar 01 15:52:36 -0800 2005
commit  02ba03509c6a0f3e0fde99f0172a0125c83e43ce
tree    e2e66a47e24660eb763dc9056e9276c90f6af9b2
parent  caf8976ce786a1d6dca69a448da394dbddd53f12
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* 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)
0
+
0
 * Added MacroReflection#macro which will return a symbol describing the macro used (like :composed_of or :has_many) #718, #248 [james@slashetc.com]
0
 
0
 
...
118
119
120
 
121
122
123
124
125
126
 
127
128
129
...
118
119
120
121
122
123
124
125
126
 
127
128
129
130
0
@@ -118,12 +118,13 @@ module ActiveRecord
0
       # composed_of :temperature, :mapping => %w(reading celsius)
0
       # composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
0
       # composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
0
+ # composed_of :gps_location
0
       def composed_of(part_id, options = {})
0
         validate_options([ :class_name, :mapping ], options.keys)
0
 
0
         name = part_id.id2name
0
         class_name = options[:class_name] || name_to_class_name(name)
0
- mapping = options[:mapping]
0
+ mapping = options[:mapping] || [ name, name ]
0
 
0
         reader_method(name, class_name, mapping)
0
         writer_method(name, class_name, mapping)
...
2
3
4
5
6
7
8
 
9
10
11
...
30
31
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
34
...
2
3
4
 
 
 
 
5
6
7
8
...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -2,10 +2,7 @@ require 'abstract_unit'
0
 require 'fixtures/customer'
0
 
0
 class AggregationsTest < Test::Unit::TestCase
0
- def setup
0
- @customers = create_fixtures "customers"
0
- @david = Customer.find(1)
0
- end
0
+ fixtures :customers
0
 
0
   def test_find_single_value_object
0
     assert_equal 50, @david.balance.amount
0
@@ -30,4 +27,21 @@ class AggregationsTest < Test::Unit::TestCase
0
     @david.balance = Money.new(100)
0
     assert_raises(TypeError) { @david.balance.instance_eval { @amount = 20 } }
0
   end
0
+
0
+ def test_inferred_mapping
0
+ assert_equal "35.544623640962634", @david.gps_location.latitude
0
+ assert_equal "-105.9309951055148", @david.gps_location.longitude
0
+
0
+ @david.gps_location = GpsLocation.new("39x-110")
0
+
0
+ assert_equal "39", @david.gps_location.latitude
0
+ assert_equal "-110", @david.gps_location.longitude
0
+
0
+ @david.save
0
+
0
+ @david.reload
0
+
0
+ assert_equal "39", @david.gps_location.latitude
0
+ assert_equal "-110", @david.gps_location.longitude
0
+ end
0
 end
0
\ No newline at end of file
...
1
2
3
 
4
5
6
...
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
...
1
2
3
4
5
6
7
...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -1,6 +1,7 @@
0
 class Customer < ActiveRecord::Base
0
   composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ]
0
   composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
0
+ composed_of :gps_location
0
 end
0
 
0
 class Address
0
@@ -27,4 +28,20 @@ class Money
0
   def exchange_to(other_currency)
0
     Money.new((amount * EXCHANGE_RATES["#{currency}_TO_#{other_currency}"]).floor, other_currency)
0
   end
0
+end
0
+
0
+class GpsLocation
0
+ attr_reader :gps_location
0
+
0
+ def initialize(gps_location)
0
+ @gps_location = gps_location
0
+ end
0
+
0
+ def latitude
0
+ gps_location.split("x").first
0
+ end
0
+
0
+ def longitude
0
+ gps_location.split("x").last
0
+ end
0
 end
0
\ No newline at end of file
...
5
6
7
 
...
5
6
7
8
0
@@ -5,3 +5,4 @@ david:
0
   address_street: Funny Street
0
   address_city: Scary Town
0
   address_country: Loony Land
0
+ gps_location: 35.544623640962634x-105.9309951055148
...
58
59
60
 
61
62
63
...
58
59
60
61
62
63
64
0
@@ -58,6 +58,7 @@ CREATE TABLE customers (
0
   address_street varchar(100) default NULL,
0
   address_city varchar(100) default NULL,
0
   address_country varchar(100) default NULL,
0
+ gps_location varchar(100) default NULL,
0
   PRIMARY KEY (id)
0
 );
0
 
...
59
60
61
 
62
63
64
...
59
60
61
62
63
64
65
0
@@ -59,6 +59,7 @@ CREATE TABLE `customers` (
0
   `address_street` varchar(100) default NULL,
0
   `address_city` varchar(100) default NULL,
0
   `address_country` varchar(100) default NULL,
0
+ `gps_location` varchar(100) default NULL,
0
   PRIMARY KEY (`id`)
0
 );
0
 
...
83
84
85
 
86
87
88
...
83
84
85
86
87
88
89
0
@@ -83,6 +83,7 @@ create table customers (
0
     address_street varchar(100) default null,
0
     address_city varchar(100) default null,
0
     address_country varchar(100) default null,
0
+ gps_location varchar(100) default null,
0
     primary key (id)
0
 );
0
 
...
65
66
67
 
68
69
70
...
65
66
67
68
69
70
71
0
@@ -65,6 +65,7 @@ CREATE TABLE customers (
0
     address_street character varying,
0
     address_city character varying,
0
     address_country character varying,
0
+ gps_location character varying,
0
     PRIMARY KEY (id)
0
 );
0
 SELECT setval('customers_id_seq', 100);
...
53
54
55
56
 
 
57
58
59
...
53
54
55
 
56
57
58
59
60
0
@@ -53,7 +53,8 @@ CREATE TABLE 'customers' (
0
   'balance' INTEGER DEFAULT 0,
0
   'address_street' TEXT DEFAULT NULL,
0
   'address_city' TEXT DEFAULT NULL,
0
- 'address_country' TEXT DEFAULT NULL
0
+ 'address_country' TEXT DEFAULT NULL,
0
+ 'gps_location' TEXT DEFAULT NULL
0
 );
0
 
0
 CREATE TABLE 'movies' (
...
57
58
59
 
60
61
62
...
57
58
59
60
61
62
63
0
@@ -57,6 +57,7 @@ CREATE TABLE customers (
0
   address_street varchar(100) default NULL,
0
   address_city varchar(100) default NULL,
0
   address_country varchar(100) default NULL,
0
+ gps_location varchar(100) default NULL,
0
   PRIMARY KEY (id)
0
 );
0
 

Comments

    No one has commented yet.