dm-mapping 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,5 +1,17 @@
1
1
  = dm-mapper changes history
2
2
 
3
+ == dm-mapping 0.3.0,
4
+
5
+ * added support of mapping Integer, DateTime, etc.
6
+ * renamed some internals.
7
+ * changed the way requiring adapter. no more setup first.
8
+ * added Migration#storages_and_fields
9
+ * added mapping :serial => true for primary key.
10
+ * added mapping :default, and :nullable.
11
+ * added support of mapping name. (through passing symbol or string)
12
+ * added support of multiple arguments.
13
+ * removed Mapping::All, use /.*/ instead.
14
+
3
15
  == dm-mapping 0.2.1, 2008-08-03
4
16
 
5
17
  * fixed a bug that type map should lookup for parent.
data/README CHANGED
@@ -1,4 +1,4 @@
1
- = dm-mapping 0.2.1
1
+ = dm-mapping 0.3.0
2
2
  by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
3
3
  strip any number: 18god29fat7029 (at] godfat32 -dooot- 20org
4
4
 
@@ -9,10 +9,6 @@ by Lin Jen-Shin (a.k.a. godfat-真常[http://godfat.org])
9
9
  DataMapper plugin that helps you manipulate an existing database.
10
10
  It creates mappings between existing columns and model's properties.
11
11
 
12
- == FEATURES:
13
-
14
- placeholder
15
-
16
12
  == SYNOPSIS:
17
13
 
18
14
  require 'dm-core'
@@ -24,36 +20,45 @@ placeholder
24
20
 
25
21
  class User
26
22
  include DataMapper::Resource
23
+ # maping all
27
24
  mapping /.*/
28
- mapping DataMapper::Mapping::All
25
+
26
+ # mapping for ended with _at, and started with salt_
29
27
  mapping /_at$/, /^salt_/
30
- mapping :account, :email
31
- mapping *storage_fields
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
32
37
  end
33
38
 
34
39
  DataMapper.repository.adapter.storages
35
40
  # => ['users']
36
41
 
37
42
  User.fields
38
- # => [['account', String, {}],
39
- ['email', String, {}],
43
+ # => [['id', Integer, {:serial => true}],
44
+ ['email', String, {:default => 'nospam@nospam.tw'}],
40
45
  ['created_at', DateTime, {}],
41
46
  ['salt_first', String, {}],
42
47
  ['salt_second', String, {}]]
43
48
 
44
49
  DataMapper.repository.adapter.fields('users')
45
- # => [['account', String, {}],
46
- ['email', String, {}],
50
+ # => [['id', Integer, {:serial => true}],
51
+ ['email', String, {:default => 'nospam@nospam.tw'}],
47
52
  ['created_at', DateTime, {}],
48
53
  ['salt_first', String, {}],
49
54
  ['salt_second', String, {}]]
50
55
 
51
56
  DataMapper.repository.adapter.storages_and_fields
52
- # => {'users' => [['account', String, {}],
53
- ['email', String, {}],
57
+ # => {'users' => [['id', Integer, {:serial => true}],
58
+ ['email', String, {:default => 'nospam@nospam.tw'}],
54
59
  ['created_at', DateTime, {}],
55
60
  ['salt_first', String, {}],
56
- ['salt_second', String, {}]] }
61
+ ['salt_second', String, {}]]}
57
62
 
58
63
  == REQUIREMENTS:
59
64
 
data/Rakefile CHANGED
@@ -28,6 +28,9 @@ namespace :git do
28
28
  end
29
29
  end
30
30
 
31
+ # supress warnings, there's too many warnings in dm-core
32
+ PROJ.ruby_opts.delete '-w'
33
+
31
34
  PROJ.name = 'dm-mapping'
32
35
  PROJ.authors = 'Lin Jen-Shin (a.k.a. godfat 真常)'
33
36
  PROJ.email = 'strip any number: 18god29fat7029 (at] godfat32 -dooot- 20org'
@@ -35,9 +38,9 @@ PROJ.url = 'http://github.com/godfat/dm-mapping'
35
38
  PROJ.description = PROJ.summary = paragraphs_of('README', 'description').join("\n\n")
36
39
  PROJ.changes = paragraphs_of('CHANGES', 0..1).join("\n\n")
37
40
  PROJ.rubyforge.name = 'ludy'
