jm81-dm-address 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/dm-address.gemspec +5 -2
- data/lib/dm-address.rb +1 -1
- data/lib/dm-address/polymorphic.rb +41 -0
- data/lib/dm-address/us.rb +4 -0
- data/spec/dm-address/polymorphic_spec.rb +99 -0
- data/spec/dm-address/us_spec.rb +20 -0
- metadata +7 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/dm-address.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{dm-address}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Jared Morgan"]
|
9
|
-
s.date = %q{2009-07-
|
9
|
+
s.date = %q{2009-07-28}
|
10
10
|
s.email = %q{jmorgan@morgancreative.net}
|
11
11
|
s.extra_rdoc_files = [
|
12
12
|
"LICENSE",
|
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
"dm-address.gemspec",
|
23
23
|
"lib/dm-address.rb",
|
24
24
|
"lib/dm-address/phone_number.rb",
|
25
|
+
"lib/dm-address/polymorphic.rb",
|
25
26
|
"lib/dm-address/us.rb",
|
26
27
|
"lib/dm-address/zip_code.rb",
|
27
28
|
"lib/dm-types/phone_number.rb",
|
@@ -29,6 +30,7 @@ Gem::Specification.new do |s|
|
|
29
30
|
"lib/dm-validations/formats/phone_number.rb",
|
30
31
|
"lib/dm-validations/formats/zip_code.rb",
|
31
32
|
"spec/dm-address/phone_number_spec.rb",
|
33
|
+
"spec/dm-address/polymorphic_spec.rb",
|
32
34
|
"spec/dm-address/us_spec.rb",
|
33
35
|
"spec/dm-address/zip_code_spec.rb",
|
34
36
|
"spec/dm-address_spec.rb",
|
@@ -43,6 +45,7 @@ Gem::Specification.new do |s|
|
|
43
45
|
s.summary = %q{TODO}
|
44
46
|
s.test_files = [
|
45
47
|
"spec/dm-address/phone_number_spec.rb",
|
48
|
+
"spec/dm-address/polymorphic_spec.rb",
|
46
49
|
"spec/dm-address/us_spec.rb",
|
47
50
|
"spec/dm-address/zip_code_spec.rb",
|
48
51
|
"spec/dm-address_spec.rb",
|
data/lib/dm-address.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Address
|
3
|
+
# Add fields to an Address class (such as one that includes Address::US)
|
4
|
+
# to allow for roughly polymorphic associations, belonging to other classes.
|
5
|
+
# (See http://pastie.org/214893). Including this module adds two properties:
|
6
|
+
# +addressable_class+ and +addressable_id+, and one method, +addressable+,
|
7
|
+
# for accessing the parent record.
|
8
|
+
#
|
9
|
+
# To use (in Address class):
|
10
|
+
#
|
11
|
+
# include DataMapper::Address::Polymorphic
|
12
|
+
#
|
13
|
+
# In parent class, something along the lines of:
|
14
|
+
#
|
15
|
+
# has n, :addresses, :class_name => 'Address',
|
16
|
+
# :child_key => [:addressable_id],
|
17
|
+
# Address.addressable_class => 'ParentModel'
|
18
|
+
module Polymorphic
|
19
|
+
class << self
|
20
|
+
# Add :addressable_class and :addressable_id properties
|
21
|
+
def included(klass)
|
22
|
+
klass.__send__(:property, :addressable_class, String)
|
23
|
+
klass.__send__(:property, :addressable_id, Integer)
|
24
|
+
end
|
25
|
+
end # class << self
|
26
|
+
|
27
|
+
|
28
|
+
# Return the addressable (that is, parent) record.
|
29
|
+
def addressable
|
30
|
+
parent_class_name = attribute_get(:addressable_class)
|
31
|
+
# Remove a trailing method reference
|
32
|
+
parent_split = parent_class_name.to_s.split('#')
|
33
|
+
parent_class_name = parent_split[0..-2].join('#') if parent_split.length > 1
|
34
|
+
return nil if parent_class_name.blank?
|
35
|
+
parent_class = Object.full_const_get(parent_class_name)
|
36
|
+
parent_class.get(attribute_get(:addressable_id))
|
37
|
+
end
|
38
|
+
|
39
|
+
end # module Polymorphic
|
40
|
+
end # module Address
|
41
|
+
end # module DataMapper
|
data/lib/dm-address/us.rb
CHANGED
@@ -0,0 +1,99 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), '..', "spec_helper" )
|
2
|
+
|
3
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
4
|
+
require 'dm-timestamps' # Only needed to make #created_at and #updated_at auto-update
|
5
|
+
|
6
|
+
module DataMapper::Address::Spec
|
7
|
+
class Polymorphed
|
8
|
+
include DataMapper::Resource
|
9
|
+
include DataMapper::Address::Polymorphic
|
10
|
+
property :id, Serial
|
11
|
+
property :street, String
|
12
|
+
end
|
13
|
+
|
14
|
+
class Person
|
15
|
+
include DataMapper::Resource
|
16
|
+
|
17
|
+
property :id, Serial
|
18
|
+
has n, :addresses, :class_name => 'DataMapper::Address::Spec::Polymorphed',
|
19
|
+
:child_key => [:addressable_id],
|
20
|
+
DataMapper::Address::Spec::Polymorphed.addressable_class =>
|
21
|
+
'DataMapper::Address::Spec::Person'
|
22
|
+
|
23
|
+
property :name, String
|
24
|
+
end
|
25
|
+
|
26
|
+
Polymorphed.auto_migrate!
|
27
|
+
Person.auto_migrate!
|
28
|
+
end
|
29
|
+
|
30
|
+
describe DataMapper::Address::Polymorphic do
|
31
|
+
before(:each) do
|
32
|
+
@parent_class = DataMapper::Address::Spec::Person
|
33
|
+
@parent_class.all.destroy!
|
34
|
+
@parent = @parent_class.create(:name => 'Jane Doe')
|
35
|
+
|
36
|
+
@klass = DataMapper::Address::Spec::Polymorphed
|
37
|
+
@klass.all.destroy!
|
38
|
+
@address = @klass.create(:street => '123 Lane Ave.',
|
39
|
+
:addressable_class => 'DataMapper::Address::Spec::Person',
|
40
|
+
:addressable_id => @parent.id
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should be valid' do
|
45
|
+
# Sanity check only
|
46
|
+
@parent.should be_valid
|
47
|
+
@address.should be_valid
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#addressable' do
|
51
|
+
it 'should return the parent' do
|
52
|
+
@address.addressable.should == @parent
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should error if the class is unknown' do
|
56
|
+
@address.addressable_class = 'DataMapper::Address::Spec::NoClass'
|
57
|
+
lambda { @address.addressable }.should raise_error(NameError)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should be nil if the id does not return a record' do
|
61
|
+
@address.addressable_id = 0
|
62
|
+
@address.addressable.should be_nil
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should remove a method identifier when determining class name' do
|
66
|
+
@address.addressable_class = 'DataMapper::Address::Spec::Person#address'
|
67
|
+
@address.addressable.should == @parent
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe 'parent#addresses' do
|
72
|
+
it 'should retrieve all addresses for this class/id' do
|
73
|
+
address2 = @klass.create(:street => '123 Lane Ave.',
|
74
|
+
:addressable_class => 'DataMapper::Address::Spec::Person',
|
75
|
+
:addressable_id => @parent.id
|
76
|
+
)
|
77
|
+
|
78
|
+
@parent.addresses.should == [@address, address2]
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should not retrieve addresses for other classes' do
|
82
|
+
address2 = @klass.create(:street => '123 Lane Ave.',
|
83
|
+
:addressable_class => 'DataMapper::Address::Spec::NoClass',
|
84
|
+
:addressable_id => @parent.id
|
85
|
+
)
|
86
|
+
|
87
|
+
@parent.addresses.should == [@address]
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should not retrieve addresses for other id' do
|
91
|
+
address2 = @klass.create(:street => '123 Lane Ave.',
|
92
|
+
:addressable_class => 'DataMapper::Address::Spec::Person',
|
93
|
+
:addressable_id => 0
|
94
|
+
)
|
95
|
+
|
96
|
+
@parent.addresses.should == [@address]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/spec/dm-address/us_spec.rb
CHANGED
@@ -221,4 +221,24 @@ describe DataMapper::Address::US do
|
|
221
221
|
@address.updated_at.should be_kind_of(DateTime)
|
222
222
|
end
|
223
223
|
end
|
224
|
+
|
225
|
+
describe '#address_properties' do
|
226
|
+
describe ':polymorphic option' do
|
227
|
+
it 'should include Polymorphic module if true' do
|
228
|
+
klass = Class.new
|
229
|
+
klass.__send__(:include, DataMapper::Resource)
|
230
|
+
klass.__send__(:include, DataMapper::Address::US)
|
231
|
+
klass.address_properties(:polymorphic => true)
|
232
|
+
klass.properties.has_property?(:addressable_class).should be_true
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'should not include Polymorphic module if nil' do
|
236
|
+
klass = Class.new
|
237
|
+
klass.__send__(:include, DataMapper::Resource)
|
238
|
+
klass.__send__(:include, DataMapper::Address::US)
|
239
|
+
klass.address_properties
|
240
|
+
klass.properties.has_property?(:addressable_class).should be_false
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
224
244
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jm81-dm-address
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared Morgan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-28 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- dm-address.gemspec
|
72
72
|
- lib/dm-address.rb
|
73
73
|
- lib/dm-address/phone_number.rb
|
74
|
+
- lib/dm-address/polymorphic.rb
|
74
75
|
- lib/dm-address/us.rb
|
75
76
|
- lib/dm-address/zip_code.rb
|
76
77
|
- lib/dm-types/phone_number.rb
|
@@ -78,6 +79,7 @@ files:
|
|
78
79
|
- lib/dm-validations/formats/phone_number.rb
|
79
80
|
- lib/dm-validations/formats/zip_code.rb
|
80
81
|
- spec/dm-address/phone_number_spec.rb
|
82
|
+
- spec/dm-address/polymorphic_spec.rb
|
81
83
|
- spec/dm-address/us_spec.rb
|
82
84
|
- spec/dm-address/zip_code_spec.rb
|
83
85
|
- spec/dm-address_spec.rb
|
@@ -86,6 +88,7 @@ files:
|
|
86
88
|
- spec/spec_helper.rb
|
87
89
|
has_rdoc: false
|
88
90
|
homepage: http://github.com/jm81/dm-address
|
91
|
+
licenses:
|
89
92
|
post_install_message:
|
90
93
|
rdoc_options:
|
91
94
|
- --charset=UTF-8
|
@@ -106,12 +109,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
109
|
requirements: []
|
107
110
|
|
108
111
|
rubyforge_project:
|
109
|
-
rubygems_version: 1.
|
112
|
+
rubygems_version: 1.3.5
|
110
113
|
signing_key:
|
111
114
|
specification_version: 3
|
112
115
|
summary: TODO
|
113
116
|
test_files:
|
114
117
|
- spec/dm-address/phone_number_spec.rb
|
118
|
+
- spec/dm-address/polymorphic_spec.rb
|
115
119
|
- spec/dm-address/us_spec.rb
|
116
120
|
- spec/dm-address/zip_code_spec.rb
|
117
121
|
- spec/dm-address_spec.rb
|