dmapper 0.1 → 0.2
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/CHANGELOG +16 -0
- data/README.rdoc +64 -45
- data/bin/dmap +95 -73
- data/lib/dmap.rb +13 -4
- data/lib/dmap/associations/belongs_to.rb +29 -0
- data/lib/dmap/associations/core.rb +30 -0
- data/lib/dmap/associations/hasn.rb +79 -0
- data/lib/dmap/core.rb +83 -8
- data/lib/dmap/new.rb +50 -9
- data/lib/dmap/properties/core.rb +3 -6
- data/lib/dmap/validations/absence.rb +9 -0
- data/lib/dmap/validations/access.rb +58 -0
- data/lib/dmap/validations/confirmation.rb +8 -0
- data/lib/dmap/validations/core.rb +6 -42
- data/lib/dmap/validations/format.rb +5 -0
- data/lib/dmap/validations/length.rb +32 -1
- data/lib/dmap/validations/misc.rb +76 -0
- data/lib/dmap/validations/presence.rb +4 -0
- data/lib/dmap/validations/primitive.rb +4 -0
- data/lib/dmap/validations/uniqueness.rb +19 -0
- data/lib/support/ordered_hash.rb +434 -0
- metadata +27 -55
- data/lib/dmap/properties.rb +0 -36
data/lib/dmap.rb
CHANGED
@@ -32,10 +32,11 @@
|
|
32
32
|
libdir = File.dirname(__FILE__)
|
33
33
|
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
34
34
|
|
35
|
-
#
|
35
|
+
# core
|
36
36
|
require 'dmap/core'
|
37
|
+
require 'support/ordered_hash'
|
37
38
|
|
38
|
-
#
|
39
|
+
# properties
|
39
40
|
require 'dmap/properties/core'
|
40
41
|
require 'dmap/properties/boolean'
|
41
42
|
require 'dmap/properties/date' # DateTime, Date and Time
|
@@ -45,16 +46,24 @@ require 'dmap/properties/misc' # Object, Discriminator, Blob
|
|
45
46
|
require 'dmap/properties/serial'
|
46
47
|
require 'dmap/properties/string' # String and Text
|
47
48
|
|
48
|
-
#
|
49
|
+
# validations
|
49
50
|
require 'dmap/validations/core'
|
50
51
|
require 'dmap/validations/absence'
|
51
|
-
require 'dmap/validations/
|
52
|
+
require 'dmap/validations/access' # accessor, writer, and reader
|
53
|
+
require 'dmap/validations/confirmation' # confirmation and acceptance
|
52
54
|
require 'dmap/validations/format'
|
53
55
|
require 'dmap/validations/length'
|
56
|
+
require 'dmap/validations/misc' # key, required, lazy
|
54
57
|
require 'dmap/validations/method'
|
55
58
|
require 'dmap/validations/presence'
|
56
59
|
require 'dmap/validations/primitive'
|
60
|
+
require 'dmap/validations/uniqueness'
|
57
61
|
require 'dmap/validations/within'
|
58
62
|
|
63
|
+
# associations
|
64
|
+
require 'dmap/associations/core'
|
65
|
+
require 'dmap/associations/belongs_to'
|
66
|
+
require 'dmap/associations/hasn'
|
67
|
+
|
59
68
|
# Template for building new model
|
60
69
|
require 'dmap/new'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DMap
|
2
|
+
module Associations
|
3
|
+
class BelongsTo
|
4
|
+
attr_accessor :associations
|
5
|
+
|
6
|
+
def self.associations; true; end
|
7
|
+
|
8
|
+
def self.field
|
9
|
+
"belongs_to "
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.parent_name
|
13
|
+
"BelongsTo"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.is_valid?(command=nil)
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.validate(command=nil)
|
21
|
+
command = command.split('-')
|
22
|
+
command[1] = nil if command[1].nil?
|
23
|
+
command[1] = command[1].to_sym unless command[1].nil? or command[1] == "true" or command[1] == "false"
|
24
|
+
{:fields => command[0].underscore.to_sym, :key => command[1]}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
class Belongs < BelongsTo; end;
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module DMap
|
2
|
+
module Associations
|
3
|
+
attr_accessor :list
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def list
|
7
|
+
@list ||= OrderedHash.auto
|
8
|
+
end
|
9
|
+
|
10
|
+
# validates_*_of
|
11
|
+
def add(field, validation, bucket, value)
|
12
|
+
list[field][validation].store bucket, value unless value.nil?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Core
|
17
|
+
module When
|
18
|
+
def self.run(validation)
|
19
|
+
{ :when => validation.split('-').map { |x| ':' + x } }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module Fields
|
24
|
+
def self.run(validation)
|
25
|
+
{ :fields => validation.split('-') }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module DMap
|
2
|
+
module Associations
|
3
|
+
class HasN
|
4
|
+
attr_accessor :associations
|
5
|
+
|
6
|
+
def self.associations; true; end
|
7
|
+
|
8
|
+
def self.parent_name
|
9
|
+
"HasN"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.field
|
13
|
+
"has n, "
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.is_valid?(command=nil)
|
17
|
+
(!command.nil? or command)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.validate(command=nil)
|
21
|
+
command = command.split('-')
|
22
|
+
command[1] = nil if command[1].nil?
|
23
|
+
command[1] = command[1].to_sym unless command[1].nil? or command[1] == "true" or command[1] == "false"
|
24
|
+
{:fields => command[0].underscore.to_sym, :through => command[1].to_sym}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
class Hasn < HasN; end;
|
28
|
+
|
29
|
+
class HasMany
|
30
|
+
attr_accessor :associations
|
31
|
+
|
32
|
+
def self.associations; true; end
|
33
|
+
|
34
|
+
def self.parent_name
|
35
|
+
"HasMany"
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.field
|
39
|
+
"has n, "
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.is_valid?(command=nil)
|
43
|
+
(!command.nil? or command)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.validate(command=nil)
|
47
|
+
command = command.split('-')
|
48
|
+
command[1] = nil if command[1].nil?
|
49
|
+
{:fields => command[0].underscore.to_sym, :through => command[1]}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
class Hasmany < HasMany; end;
|
53
|
+
|
54
|
+
class Has1
|
55
|
+
attr_accessor :associations
|
56
|
+
|
57
|
+
def self.associations; true; end
|
58
|
+
|
59
|
+
def self.field
|
60
|
+
"has 1, "
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.parent_name
|
64
|
+
"Has1"
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.is_valid?(command=nil)
|
68
|
+
(!command.nil? or command or command.is_a? Symbol)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.validate(command=nil)
|
72
|
+
command = command.split('-')
|
73
|
+
command[1] = nil if command[1].nil?
|
74
|
+
{:fields => command[0].underscore.to_sym, :through => command[1].to_sym}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
class Hasone < Has1; end;
|
78
|
+
end
|
79
|
+
end
|
data/lib/dmap/core.rb
CHANGED
@@ -13,24 +13,99 @@ class String
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
#DMap Core functions + Table and Fields
|
16
|
+
# DMap Core functions + Table and Fields
|
17
17
|
module DMap
|
18
18
|
# List available commands here
|
19
19
|
class Commands
|
20
|
+
attr_accessor :options
|
21
|
+
|
20
22
|
def self.exists?(command)
|
21
|
-
|
23
|
+
self.respond_to? "cmd_" + command
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.options
|
27
|
+
@options ||= OrderedHash.new
|
28
|
+
end
|
29
|
+
|
30
|
+
# Each command has a cmd_ prefix
|
31
|
+
def self.cmd_new
|
32
|
+
validations = DMap::Validations.list
|
33
|
+
associations = DMap::Associations.list
|
34
|
+
DMap::Tables.list.each do |k, table|
|
35
|
+
properties = DMap::Properties.list[table]
|
36
|
+
obj = $new_block.result(binding)
|
37
|
+
|
38
|
+
if DMap::Commands.options['test'].nil? or DMap::Commands.options['test'] == false
|
39
|
+
handler = File.new(table + ".rb", "w")
|
40
|
+
handler.write(obj)
|
41
|
+
handler.close
|
42
|
+
p table.camelcase + " saved successfully"
|
43
|
+
end
|
44
|
+
|
45
|
+
if DMap::Commands.options['verbose'] == true
|
46
|
+
puts obj
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Rendering options since command-line is all string
|
53
|
+
class Render
|
54
|
+
def self.run(command, value)
|
55
|
+
value = Hash[value.class.name, value] unless value.is_a?(Hash)
|
56
|
+
if command.match(/^:/).nil?
|
57
|
+
command = ':' + command
|
58
|
+
end
|
59
|
+
|
60
|
+
value.each do |key, val|
|
61
|
+
next if val.nil?
|
62
|
+
temp = val.to_s.split(' ')
|
63
|
+
match = val.match(/^\[(.*?)\]$/) if val.is_a? String
|
64
|
+
if temp.length > 1 and match.class.name != "MatchData"
|
65
|
+
val = '"' + temp.join(' ').strip + '"'
|
66
|
+
end
|
67
|
+
|
68
|
+
if val.class.name == "Array"
|
69
|
+
val.flatten!
|
70
|
+
val = '[' + val.join(', ').to_s + ']'
|
71
|
+
end
|
72
|
+
|
73
|
+
next if val.nil?
|
74
|
+
case command
|
75
|
+
when ":within"
|
76
|
+
temp = val.split('..')
|
77
|
+
[':' + command, Range.new(temp[0], temp[1])]
|
78
|
+
else
|
79
|
+
[command.to_s, val]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class Helpers
|
86
|
+
def self.run(command)
|
87
|
+
return command unless command.is_a? String
|
88
|
+
|
89
|
+
case command.downcase
|
90
|
+
when "time.now"
|
91
|
+
"proc => { Time.now }"
|
92
|
+
when "datetime.now"
|
93
|
+
"proc => { DateTime.now }"
|
94
|
+
else
|
95
|
+
command
|
96
|
+
end
|
22
97
|
end
|
23
98
|
end
|
24
99
|
|
25
100
|
class Fields
|
26
101
|
attr_accessor :list
|
27
|
-
|
102
|
+
|
28
103
|
def self.list
|
29
|
-
@list ||=
|
104
|
+
@list ||= OrderedHash.new_by
|
30
105
|
end
|
31
106
|
|
32
107
|
def self.add(field, property=nil)
|
33
|
-
list.
|
108
|
+
list.push field, property unless property.nil?
|
34
109
|
end
|
35
110
|
end
|
36
111
|
|
@@ -42,12 +117,12 @@ module DMap
|
|
42
117
|
end
|
43
118
|
|
44
119
|
def self.list
|
45
|
-
@list ||=
|
120
|
+
@list ||= OrderedHash.new_by
|
46
121
|
end
|
47
122
|
|
48
123
|
def self.add(key, value=nil)
|
49
|
-
list[key] = {} if list[key].nil?
|
50
|
-
list.
|
124
|
+
#list[key] = {} if list[key].nil?
|
125
|
+
list.push key, value unless value.nil?
|
51
126
|
end
|
52
127
|
end
|
53
128
|
end
|
data/lib/dmap/new.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
$new_block = ERB.new <<-EOF
|
2
|
-
class <%= table
|
2
|
+
class <%= table.camelcase %>
|
3
3
|
include DataMapper::Resource
|
4
4
|
<%
|
5
5
|
$str = ''
|
@@ -7,9 +7,23 @@ class <%= table[0] %>
|
|
7
7
|
$str += ' property :' + k
|
8
8
|
$str += ', ' + val["type"] unless val["type"].nil?
|
9
9
|
val.each do |key, v|
|
10
|
-
next if key == "type"
|
11
|
-
|
12
|
-
|
10
|
+
next if key == "type" or v.nil?
|
11
|
+
keys = DMap::Render.run key, v
|
12
|
+
keys.each do |field, value|
|
13
|
+
next if value.nil? or value == ""
|
14
|
+
|
15
|
+
$str += ', :' + key + ' => '
|
16
|
+
# Add strings around values that need it
|
17
|
+
if value.class.name == "String" and value.split(' ').length > 1
|
18
|
+
$str += '"' + value + '"'
|
19
|
+
# Turn a string into an "Array"
|
20
|
+
elsif value.class.name == "Array"
|
21
|
+
$str += '[' + value.join(', ') + ']'
|
22
|
+
else
|
23
|
+
value = DMap::Helpers.run(value)
|
24
|
+
$str += value.to_s
|
25
|
+
end
|
26
|
+
end
|
13
27
|
end
|
14
28
|
$str += "\n"
|
15
29
|
end
|
@@ -21,17 +35,44 @@ class <%= table[0] %>
|
|
21
35
|
$str += " validates_" + key + " :" + field + ", "
|
22
36
|
v.each do |k, cmd|
|
23
37
|
next if k == :null
|
38
|
+
|
24
39
|
cmd = ":" + field if cmd == :self
|
25
|
-
cmd = '"' + cmd + '"' if cmd.is_a?
|
26
|
-
k = ":" + k.to_s if k.is_a?
|
27
|
-
cmd = ":" + cmd.to_s if cmd.is_a?
|
28
|
-
cmd = "[" + cmd.
|
40
|
+
cmd = '"' + cmd + '"' if cmd.is_a? String
|
41
|
+
k = ":" + k.to_s if k.is_a? Symbol
|
42
|
+
cmd = ":" + cmd.to_s if cmd.is_a? Symbol
|
43
|
+
cmd = "[" + cmd.join(', ') + "]" if cmd.is_a? Array
|
29
44
|
|
30
|
-
|
45
|
+
keys = DMap::Render.run k, cmd
|
46
|
+
keys.each do |index, item|
|
47
|
+
item = item.gsub(/^"/, "" ).gsub(/"$/m, "" )
|
48
|
+
if item.is_a?(String) and item.split(' ').length > 1 and item.match(/^\\[/).nil?
|
49
|
+
item = '"' + item + '"'
|
50
|
+
end
|
51
|
+
$str += k.to_s + " => " + item + ", "
|
52
|
+
end
|
31
53
|
end
|
32
54
|
$str.slice!(-2)
|
33
55
|
$str += "\n"
|
34
56
|
end
|
57
|
+
|
58
|
+
$str += "\n" if associations.length > 0
|
59
|
+
associations.each do |field, value|
|
60
|
+
value.each do |key, v|
|
61
|
+
$str += ' ' + DMap::Associations.const_get(key.camelcase).method('field').call
|
62
|
+
$str += ':' + v[:fields].to_s + ', ' unless v[:fields].nil?
|
63
|
+
unless v[:through].empty?
|
64
|
+
$str += ':through => '
|
65
|
+
if v[:through].is_a? Symbol
|
66
|
+
$str += ':' + v[:through].to_s
|
67
|
+
else
|
68
|
+
$str += v[:through].to_s
|
69
|
+
end
|
70
|
+
$str += ', '
|
71
|
+
end
|
72
|
+
$str.slice!(-2)
|
73
|
+
$str += "\n"
|
74
|
+
end
|
75
|
+
end
|
35
76
|
end
|
36
77
|
%>
|
37
78
|
<%= $str %>
|
data/lib/dmap/properties/core.rb
CHANGED
@@ -3,26 +3,23 @@ module DMap
|
|
3
3
|
attr_accessor :list
|
4
4
|
class << self
|
5
5
|
# default methods
|
6
|
-
def
|
7
|
-
value.is_a?(Boolean)
|
6
|
+
def default
|
8
7
|
end
|
9
8
|
|
10
9
|
def valid?(klass)
|
11
10
|
begin
|
12
|
-
self.const_get(klass.capitalize).is_a?
|
11
|
+
self.const_get(klass.capitalize).is_a? Class
|
13
12
|
rescue
|
14
13
|
false
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
18
17
|
def list
|
19
|
-
@list ||=
|
18
|
+
@list ||= OrderedHash.auto
|
20
19
|
end
|
21
20
|
|
22
21
|
def add(table, property=nil, value=nil, bucket=nil)
|
23
|
-
list[table] = {} if list[table].nil?
|
24
22
|
unless property.nil?
|
25
|
-
list[table][property] = {} if list[table][property].nil?
|
26
23
|
if bucket.nil?
|
27
24
|
list[table].store property, value unless value.nil?
|
28
25
|
else
|
@@ -1,14 +1,23 @@
|
|
1
1
|
module DMap
|
2
2
|
module Validations
|
3
3
|
class AbsenceOf
|
4
|
+
# for aliases
|
4
5
|
def self.parent_name
|
5
6
|
"AbsenceOf"
|
6
7
|
end
|
7
8
|
|
9
|
+
# Return true if it's valid
|
10
|
+
def self.is_valid?(command)
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
# This is what's returned to our template
|
8
15
|
def self.validate(command=nil)
|
9
16
|
{:null => true}
|
10
17
|
end
|
11
18
|
end
|
19
|
+
|
20
|
+
# aliases
|
12
21
|
class Absence < AbsenceOf; end
|
13
22
|
class Absent < AbsenceOf; end
|
14
23
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module DMap
|
2
|
+
module Validations
|
3
|
+
class Accessor
|
4
|
+
attr_accessor :property
|
5
|
+
|
6
|
+
def self.property; true; end;
|
7
|
+
|
8
|
+
# for aliases
|
9
|
+
def self.parent_name
|
10
|
+
"Accessor"
|
11
|
+
end
|
12
|
+
|
13
|
+
# Return true if it's valid
|
14
|
+
def self.is_valid?(command)
|
15
|
+
(command == "protected" or command == "public" or command == "private")
|
16
|
+
end
|
17
|
+
|
18
|
+
# This is what's returned to our template
|
19
|
+
def self.validate(command=nil)
|
20
|
+
command ||= "protected"
|
21
|
+
":" + command
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# aliases
|
26
|
+
class Access < Accessor; end
|
27
|
+
|
28
|
+
class Writer
|
29
|
+
attr_accessor :property
|
30
|
+
|
31
|
+
def self.property; true; end;
|
32
|
+
|
33
|
+
def self.is_valid?(command)
|
34
|
+
(command == "protected" or command == "public" or command == "private")
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.validate(command=nil)
|
38
|
+
command ||= "protected"
|
39
|
+
":" + command
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Reader
|
44
|
+
attr_accessor :property
|
45
|
+
|
46
|
+
def self.property; true; end;
|
47
|
+
|
48
|
+
def self.is_valid?(command)
|
49
|
+
(command == "protected" or command == "public" or command == "private")
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.validate(command=nil)
|
53
|
+
command ||= "protected"
|
54
|
+
":" + command
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|