38
- PROJ.version = File.open('lib/dm-mapping/version.rb', 0){ |f| f.read.gsub(/.*VERSION = "(.*)".*/m, '\1') }
41
+ PROJ.version = File.open('lib/dm-mapping/version.rb', 0){ |f| f.read.gsub(/.*VERSION = '(.*)'.*/m, '\1') }
39
42
 
40
- PROJ.gem.dependencies << 'dm-core'
43
+ PROJ.gem.dependencies << ['dm-core', '>=0.9.3']
41
44
  # PROJ.gem.executables = []
42
45
  # PROJ.gem.files = []
43
46
 
data/TODO CHANGED
@@ -1,9 +1,4 @@
1
1
  = dm-mapping todo list
2
2
 
3
- * o DataMapper::Resource#mapping
4
- * o DataMapper::Resource#storage_fields
5
- * x DataMapper.repository.storages_and_fields
6
-
7
- * o Mapping::All
8
- * Mapping::String
9
- * Mapping::Integer
3
+ * postgresql adapter
4
+ * mysql adapter
data/dm-mapping.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
 
2
2
  Gem::Specification.new do |s|
3
3
  s.name = %q{dm-mapping}
4
- s.version = "0.2.1"
4
+ s.version = "0.3.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-03}
8
+ s.date = %q{2008-08-04}
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{strip any number: 18god29fat7029 (at] godfat32 -dooot- 20org}
11
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/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"]
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
13
  s.has_rdoc = true
14
14
  s.homepage = %q{http://github.com/godfat/dm-mapping}
15
15
  s.rdoc_options = ["--diagram", "--charset=utf-8", "--inline-source", "--line-numbers", "--promiscuous", "--main", "README"]
@@ -24,11 +24,11 @@ Gem::Specification.new do |s|
24
24
  s.specification_version = 2
25
25
 
26
26
  if current_version >= 3 then
27
- s.add_runtime_dependency(%q<dm-core>, [">= 0"])
27
+ s.add_runtime_dependency(%q<dm-core>, [">= 0.9.3"])
28
28
  else
29
- s.add_dependency(%q<dm-core>, [">= 0"])
29
+ s.add_dependency(%q<dm-core>, [">= 0.9.3"])
30
30
  end
31
31
  else
32
- s.add_dependency(%q<dm-core>, [">= 0"])
32
+ s.add_dependency(%q<dm-core>, [">= 0.9.3"])
33
33
  end
34
34
  end
data/lib/dm-mapping.rb CHANGED
@@ -3,27 +3,24 @@ gem 'dm-core', '=0.9.3'
3
3
  require 'dm-core'
4
4
 
5
5
  module DataMapper
6
- module Mapping
7
- All = /.*/
8
- end
9
- end
10
6
 
11
- module DataMapper
12
- module Adapters
13
- class AbstractAdapter
14
- module Migration
15
- # returns all tables in the repository.
16
- # e.g. ['comments', 'posts']
17
- def storages
18
- raise NotImplementedError
19
- end
7
+ class << self
8
+ # ensure the using adapter is extended by dm-mapping
9
+ def ensure_required_dm_mapping_adapter
10
+ adapter_name = repository.adapter.class.to_s.split('::').last.downcase
11
+ require "dm-mapping/adapters/#{adapter_name}"
12
+ end
20
13
 
21
- def fields storage
22
- raise NotImplementedError
23
- end
24
- end
14
+ # dirty hack that hook requirement after setup.
15
+ alias_method :__setup_alias_by_dm_mapping__, :setup
16
+ private :__setup_alias_by_dm_mapping__
17
+ def setup name, uri_or_options
18
+ adapter = __setup_alias_by_dm_mapping__ name, uri_or_options
19
+ ensure_required_dm_mapping_adapter
20
+ adapter
25
21
  end
26
22
  end
23
+
27
24
  end
28
25
 
29
- require "dm-mapping/adapters/#{DataMapper.repository.adapter.class.to_s.split('::').last.downcase}"
26
+ require 'dm-mapping/model'
@@ -0,0 +1,25 @@
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
+ end
23
+ end
24
+ end
25
+ end
@@ -1,7 +1,6 @@
1
1
 
2
- require 'dm-mapping'
2
+ require 'dm-mapping/adapters/abstract_adapter'
3
3
  require 'dm-mapping/type_map'
4
- require 'dm-mapping/model'
5
4
 
6
5
  module DataMapper
7
6
  module Adapters
@@ -22,12 +21,17 @@ module DataMapper
22
21
  def fields table
23
22
  query_table(table).map{ |field|
24
23
  type, chain = self.class.type_map.
25
- find_primitive(field.type.gsub(/\(\d+\)/, '').upcase)
24
+ lookup_primitive(field.type.gsub(/\(\d+\)/, '').upcase)
26
25
 
27
26
  # stupid hack
28
27
  type = String if type == Class
29
28
 
30
- [field.name, type, chain.attributes]
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)]
31
35
  }
