dm-mapping 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +11 -0
- data/README +43 -24
- data/TODO +3 -2
- data/dm-mapping.gemspec +2 -2
- data/lib/dm-mapping.rb +7 -21
- data/lib/dm-mapping/adapters/abstract_adapter.rb +70 -24
- data/lib/dm-mapping/adapters/mysql_adapter.rb +12 -0
- data/lib/dm-mapping/model.rb +17 -10
- data/lib/dm-mapping/type_map.rb +3 -1
- data/lib/dm-mapping/version.rb +1 -1
- data/test/abstract.rb +88 -21
- metadata +2 -2
data/CHANGES
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
= dm-mapper changes history
|
2
2
|
|
3
|
+
=== dm-mapping 0.6.0, 2008-08-16
|
4
|
+
* mapping returns an array of properties indicating fields it mapped.
|
5
|
+
* performance boosted by refactored mapping implementation.
|
6
|
+
* changed the way using auto_genclass!, now accepts args like mapping!
|
7
|
+
* changed fields to return field name with Symbol instead of String.
|
8
|
+
this would make it be more consistent with DataMapper.
|
9
|
+
* storage names remain String.
|
10
|
+
* added more mysql data type to map
|
11
|
+
* use Extlib::Hook to setup dm-mapping instead of stupid alias_method.
|
12
|
+
* removed ensure_require in model. always setup DataMapper before define model.
|
13
|
+
|
3
14
|
=== dm-mapping 0.5.0, 2008-08-14
|
4
15
|
* feature added
|
5
16
|
- added mysql support.
|
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= dm-mapping 0.
|
1
|
+
= dm-mapping 0.6.0
|
2
2
|
by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
|
3
3
|
godfat (XD) godfat.org
|
4
4
|
|
@@ -15,15 +15,21 @@ by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
|
|
15
15
|
|
16
16
|
== SYNOPSIS:
|
17
17
|
|
18
|
-
require 'dm-mapping'
|
18
|
+
require 'dm-mapping' # this would require 'dm-core'
|
19
19
|
dm = DataMapper.setup :default, 'sqlite3:db/development.sqlite3'
|
20
20
|
|
21
21
|
class User
|
22
22
|
include DataMapper::Resource
|
23
|
-
# mapping all
|
24
|
-
mapping /.*/
|
23
|
+
# mapping all, returns an array of properties indicating fields it mapped
|
24
|
+
mapping /.*/ # e.g. => [#<Property:#<Class:0x18f89b8>:id>,
|
25
|
+
# #<Property:#<Class:0x18f89b8>:title>,
|
26
|
+
# #<Property:#<Class:0x18f89b8>:body>,
|
27
|
+
# #<Property:#<Class:0x18f89b8>:user_id>]
|
25
28
|
|
26
|
-
# mapping
|
29
|
+
# mapping all (with no argument at all)
|
30
|
+
mapping
|
31
|
+
|
32
|
+
# mapping for field name ended with _at, and started with salt_
|
27
33
|
mapping /_at$/, /^salt_/
|
28
34
|
|
29
35
|
# mapping id and email
|
@@ -41,27 +47,28 @@ by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
|
|
41
47
|
# => ['users']
|
42
48
|
|
43
49
|
# there's no guarantee of the order in fields array
|
44
|
-
User.fields
|
45
|
-
# => [[
|
46
|
-
[
|
47
|
-
|
48
|
-
[
|
49
|
-
|
50
|
-
[
|
51
|
-
[
|
52
|
-
|
53
|
-
dm.fields('users').
|
50
|
+
User.fields
|
51
|
+
# => [[:created_at, DateTime, {:nullable => true}],
|
52
|
+
[:email, String, {:nullable => true, :size => 255,
|
53
|
+
:default => 'nospam@nospam.tw'}],
|
54
|
+
[:id, Integer, {:nullable => false, :serial => true,
|
55
|
+
:key => true}],
|
56
|
+
[:salt_first, String, {:nullable => true, :size => 50}],
|
57
|
+
[:salt_second, String, {:nullable => true, :size => 50}]]
|
58
|
+
|
59
|
+
dm.fields('users').sort_by{ |field| field.first.to_s } ==
|
60
|
+
User.fields.sort_by{ |field| field.first.to_s }
|
54
61
|
# => true
|
55
62
|
|
56
63
|
dm.storages_and_fields
|
57
|
-
# => {'users' => [[
|
58
|
-
|
59
|
-
|
60
|
-
[
|
61
|
-
|
62
|
-
[
|
63
|
-
[
|
64
|
-
[
|
64
|
+
# => {'users' => [[:id, Integer, {:nullable => false,
|
65
|
+
:serial => true,
|
66
|
+
:key => true}],
|
67
|
+
[:email, String, {:nullable => true,
|
68
|
+
:default => 'nospam@nospam.tw'}],
|
69
|
+
[:created_at, DateTime, {:nullable => true}],
|
70
|
+
[:salt_first, String, {:nullable => true, :size => 50}],
|
71
|
+
[:salt_second, String, {:nullable => true, :size => 50}]]}
|
65
72
|
|
66
73
|
# there's no guarantee of the order in returned array
|
67
74
|
dm.auto_genclass!
|
@@ -70,9 +77,21 @@ by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
|
|
70
77
|
DataMapper::Mapping::Session]
|
71
78
|
|
72
79
|
# you can change the scope of generated models:
|
73
|
-
dm.auto_genclass! Object
|
80
|
+
dm.auto_genclass! :scope => Object
|
74
81
|
# => [User, SchemaInfo, Session]
|
75
82
|
|
83
|
+
# you can generate classes for tables you specified only:
|
84
|
+
dm.auto_genclass! :scope => Object, :storages => /^phpbb_/
|
85
|
+
# => [PhpbbUser, PhpbbPost, PhpbbConfig]
|
86
|
+
|
87
|
+
# you can generate classes with String too:
|
88
|
+
dm.auto_genclass! :storages => ['users', 'config'], :scope => Object
|
89
|
+
# => [User, Config]
|
90
|
+
|
91
|
+
# you can generate a class only:
|
92
|
+
dm.auto_genclass! :storages => 'users'
|
93
|
+
# => [DataMapper::Mapping::User]
|
94
|
+
|
76
95
|
== REQUIREMENTS:
|
77
96
|
|
78
97
|
* dm-core 0.9.3
|
data/TODO
CHANGED
data/dm-mapping.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = %q{dm-mapping}
|
4
|
-
s.version = "0.
|
4
|
+
s.version = "0.6.0"
|
5
5
|
|
6
6
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
7
7
|
s.authors = ["Lin Jen-Shin (a.k.a. godfat \347\234\237\345\270\270)"]
|
8
|
-
s.date = %q{2008-08-
|
8
|
+
s.date = %q{2008-08-16}
|
9
9
|
s.description = %q{DataMapper plugin that helps you manipulate an existing database. It creates mappings between existing columns and model's properties.}
|
10
10
|
s.email = %q{godfat (XD) godfat.org}
|
11
11
|
s.extra_rdoc_files = ["CHANGES", "LICENSE", "NOTICE", "README", "TODO", "dm-mapping.gemspec"]
|
data/lib/dm-mapping.rb
CHANGED
@@ -1,31 +1,17 @@
|
|
1
1
|
|
2
2
|
gem 'dm-core', '=0.9.3'
|
3
3
|
require 'dm-core'
|
4
|
+
require 'extlib'
|
4
5
|
|
5
6
|
module DataMapper
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
include Extlib::Hook
|
8
|
+
after_class_method :setup do
|
9
|
+
adapter_name = repository.adapter.class.to_s.split('::').last
|
10
|
+
require "dm-mapping/adapters/#{Extlib::Inflection.underscore(adapter_name)}"
|
9
11
|
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
def ensure_required_dm_mapping_adapter
|
14
|
-
require 'extlib'
|
15
|
-
adapter_name = repository.adapter.class.to_s.split('::').last
|
16
|
-
require "dm-mapping/adapters/#{Extlib::Inflection.underscore(adapter_name)}"
|
17
|
-
end
|
18
|
-
|
19
|
-
# dirty hack that hook requirement after setup.
|
20
|
-
alias_method :__setup_alias_by_dm_mapping__, :setup
|
21
|
-
private :__setup_alias_by_dm_mapping__
|
22
|
-
# dirty hack that hook requirement after setup.
|
23
|
-
# usage is the same as original setup.
|
24
|
-
def setup name, uri_or_options
|
25
|
-
adapter = __setup_alias_by_dm_mapping__ name, uri_or_options
|
26
|
-
ensure_required_dm_mapping_adapter
|
27
|
-
adapter
|
28
|
-
end
|
13
|
+
# default scope for Migration#auto_genclasses! series.
|
14
|
+
module Mapping # namespace
|
29
15
|
end
|
30
16
|
|
31
17
|
end
|
@@ -12,27 +12,33 @@ module DataMapper
|
|
12
12
|
|
13
13
|
# returns all fields, with format [[name, type, attrs]]
|
14
14
|
# e.g.
|
15
|
-
# [[
|
16
|
-
# [
|
17
|
-
#
|
18
|
-
# [
|
19
|
-
#
|
15
|
+
# [[:created_at, DateTime, {:nullable => true}],
|
16
|
+
# [:email, String, {:nullable => true, :size => 255,
|
17
|
+
# :default => 'nospam@nospam.tw'}],
|
18
|
+
# [:id, Integer, {:nullable => false, :serial => true,
|
19
|
+
# :key => true}],
|
20
|
+
# [:salt_first, String, {:nullable => true, :size => 50}],
|
21
|
+
# [:salt_second, String, {:nullable => true, :size => 50}]]
|
20
22
|
def fields storage
|
21
23
|
dmm_query_storage(storage).map{ |field|
|
22
|
-
|
23
|
-
|
24
|
+
primitive = dmm_primitive(field)
|
25
|
+
type, chain = self.class.type_map.lookup_primitive(primitive) ||
|
26
|
+
dmm_lookup_primitive(primitive)
|
24
27
|
|
25
|
-
[dmm_field_name(field), type, dmm_attributes(field)]
|
28
|
+
[dmm_field_name(field).to_sym, type, dmm_attributes(field)]
|
26
29
|
}
|
27
30
|
end
|
28
31
|
|
29
32
|
# returns a hash with storage names in keys and
|
30
33
|
# corresponded fields in values. e.g.
|
31
|
-
# {'users' => [[
|
32
|
-
#
|
33
|
-
#
|
34
|
-
# [
|
35
|
-
#
|
34
|
+
# {'users' => [[:id, Integer, {:nullable => false,
|
35
|
+
# :serial => true,
|
36
|
+
# :key => true}],
|
37
|
+
# [:email, String, {:nullable => true,
|
38
|
+
# :default => 'nospam@nospam.tw'}],
|
39
|
+
# [:created_at, DateTime, {:nullable => true}],
|
40
|
+
# [:salt_first, String, {:nullable => true, :size => 50}],
|
41
|
+
# [:salt_second, String, {:nullable => true, :size => 50}]]}
|
36
42
|
# see Migration#storages and Migration#fields for detail
|
37
43
|
def storages_and_fields
|
38
44
|
storages.inject({}){ |result, storage|
|
@@ -41,7 +47,7 @@ module DataMapper
|
|
41
47
|
}
|
42
48
|
end
|
43
49
|
|
44
|
-
# automaticly generate
|
50
|
+
# automaticly generate model class(es) and mapping
|
45
51
|
# all fields with mapping /.*/ for you.
|
46
52
|
# e.g.
|
47
53
|
# dm.auto_genclass!
|
@@ -51,23 +57,63 @@ module DataMapper
|
|
51
57
|
#
|
52
58
|
# you can change the scope of generated models:
|
53
59
|
# e.g.
|
54
|
-
# dm.auto_genclass! Object
|
60
|
+
# dm.auto_genclass! :scope => Object
|
55
61
|
# # => [User, SchemaInfo, Session]
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
#
|
63
|
+
# you can generate classes for tables you specified only:
|
64
|
+
# e.g.
|
65
|
+
# dm.auto_genclass! :scope => Object, :storages => /^phpbb_/
|
66
|
+
# # => [PhpbbUser, PhpbbPost, PhpbbConfig]
|
67
|
+
#
|
68
|
+
# you can generate classes with String too:
|
69
|
+
# e.g.
|
70
|
+
# dm.auto_genclass! :storages => ['users', 'config'], :scope => Object
|
71
|
+
# # => [User, Config]
|
72
|
+
#
|
73
|
+
# you can generate a class only:
|
74
|
+
# e.g.
|
75
|
+
# dm.auto_genclass! :storages => 'users'
|
76
|
+
# # => [DataMapper::Mapping::User]
|
77
|
+
def auto_genclass! opts = {}
|
78
|
+
opts[:scope] ||= DataMapper::Mapping
|
79
|
+
opts[:storages] ||= /.*/
|
80
|
+
opts[:storages] = [opts[:storages]].flatten
|
81
|
+
|
82
|
+
storages.map{ |storage|
|
83
|
+
|
84
|
+
mapped = opts[:storages].each{ |target|
|
85
|
+
case target
|
86
|
+
when Regexp;
|
87
|
+
break storage if storage =~ target
|
88
|
+
|
89
|
+
when Symbol, String;
|
90
|
+
break storage if storage == target.to_s
|
91
|
+
|
92
|
+
else
|
93
|
+
raise ArgumentError.new("invalid argument: #{target.inspect}")
|
94
|
+
end
|
95
|
+
}
|
96
|
+
|
97
|
+
dmm_genclass mapped, opts[:scope] if mapped.kind_of?(String)
|
98
|
+
}.compact
|
65
99
|
end
|
66
100
|
|
67
101
|
private
|
68
102
|
def dmm_query_storage
|
69
103
|
raise NotImplementError.new("#{self.class}#fields is not implemented.")
|
70
104
|
end
|
105
|
+
|
106
|
+
def dmm_genclass storage, scope
|
107
|
+
model = Class.new
|
108
|
+
model.__send__ :include, DataMapper::Resource
|
109
|
+
model.storage_names[:default] = storage
|
110
|
+
model.__send__ :mapping, /.*/
|
111
|
+
scope.const_set(Extlib::Inflection.classify(storage), model)
|
112
|
+
end
|
113
|
+
|
114
|
+
def dmm_lookup_primitive primitive
|
115
|
+
raise TypeError.new("#{primitive} not found for #{self.class}")
|
116
|
+
end
|
71
117
|
end
|
72
118
|
end
|
73
119
|
end
|
@@ -41,6 +41,18 @@ module DataMapper
|
|
41
41
|
|
42
42
|
attrs
|
43
43
|
end
|
44
|
+
|
45
|
+
def dmm_lookup_primitive primitive
|
46
|
+
case primitive.upcase
|
47
|
+
when 'TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'YEAR'; Integer
|
48
|
+
when 'DOUBLE'; BigDecimal
|
49
|
+
when 'BOOL'; TrueClass
|
50
|
+
when 'CHAR', 'ENUM', 'SET', 'TINYBLOB', 'MEDIUMBLOB',
|
51
|
+
'BLOB', 'LONGBLOB', 'BINARY', 'VARBINARY'; String
|
52
|
+
when 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT'; DM::Text
|
53
|
+
else super(primitive)
|
54
|
+
end
|
55
|
+
end
|
44
56
|
end
|
45
57
|
end
|
46
58
|
end
|
data/lib/dm-mapping/model.rb
CHANGED
@@ -5,7 +5,6 @@ module DataMapper
|
|
5
5
|
# e.g.
|
6
6
|
# DataMapper.repository.adapter.fields storage_name
|
7
7
|
def fields
|
8
|
-
DataMapper.ensure_required_dm_mapping_adapter
|
9
8
|
DataMapper.repository.adapter.fields storage_name
|
10
9
|
end
|
11
10
|
|
@@ -16,13 +15,20 @@ module DataMapper
|
|
16
15
|
# you can pass it Regexp to map any field it matched, or just
|
17
16
|
# the field name in Symbol or String, or a Class telling it
|
18
17
|
# map any field which type equals to the Class.
|
18
|
+
# returned value is an array of properties indicating fields it mapped
|
19
19
|
# e.g.
|
20
20
|
# class User
|
21
21
|
# include DataMapper::Resource
|
22
|
-
# #
|
23
|
-
# mapping /.*/
|
22
|
+
# # mapping all
|
23
|
+
# mapping /.*/ # e.g. => [#<Property:#<Class:0x18f89b8>:id>,
|
24
|
+
# # #<Property:#<Class:0x18f89b8>:title>,
|
25
|
+
# # #<Property:#<Class:0x18f89b8>:body>,
|
26
|
+
# # #<Property:#<Class:0x18f89b8>:user_id>]
|
24
27
|
#
|
25
|
-
# # mapping
|
28
|
+
# # mapping all (with no argument at all)
|
29
|
+
# mapping
|
30
|
+
#
|
31
|
+
# # mapping for field name ended with _at, and started with salt_
|
26
32
|
# mapping /_at$/, /^salt_/
|
27
33
|
#
|
28
34
|
# # mapping id and email
|
@@ -35,28 +41,29 @@ module DataMapper
|
|
35
41
|
# mapping :login, Integer
|
36
42
|
# end
|
37
43
|
def mapping *targets
|
38
|
-
DataMapper.ensure_required_dm_mapping_adapter
|
39
44
|
targets << /.*/ if targets.empty?
|
40
45
|
|
41
46
|
fields.map{ |field|
|
42
47
|
name, type, attrs = field
|
43
48
|
|
44
|
-
targets.each{ |target|
|
49
|
+
mapped = targets.each{ |target|
|
45
50
|
case target
|
46
51
|
when Regexp;
|
47
|
-
|
52
|
+
break name if name.to_s =~ target
|
48
53
|
|
49
54
|
when Symbol, String;
|
50
|
-
|
55
|
+
break name if name == target.to_sym
|
51
56
|
|
52
57
|
when Class;
|
53
|
-
|
58
|
+
break name if type == target
|
54
59
|
|
55
60
|
else
|
56
61
|
raise ArgumentError.new("invalid argument: #{target.inspect}")
|
57
62
|
end
|
58
63
|
}
|
59
|
-
|
64
|
+
|
65
|
+
property mapped, type, attrs if mapped.kind_of?(Symbol)
|
66
|
+
}.compact
|
60
67
|
end
|
61
68
|
end
|
62
69
|
end
|
data/lib/dm-mapping/type_map.rb
CHANGED
@@ -4,8 +4,10 @@ module DataMapper
|
|
4
4
|
# reversed lookup for primitive type to ruby type.
|
5
5
|
# e.g.
|
6
6
|
# lookup_primitive('DATETIME')
|
7
|
-
# # =>
|
7
|
+
# # => [DateTime, #<DataMapper::TypeMap::TypeChain:0x830b8>]
|
8
8
|
def lookup_primitive primitive, type_map = self
|
9
|
+
return nil unless type_map
|
10
|
+
|
9
11
|
type_map.chains.find{ |type, chain|
|
10
12
|
primitive.upcase == chain.primitive &&
|
11
13
|
[Integer, Float, BigDecimal,
|
data/lib/dm-mapping/version.rb
CHANGED
data/test/abstract.rb
CHANGED
@@ -5,7 +5,7 @@ require 'dm-mapping'
|
|
5
5
|
|
6
6
|
module Abstract
|
7
7
|
def setup_data_mapper
|
8
|
-
raise 'please provide
|
8
|
+
raise 'please provide a clean database because it is a destructive test!!'
|
9
9
|
end
|
10
10
|
|
11
11
|
AttrCommon = {:nullable => true}
|
@@ -13,18 +13,22 @@ module Abstract
|
|
13
13
|
AttrText = {:size => 65535}.merge AttrCommon
|
14
14
|
|
15
15
|
def user_fields
|
16
|
-
[[
|
17
|
-
[
|
18
|
-
[
|
19
|
-
[
|
16
|
+
[[:created_at, DateTime, AttrCommon],
|
17
|
+
[:id, Integer, AttrCommonPK],
|
18
|
+
[:login, String, {:size => 70}.merge(AttrCommon)],
|
19
|
+
[:sig, DM::Text, AttrText]]
|
20
20
|
end
|
21
21
|
|
22
22
|
def comment_fields
|
23
|
-
[[
|
24
|
-
[
|
25
|
-
[
|
23
|
+
[[:body, DM::Text, AttrText],
|
24
|
+
[:id, Integer, AttrCommonPK],
|
25
|
+
[:title, String, {:size => 50, :default => 'default title'}.
|
26
26
|
merge(AttrCommon)],
|
27
|
-
[
|
27
|
+
[:user_id, Integer, AttrCommon]]
|
28
|
+
end
|
29
|
+
|
30
|
+
def super_user_fields
|
31
|
+
[[:id, Integer, AttrCommonPK]]
|
28
32
|
end
|
29
33
|
|
30
34
|
class User
|
@@ -37,6 +41,11 @@ module Abstract
|
|
37
41
|
property :created_at, DateTime
|
38
42
|
end
|
39
43
|
|
44
|
+
class SuperUser
|
45
|
+
include DataMapper::Resource
|
46
|
+
property :id, Integer, :serial => true
|
47
|
+
end
|
48
|
+
|
40
49
|
class Comment
|
41
50
|
include DataMapper::Resource
|
42
51
|
belongs_to :user
|
@@ -48,6 +57,14 @@ module Abstract
|
|
48
57
|
|
49
58
|
class Model; end
|
50
59
|
|
60
|
+
Tables = ['abstract_comments', 'abstract_super_users', 'abstract_users']
|
61
|
+
|
62
|
+
def sort_fields fields
|
63
|
+
fields.sort_by{ |field|
|
64
|
+
field.first.to_s
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
51
68
|
def create_fake_model
|
52
69
|
[ Model.dup.__send__(:include, DataMapper::Resource),
|
53
70
|
setup_data_mapper ]
|
@@ -59,11 +76,16 @@ module Abstract
|
|
59
76
|
# this is significant faster than DataMapper.auto_migrate!
|
60
77
|
User.auto_migrate!
|
61
78
|
Comment.auto_migrate!
|
79
|
+
SuperUser.auto_migrate!
|
80
|
+
end
|
81
|
+
|
82
|
+
def new_scope
|
83
|
+
self.class.const_set("Scope#{object_id.object_id}", Module.new)
|
62
84
|
end
|
63
85
|
|
64
86
|
def test_storages
|
65
|
-
assert_equal
|
66
|
-
assert_equal comment_fields, dm.fields('abstract_comments')
|
87
|
+
assert_equal Tables, dm.storages.sort
|
88
|
+
assert_equal comment_fields, sort_fields(dm.fields('abstract_comments'))
|
67
89
|
end
|
68
90
|
|
69
91
|
def test_create_comment
|
@@ -72,16 +94,25 @@ module Abstract
|
|
72
94
|
assert_equal 'XD', Comment.first.title
|
73
95
|
end
|
74
96
|
|
97
|
+
def test_create_user
|
98
|
+
now = Time.now
|
99
|
+
User.create(:created_at => now)
|
100
|
+
assert_equal 1, User.first.id
|
101
|
+
assert_equal now.asctime, User.first.created_at.asctime
|
102
|
+
|
103
|
+
return now
|
104
|
+
end
|
105
|
+
|
75
106
|
def test_mapping_all
|
76
107
|
test_create_comment # for fixtures
|
77
108
|
model, local_dm = create_fake_model
|
78
109
|
model.storage_names[:default] = 'abstract_comments'
|
79
110
|
|
80
|
-
assert_equal
|
111
|
+
assert_equal Tables, local_dm.storages.sort
|
81
112
|
assert_equal 'abstract_comments', model.storage_name
|
82
113
|
|
83
114
|
assert_equal 1, model.count
|
84
|
-
assert_equal comment_fields, model.fields
|
115
|
+
assert_equal comment_fields, sort_fields(model.fields)
|
85
116
|
|
86
117
|
model.send :mapping
|
87
118
|
assert_equal 'XD', model.first.title
|
@@ -102,12 +133,13 @@ module Abstract
|
|
102
133
|
end
|
103
134
|
|
104
135
|
def test_storages_and_fields
|
105
|
-
assert_equal user_fields, dm.fields('abstract_users')
|
136
|
+
assert_equal user_fields, sort_fields(dm.fields('abstract_users'))
|
106
137
|
assert_equal( {'abstract_users' => user_fields,
|
107
|
-
'abstract_comments' => comment_fields
|
138
|
+
'abstract_comments' => comment_fields,
|
139
|
+
'abstract_super_users' => super_user_fields},
|
108
140
|
dm.storages_and_fields.inject({}){ |r, i|
|
109
141
|
key, value = i
|
110
|
-
r[key] = value.
|
142
|
+
r[key] = value.sort_by{ |v| v.first.to_s }
|
111
143
|
r
|
112
144
|
} )
|
113
145
|
end
|
@@ -142,15 +174,16 @@ module Abstract
|
|
142
174
|
}
|
143
175
|
end
|
144
176
|
|
145
|
-
def
|
146
|
-
scope =
|
177
|
+
def test_auto_genclasses
|
178
|
+
scope = new_scope
|
147
179
|
assert_equal ["#{scope == Object ? '' : "#{scope}::"}AbstractComment",
|
148
|
-
"#{scope
|
149
|
-
|
180
|
+
"#{scope}::AbstractSuperUser",
|
181
|
+
"#{scope}::AbstractUser"],
|
182
|
+
dm.auto_genclass!(:scope => scope).map(&:to_s).sort
|
150
183
|
|
151
184
|
comment = scope.const_get('AbstractComment')
|
152
185
|
|
153
|
-
assert_equal comment_fields, comment.fields
|
186
|
+
assert_equal comment_fields, sort_fields(comment.fields)
|
154
187
|
|
155
188
|
test_create_comment
|
156
189
|
|
@@ -159,4 +192,38 @@ module Abstract
|
|
159
192
|
assert_equal 'dm-mapping', comment.get(2).body
|
160
193
|
end
|
161
194
|
|
195
|
+
def test_auto_genclass
|
196
|
+
scope = new_scope
|
197
|
+
assert_equal ["#{scope}::AbstractUser"],
|
198
|
+
dm.auto_genclass!(:scope => scope,
|
199
|
+
:storages => 'abstract_users').map(&:to_s)
|
200
|
+
|
201
|
+
user = scope.const_get('AbstractUser')
|
202
|
+
assert_equal user_fields, sort_fields(user.fields)
|
203
|
+
|
204
|
+
now = test_create_user
|
205
|
+
|
206
|
+
assert_equal now.asctime, user.first.created_at.asctime
|
207
|
+
user.create(:login => 'godfat')
|
208
|
+
assert_equal 'godfat', user.get(2).login
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_auto_genclass_with_regexp
|
212
|
+
scope = new_scope
|
213
|
+
assert_equal ["#{scope}::AbstractSuperUser", "#{scope}::AbstractUser"],
|
214
|
+
dm.auto_genclass!(:scope => scope,
|
215
|
+
:storages => /_users$/).map(&:to_s).sort
|
216
|
+
|
217
|
+
user = scope.const_get('AbstractSuperUser')
|
218
|
+
assert_equal sort_fields(SuperUser.fields), sort_fields(user.fields)
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_mapping_return_value
|
222
|
+
model, local_dm = create_fake_model
|
223
|
+
model.storage_names[:default] = 'abstract_comments'
|
224
|
+
mapped = model.send :mapping, /.*/
|
225
|
+
|
226
|
+
assert_equal model.properties.map(&:object_id).sort, mapped.map(&:object_id).sort
|
227
|
+
end
|
228
|
+
|
162
229
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-mapping
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Lin Jen-Shin (a.k.a. godfat \xE7\x9C\x9F\xE5\xB8\xB8)"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08-
|
12
|
+
date: 2008-08-17 00:00:00 +08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|