godfat-dm-mapping 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGES +137 -0
- data/LICENSE +201 -0
- data/NOTICE +9 -0
- data/README +95 -0
- data/Rakefile +58 -0
- data/TODO +4 -0
- data/dm-mapping.gemspec +34 -0
- data/lib/dm-mapping/adapters/abstract_adapter.rb +36 -0
- data/lib/dm-mapping/adapters/sqlite3adapter.rb +40 -0
- data/lib/dm-mapping/model.rb +36 -0
- data/lib/dm-mapping/type_map.rb +10 -0
- data/lib/dm-mapping/version.rb +5 -0
- data/lib/dm-mapping.rb +29 -0
- data/tasks/ann.rake +81 -0
- data/tasks/bones.rake +21 -0
- data/tasks/gem.rake +126 -0
- data/tasks/git.rake +41 -0
- data/tasks/manifest.rake +49 -0
- data/tasks/notes.rake +28 -0
- data/tasks/post_load.rake +39 -0
- data/tasks/rdoc.rake +51 -0
- data/tasks/rubyforge.rake +57 -0
- data/tasks/setup.rb +268 -0
- data/tasks/spec.rake +55 -0
- data/tasks/svn.rake +48 -0
- data/tasks/test.rake +38 -0
- data/test/test_dm-mapping.rb +137 -0
- metadata +112 -0
data/.gitignore
ADDED
data/CHANGES
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
= dm-mapper changes history
|
2
|
+
|
3
|
+
== dm-mapping 0.4.0, 2008-08-04
|
4
|
+
|
5
|
+
* added Migration#auto_genclass.
|
6
|
+
* updated README.
|
7
|
+
* added more rdoc.
|
8
|
+
|
9
|
+
== dm-mapping 0.3.0, 2008-08-04
|
10
|
+
|
11
|
+
* added support of mapping Integer, DateTime, etc.
|
12
|
+
* renamed some internals.
|
13
|
+
* changed the way requiring adapter. no more setup first.
|
14
|
+
* added Migration#storages_and_fields
|
15
|
+
* added mapping :serial => true for primary key.
|
16
|
+
* added mapping :default, and :nullable.
|
17
|
+
* added support of mapping name. (through passing symbol or string)
|
18
|
+
* added support of multiple arguments.
|
19
|
+
* removed Mapping::All, use /.*/ instead.
|
20
|
+
|
21
|
+
== dm-mapping 0.2.1, 2008-08-03
|
22
|
+
|
23
|
+
* fixed a bug that type map should lookup for parent.
|
24
|
+
* fixed a bug that sql type could be lower case.
|
25
|
+
fixed by calling upcase.
|
26
|
+
|
27
|
+
== dm-mapping 0.2.0, 2008-08-02
|
28
|
+
|
29
|
+
* added Sqlite3Adapter::Migration#fields
|
30
|
+
* added DataMapper::Model#mapping
|
31
|
+
* added DataMapper::Model#fields
|
32
|
+
* added DataMapper::TypeMap#find_primitive for reversed lookup.
|
33
|
+
mapping SQL type back to Ruby type.
|
34
|
+
* added corresponded test.
|
35
|
+
|
36
|
+
== dm-mapping 0.1.0, 2008-07-27
|
37
|
+
|
38
|
+
* birthday!
|
39
|
+
* added DataMapper.repository.storages for sqlite3.
|
40
|
+
* please refer:
|
41
|
+
http://groups.google.com/group/datamapper/browse_thread/thread/b9ca41120c5c9389
|
42
|
+
|
43
|
+
original message:
|
44
|
+
|
45
|
+
from Lin Jen-Shin
|
46
|
+
to DataMapper
|
47
|
+
cc godfat
|
48
|
+
date Sun, Jul 27, 2008 at 5:40 PM
|
49
|
+
subject Manipulate an existing database.
|
50
|
+
mailed-by gmail.com
|
51
|
+
|
52
|
+
Greetings,
|
53
|
+
|
54
|
+
DataMapper looks very promising for me, so I am thinking of
|
55
|
+
using it in the near future. I hate separate my domain objects into
|
56
|
+
two parts in Rails, writing migration and switching to ActiveRecord,
|
57
|
+
vice versa, is very annoying to me.
|
58
|
+
|
59
|
+
But there's a very convenient feature to me in ActiveRecord,
|
60
|
+
that is ActiveRecord automatically mapping all fields in a table.
|
61
|
+
It makes me easily control an existing database without any domain object.
|
62
|
+
|
63
|
+
For example,
|
64
|
+
|
65
|
+
require 'active_record'
|
66
|
+
|
67
|
+
ActiveRecord::Base.establish_connection(
|
68
|
+
:adapter => 'sqlite3',
|
69
|
+
:database => 'db/development.sqlite3'
|
70
|
+
)
|
71
|
+
|
72
|
+
clsas User < ActiveRecord::Base
|
73
|
+
end
|
74
|
+
|
75
|
+
User.find 1
|
76
|
+
=> #<User id: 1, account: "admin", created_at: "2008-05-18 20:08:37", etc.>
|
77
|
+
|
78
|
+
Some people would use database admin such as phpMyAdmin to
|
79
|
+
accomplish this kind of task, but I prefer anything in Ruby,
|
80
|
+
calling Ruby function, manipulating data without SQL and
|
81
|
+
any domain object. (i.e. I didn't have to load up entire environment.)
|
82
|
+
|
83
|
+
In DataMapper, I didn't find an easy way to accomplish this.
|
84
|
+
I am sorry if there's one but I didn't find it, please point out,
|
85
|
+
many thanks. In short, I would like to do this in DataMapper:
|
86
|
+
|
87
|
+
class User
|
88
|
+
include DataMapper::Resource
|
89
|
+
mapping :account, :created_at
|
90
|
+
end
|
91
|
+
|
92
|
+
or
|
93
|
+
|
94
|
+
class User
|
95
|
+
include DataMapper::Resource
|
96
|
+
mapping All
|
97
|
+
end
|
98
|
+
|
99
|
+
class User
|
100
|
+
include DataMapper::ResourceAll
|
101
|
+
end
|
102
|
+
|
103
|
+
or
|
104
|
+
|
105
|
+
class User
|
106
|
+
include DataMapper::Resource
|
107
|
+
mapping *storage_fields
|
108
|
+
end
|
109
|
+
|
110
|
+
The above User.storage_fields should return an Array,
|
111
|
+
telling all the fields in the table, e.g. [:account, :created_at, :etc]
|
112
|
+
or a Hash includes data type, e.g. {:account => String,
|
113
|
+
:created_at => DateTime}
|
114
|
+
then mapping *storage_fields should change to:
|
115
|
+
|
116
|
+
mapping *storage_fields.each_key.to_a
|
117
|
+
|
118
|
+
If it's possible, a feature returning the database schema as well:
|
119
|
+
|
120
|
+
DataMapper.repository.storages
|
121
|
+
# => [:users, :posts, :etc]
|
122
|
+
|
123
|
+
DataMapper.repository.storages_and_fields
|
124
|
+
# => {:users => {:account => String},
|
125
|
+
:posts => {:title => String, :content => Text}}
|
126
|
+
|
127
|
+
or returning DataObject::Field, DataObject::Storage, etc.
|
128
|
+
|
129
|
+
DataMapper.repository.storage
|
130
|
+
# => [#<DataObject::Storage @name='users' @fields=
|
131
|
+
[#<DataObject::Field @name='account' @type=String>]>]
|
132
|
+
|
133
|
+
If you feel this kind of feature is indeed needed or not bad for
|
134
|
+
adding it, I could try to provide a patch for it. Though I didn't
|
135
|
+
read the source code deeply, not knowning it's easy or not.
|
136
|
+
|
137
|
+
sincerely,
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/NOTICE
ADDED
data/README
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
= dm-mapping 0.4.0
|
2
|
+
by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
|
3
|
+
strip any number: 18god29fat7029 (at] godfat32 -dooot- 20org
|
4
|
+
|
5
|
+
* github-project[http://github.com/godfat/dm-mapping]
|
6
|
+
* rubyforge-project[http://rubyforge.org/projects/ludy]
|
7
|
+
|
8
|
+
== DESCRIPTION:
|
9
|
+
|
10
|
+
DataMapper plugin that helps you manipulate an existing database.
|
11
|
+
It creates mappings between existing columns and model's properties.
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
require 'dm-mapping'
|
16
|
+
dm = DataMapper.setup :default, 'sqlite3:db/development.sqlite3'
|
17
|
+
|
18
|
+
dm.object_id == DataMapper.repository.adapter.object_id
|
19
|
+
# => true
|
20
|
+
|
21
|
+
class User
|
22
|
+
include DataMapper::Resource
|
23
|
+
# maping all
|
24
|
+
mapping /.*/
|
25
|
+
|
26
|
+
# mapping for ended with _at, and started with salt_
|
27
|
+
mapping /_at$/, /^salt_/
|
28
|
+
|
29
|
+
# mapping id and email
|
30
|
+
mapping :id, :email
|
31
|
+
|
32
|
+
# mapping all fields with type String, and id
|
33
|
+
mapping String, :id
|
34
|
+
|
35
|
+
# mapping login, and all fields with type Integer
|
36
|
+
mapping :login, Integer
|
37
|
+
end
|
38
|
+
|
39
|
+
# there's no guarantee of the order in storages array
|
40
|
+
dm.storages
|
41
|
+
# => ['users']
|
42
|
+
|
43
|
+
# there's no guarantee of the order in fields array
|
44
|
+
User.fields.sort
|
45
|
+
# => [['created_at', DateTime, {}],
|
46
|
+
['email', String, {:default => 'nospam@nospam.tw'}],
|
47
|
+
['id', Integer, {:serial => true}],
|
48
|
+
['salt_first', String, {}],
|
49
|
+
['salt_second', String, {}]]
|
50
|
+
|
51
|
+
dm.fields('users').sort == User.fields.sort
|
52
|
+
# => true
|
53
|
+
|
54
|
+
dm.storages_and_fields
|
55
|
+
# => {'users' => [['id', Integer, {:serial => true}],
|
56
|
+
['email', String, {:default => 'nospam@nospam.tw'}],
|
57
|
+
['created_at', DateTime, {}],
|
58
|
+
['salt_first', String, {}],
|
59
|
+
['salt_second', String, {}]]}
|
60
|
+
|
61
|
+
# there's no guarantee of the order in returned array
|
62
|
+
dm.auto_genclass
|
63
|
+
# => [DataMapper::Mapping::User,
|
64
|
+
DataMapper::Mapping::SchemaInfo,
|
65
|
+
DataMapper::Mapping::Session]
|
66
|
+
|
67
|
+
# you can change the scope of generated models:
|
68
|
+
dm.auto_genclass Object
|
69
|
+
# => [User, SchemaInfo, Session]
|
70
|
+
|
71
|
+
== REQUIREMENTS:
|
72
|
+
|
73
|
+
* dm-core 0.9.3
|
74
|
+
|
75
|
+
== INSTALL:
|
76
|
+
|
77
|
+
> sudo gem install dm-mapping
|
78
|
+
|
79
|
+
== LICENSE:
|
80
|
+
|
81
|
+
Apache License 2.0
|
82
|
+
|
83
|
+
Copyright (c) 2008, Lin Jen-Shin (a.k.a. godfat 真常)
|
84
|
+
|
85
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
86
|
+
you may not use this file except in compliance with the License.
|
87
|
+
You may obtain a copy of the License at
|
88
|
+
|
89
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
90
|
+
|
91
|
+
Unless required by applicable law or agreed to in writing, software
|
92
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
93
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
94
|
+
See the License for the specific language governing permissions and
|
95
|
+
limitations under the License.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# Look in the tasks/setup.rb file for the various options that can be
|
3
|
+
# configured in this Rakefile. The .rake files in the tasks directory
|
4
|
+
# are where the options are used.
|
5
|
+
load 'tasks/setup.rb'
|
6
|
+
|
7
|
+
ensure_in_path 'lib'
|
8
|
+
|
9
|
+
CLEAN.include Dir['**/*.rbc']
|
10
|
+
|
11
|
+
task :default do
|
12
|
+
Rake.application.options.show_task_pattern = /./
|
13
|
+
Rake.application.display_tasks_and_comments
|
14
|
+
end
|
15
|
+
|
16
|
+
namespace :gem do
|
17
|
+
desc 'create dm-mapping.gemspec'
|
18
|
+
task 'gemspec' do
|
19
|
+
puts 'rake gem:debug > dm-mapping.gemspec'
|
20
|
+
File.open('dm-mapping.gemspec', 'w'){|spec| spec << `rake gem:debug`.sub(/.*/, '')}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
namespace :git do
|
25
|
+
desc 'push to github'
|
26
|
+
task 'push' do
|
27
|
+
sh 'git push origin master'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# supress warnings, there's too many warnings in dm-core
|
32
|
+
PROJ.ruby_opts.delete '-w'
|
33
|
+
|
34
|
+
PROJ.name = 'dm-mapping'
|
35
|
+
PROJ.authors = 'Lin Jen-Shin (a.k.a. godfat 真常)'
|
36
|
+
PROJ.email = 'strip any number: 18god29fat7029 (at] godfat32 -dooot- 20org'
|
37
|
+
PROJ.url = 'http://github.com/godfat/dm-mapping'
|
38
|
+
PROJ.description = PROJ.summary = paragraphs_of('README', 'description').join("\n\n")
|
39
|
+
PROJ.changes = paragraphs_of('CHANGES', 0..1).join("\n\n")
|
40
|
+
PROJ.rubyforge.name = 'ludy'
|
41
|
+
PROJ.version = File.open('lib/dm-mapping/version.rb', 0){ |f| f.read.gsub(/.*VERSION = '(.*)'.*/m, '\1') }
|
42
|
+
|
43
|
+
PROJ.gem.dependencies << ['dm-core', '>=0.9.3']
|
44
|
+
# PROJ.gem.executables = []
|
45
|
+
# PROJ.gem.files = []
|
46
|
+
|
47
|
+
PROJ.manifest_file = 'Manifest'
|
48
|
+
PROJ.exclude << 'Manifest' << '^tmp'
|
49
|
+
|
50
|
+
PROJ.rdoc.main = 'README'
|
51
|
+
PROJ.rdoc.exclude << 'Manifest' << 'Rakefile' << 'tmp$' << '^tmp'
|
52
|
+
PROJ.rdoc.include << '\w+'
|
53
|
+
PROJ.rdoc.opts << '--diagram' if !WIN32 and `which dot` =~ %r/\/dot/
|
54
|
+
PROJ.rdoc.opts << '--charset=utf-8' << '--inline-source' << '--line-numbers' << '--promiscuous'
|
55
|
+
|
56
|
+
PROJ.spec.opts << '--color'
|
57
|
+
|
58
|
+
# EOF
|
data/TODO
ADDED
data/dm-mapping.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = %q{dm-mapping}
|
4
|
+
s.version = "0.3.0"
|
5
|
+
|
6
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
7
|
+
s.authors = ["Lin Jen-Shin (a.k.a. godfat \347\234\237\345\270\270)"]
|
8
|
+
s.date = %q{2008-08-04}
|
9
|
+
s.description = %q{DataMapper plugin that helps you manipulate an existing database. It creates mappings between existing columns and model's properties.}
|
10
|
+
s.email = %q{strip any number: 18god29fat7029 (at] godfat32 -dooot- 20org}
|
11
|
+
s.extra_rdoc_files = [".gitignore", "CHANGES", "LICENSE", "NOTICE", "README", "TODO", "dm-mapping.gemspec", "tasks/ann.rake", "tasks/bones.rake", "tasks/gem.rake", "tasks/git.rake", "tasks/manifest.rake", "tasks/notes.rake", "tasks/post_load.rake", "tasks/rdoc.rake", "tasks/rubyforge.rake", "tasks/spec.rake", "tasks/svn.rake", "tasks/test.rake"]
|
12
|
+
s.files = [".gitignore", "CHANGES", "LICENSE", "NOTICE", "README", "Rakefile", "TODO", "dm-mapping.gemspec", "lib/dm-mapping.rb", "lib/dm-mapping/adapters/abstract_adapter.rb", "lib/dm-mapping/adapters/sqlite3adapter.rb", "lib/dm-mapping/model.rb", "lib/dm-mapping/type_map.rb", "lib/dm-mapping/version.rb", "tasks/ann.rake", "tasks/bones.rake", "tasks/gem.rake", "tasks/git.rake", "tasks/manifest.rake", "tasks/notes.rake", "tasks/post_load.rake", "tasks/rdoc.rake", "tasks/rubyforge.rake", "tasks/setup.rb", "tasks/spec.rake", "tasks/svn.rake", "tasks/test.rake", "test/test_dm-mapping.rb"]
|
13
|
+
s.has_rdoc = true
|
14
|
+
s.homepage = %q{http://github.com/godfat/dm-mapping}
|
15
|
+
s.rdoc_options = ["--diagram", "--charset=utf-8", "--inline-source", "--line-numbers", "--promiscuous", "--main", "README"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{ludy}
|
18
|
+
s.rubygems_version = %q{1.2.0}
|
19
|
+
s.summary = %q{DataMapper plugin that helps you manipulate an existing database. It creates mappings between existing columns and model's properties.}
|
20
|
+
s.test_files = ["test/test_dm-mapping.rb"]
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if current_version >= 3 then
|
27
|
+
s.add_runtime_dependency(%q<dm-core>, [">= 0.9.3"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<dm-core>, [">= 0.9.3"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<dm-core>, [">= 0.9.3"])
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
module DataMapper
|
3
|
+
module Adapters
|
4
|
+
class AbstractAdapter
|
5
|
+
module Migration
|
6
|
+
# returns all tables in the repository.
|
7
|
+
# e.g. ['comments', 'posts']
|
8
|
+
def storages
|
9
|
+
raise NotImplementedError
|
10
|
+
end
|
11
|
+
|
12
|
+
def fields storage
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
def storages_and_fields
|
17
|
+
storages.inject({}){ |result, storage|
|
18
|
+
result[storage] = fields(storage)
|
19
|
+
result
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def auto_genclass scope = DataMapper::Mapping
|
24
|
+
require 'extlib'
|
25
|
+
storages_and_fields.map{ |storage, fields|
|
26
|
+
model = Class.new
|
27
|
+
model.__send__ :include, DataMapper::Resource
|
28
|
+
model.storage_names[:default] = storage
|
29
|
+
model.__send__ :mapping, /.*/
|
30
|
+
scope.const_set(Extlib::Inflection.classify(storage), model)
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
require 'dm-mapping/adapters/abstract_adapter'
|
3
|
+
require 'dm-mapping/type_map'
|
4
|
+
|
5
|
+
module DataMapper
|
6
|
+
module Adapters
|
7
|
+
class Sqlite3Adapter < DataObjectsAdapter
|
8
|
+
module Migration
|
9
|
+
def storages
|
10
|
+
# activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
|
11
|
+
sql = <<-SQL
|
12
|
+
SELECT name
|
13
|
+
FROM sqlite_master
|
14
|
+
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
|
15
|
+
SQL
|
16
|
+
# activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 181
|
17
|
+
|
18
|
+
query sql
|
19
|
+
end
|
20
|
+
|
21
|
+
def fields table
|
22
|
+
query_table(table).map{ |field|
|
23
|
+
type, chain = self.class.type_map.
|
24
|
+
lookup_primitive(field.type.gsub(/\(\d+\)/, '').upcase)
|
25
|
+
|
26
|
+
# stupid hack
|
27
|
+
type = String if type == Class
|
28
|
+
|
29
|
+
attrs = {}
|
30
|
+
attrs[:serial] = true if field.pk != 0
|
31
|
+
attrs[:nullable] = true if field.notnull != 0 && !attrs[:serial]
|
32
|
+
attrs[:default] = field.dflt_value[1..-2] if field.dflt_value
|
33
|
+
|
34
|
+
[field.name, type, attrs.merge(chain.attributes)]
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
module DataMapper
|
3
|
+
module Model
|
4
|
+
# returing all fields, with format [[name, type, attrs]]
|
5
|
+
def fields
|
6
|
+
DataMapper.ensure_required_dm_mapping_adapter
|
7
|
+
DataMapper.repository.adapter.fields storage_name
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
def mapping *targets
|
12
|
+
DataMapper.ensure_required_dm_mapping_adapter
|
13
|
+
targets << /.*/ if targets.empty?
|
14
|
+
|
15
|
+
fields.map{ |field|
|
16
|
+
name, type, attrs = field
|
17
|
+
|
18
|
+
targets.each{ |target|
|
19
|
+
case target
|
20
|
+
when Regexp;
|
21
|
+
property name.to_sym, type, attrs if name =~ target
|
22
|
+
|
23
|
+
when Symbol, String;
|
24
|
+
property name.to_sym, type, attrs if name == target.to_s
|
25
|
+
|
26
|
+
when Class;
|
27
|
+
property name.to_sym, type, attrs if type == target
|
28
|
+
|
29
|
+
else
|
30
|
+
raise ArgumentError.new("invalid argument: #{target.inspect}")
|
31
|
+
end
|
32
|
+
}
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|