32
36
  end
33
37
  end
@@ -1,14 +1,36 @@
1
1
 
2
2
  module DataMapper
3
3
  module Model
4
- def mapping regexp = /.*/
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
+
5
15
  fields.map{ |field|
6
16
  name, type, attrs = field
7
- property name.to_sym, type, attrs if name =~ regexp
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
+ }
8
33
  }
9
34
  end
10
- def fields
11
- DataMapper.repository.adapter.fields storage_name
12
- end
13
35
  end
14
36
  end
@@ -1,10 +1,10 @@
1
1
 
2
2
  module DataMapper
3
3
  class TypeMap
4
- def find_primitive primitive, type_map = self
4
+ def lookup_primitive primitive, type_map = self
5
5
  type_map.chains.find{ |type, chain|
6
6
  primitive == chain.primitive
7
- } or find_primitive(primitive, type_map.parent)
7
+ } or lookup_primitive(primitive, type_map.parent)
8
8
  end
9
9
  end
10
10
  end
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Mapping
3
- VERSION = "0.2.1" unless defined?(DataMapper::Mapping::VERSION)
3
+ VERSION = '0.3.0' unless defined?(DataMapper::Mapping::VERSION)
4
4
  end
5
5
  end
@@ -3,6 +3,7 @@ require 'test/unit'
3
3
 
4
4
  require 'rubygems'
5
5
  require 'data_mapper'
6
+ require 'dm-mapping'
6
7
 
7
8
  class DMMTest < Test::Unit::TestCase
8
9
  class User
@@ -20,43 +21,117 @@ class DMMTest < Test::Unit::TestCase
20
21
  belongs_to :user
21
22
 
22
23
  property :id, Integer, :serial => true
23
- property :title, String
24
+ property :title, String, :default => 'default title'
24
25
  property :body, Text
25
26
  end
26
27
 
27
- class Tmp; end
28
+ class Model; end
28
29
 
29
- def test_storages
30
- @dm = DataMapper.setup :default, 'sqlite3:tmp.db'
31
- require 'dm-mapping'
32
- DataMapper.auto_migrate!
30
+ def create_fake_model
31
+ [Model.dup.__send__(:include, DataMapper::Resource),
32
+ DataMapper.setup(:default, 'sqlite3:tmp.db')]
33
+ end
34
+
35
+ @@dm = DataMapper.setup :default, 'sqlite3:tmp.db'
36
+ def dm; @@dm; end
37
+
38
+ def setup
39
+ User.auto_migrate!
40
+ Comment.auto_migrate!
41
+ end
42
+
43
+ def user_fields
44
+ [['created_at', DateTime, {}],
45
+ ['id', Integer, {:serial => true}],
46
+ ['login', String, {}],
47
+ ['sig', DataMapper::Types::Text, {}]]
48
+ end
33
49
 
34
- assert_equal ['dmm_test_comments', 'dmm_test_users'], @dm.storages.sort
35
- comment_fields = [["body", DataMapper::Types::Text, {}],
36
- ["id", Integer, {}],
37
- ["title", String, {}],
38
- ["user_id", Integer, {}]]
50
+ def comment_fields
51
+ [['body', DataMapper::Types::Text, {}],
52
+ ['id', Integer , {:serial => true}],
53
+ ['title', String , {:default => 'default title'}],
54
+ ['user_id', Integer , {}]]
55
+ end
39
56
 
40
- assert_equal comment_fields, @dm.fields('dmm_test_comments').sort
57
+ def test_storages
58
+ assert_equal ['dmm_test_comments', 'dmm_test_users'], dm.storages.sort
59
+ assert_equal comment_fields, dm.fields('dmm_test_comments').sort
60
+ end
41
61
 
62
+ def test_create_comment
42
63
  Comment.create(:title => 'XD')
43
64
  assert_equal 1, Comment.first.id
44
65
  assert_equal 'XD', Comment.first.title
66
+ end
67
+
68
+ def test_mapping_all
69
+ test_create_comment # for fixtures
70
+ model, local_dm = create_fake_model
71
+ model.storage_names[:default] = 'dmm_test_comments'
72
+
73
+ assert_equal ['dmm_test_comments', 'dmm_test_users'], local_dm.storages.sort
74
+ assert_equal 'dmm_test_comments', model.storage_name
75
+
76
+ assert_equal 1, model.count
77
+ assert_equal comment_fields, model.fields.sort
45
78
 
46
- #
79
+ model.send :mapping
80
+ assert_equal 'XD', model.first.title
81
+ assert_equal 1, model.first.id
82
+ end
83
+
84
+ def test_mapping_and_create
85
+ model, local_dm = create_fake_model
86
+ model.storage_names[:default] = 'dmm_test_comments'
87
+ model.send :mapping
88
+
89
+ model.create(:title => 'orz')
90
+ assert_equal 'orz', model.first.title
91
+ assert_equal 1, model.first.id
92
+
93
+ model.create
94
+ assert_equal 'default title', model.get(2).title
95
+ end
96
+
97
+ def test_storages_and_fields
98
+ assert_equal user_fields, dm.fields('dmm_test_users').sort
99
+ assert_equal( {'dmm_test_users' => user_fields, 'dmm_test_comments' => comment_fields},
100
+ dm.storages_and_fields.inject({}){ |r, i|
101
+ key, value = i
102
+ r[key] = value.sort
103
+ r
104
+ } )
105
+ end
47
106
 
48
- Tmp.send :include, DataMapper::Resource
49
- @dm = DataMapper.setup :default, 'sqlite3:tmp.db'
50
- assert_equal ['dmm_test_comments', 'dmm_test_users'], @dm.storages.sort
107
+ def test_mapping_type
108
+ model, local_dm = create_fake_model
109
+ model.storage_names[:default] = 'dmm_test_comments'
110
+ model.send :mapping, Integer
51
111
 
52
- Tmp.storage_names[:default] = 'dmm_test_comments'
53
- assert_equal 'dmm_test_comments', Tmp.storage_name
112
+ assert_equal ['id', 'user_id'], model.properties.map(&:name).map(&:to_s).sort
113
+ end
114
+
115
+ def test_mapping_multiple
116
+ model, local_dm = create_fake_model
117
+ model.storage_names[:default] = 'dmm_test_users'
118
+ model.send :mapping, :login, Integer
119
+
120
+ assert_equal ['id', 'login'], model.properties.map(&:name).map(&:to_s).sort
121
+ end
54
122
 
55
- assert_equal 1, Tmp.count
56
- assert_equal comment_fields, Tmp.fields.sort
123
+ def test_mapping_regexp
124
+ model, local_dm = create_fake_model
125
+ model.storage_names[:default] = 'dmm_test_comments'
126
+ model.send :mapping, /id$/
57
127
 
58
- Tmp.mapping
59
- assert_equal 'XD', Tmp.first.title
60
- assert_equal 1, Tmp.first.id
128
+ assert_equal ['id', 'user_id'], model.properties.map(&:name).map(&:to_s).sort
61
129
  end
130
+
131
+ def test_invalid_argument
132
+ assert_raise(ArgumentError){
133
+ User.send :mapping, 29
134
+ }
135
+ end
136
+
62
137
  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.2.1
4
+ version: 0.3.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-03 00:00:00 +08:00
12
+ date: 2008-08-04 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: 0.9.3
24
24
  version:
25
25
  description: DataMapper plugin that helps you manipulate an existing database. It creates mappings between existing columns and model's properties.
26
26
  email: "strip any number: 18god29fat7029 (at] godfat32 -dooot- 20org"
@@ -58,6 +58,7 @@ files:
58
58
  - TODO
59
59
  - dm-mapping.gemspec
60
60
  - lib/dm-mapping.rb
61
+ - lib/dm-mapping/adapters/abstract_adapter.rb
61
62
  - lib/dm-mapping/adapters/sqlite3adapter.rb
62
63
  - lib/dm-mapping/model.rb
63
64
  - lib/dm-mapping/type_map.rb