kwatable 0.1.0 → 0.2.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.
Files changed (41) hide show
  1. data/ChangeLog.txt +15 -3
  2. data/README.txt +1 -1
  3. data/bin/kwatable +3 -9
  4. data/examples/ex1/Makefile +18 -26
  5. data/examples/ex1/example1.yaml +9 -6
  6. data/examples/ex2/Makefile +15 -24
  7. data/kwatable.gemspec +2 -2
  8. data/lib/kwatable.rb +1 -1
  9. data/lib/kwatable/error-msg.rb +1 -1
  10. data/lib/kwatable/kwatable.schema.yaml +12 -2
  11. data/lib/kwatable/main-program.rb +18 -7
  12. data/lib/kwatable/manipulator.rb +3 -3
  13. data/lib/kwatable/templates/ddl-mysql.eruby +1 -1
  14. data/lib/kwatable/templates/ddl-postgresql.eruby +3 -3
  15. data/lib/kwatable/templates/defaults.yaml +1 -1
  16. data/lib/kwatable/templates/dto-java.eruby +75 -9
  17. data/lib/kwatable/templates/dto-ruby.eruby +27 -15
  18. data/lib/kwatable/templates/validator-ruby.eruby +97 -0
  19. data/test/assert-diff.rb +1 -1
  20. data/test/test.rb +80 -151
  21. metadata +4 -25
  22. data/test/test1/test1.ddl-mysql.expected +0 -23
  23. data/test/test1/test1.ddl-postgresql.expected +0 -23
  24. data/test/test1/test1.dto-java.Group.expected +0 -35
  25. data/test/test1/test1.dto-java.User.expected +0 -57
  26. data/test/test1/test1.dto-ruby.Group.expected +0 -20
  27. data/test/test1/test1.dto-ruby.User.expected +0 -33
  28. data/test/test1/test1.yaml +0 -96
  29. data/test/test2/test2.ddl-mysql.expected +0 -49
  30. data/test/test2/test2.ddl-postgresql.expected +0 -49
  31. data/test/test2/test2.dto-java.Address.expected +0 -40
  32. data/test/test2/test2.dto-java.Customer.expected +0 -47
  33. data/test/test2/test2.dto-java.Item.expected +0 -35
  34. data/test/test2/test2.dto-java.SalesOrder.expected +0 -48
  35. data/test/test2/test2.dto-java.SalesOrderLine.expected +0 -54
  36. data/test/test2/test2.dto-ruby.Address.expected +0 -22
  37. data/test/test2/test2.dto-ruby.Customer.expected +0 -29
  38. data/test/test2/test2.dto-ruby.Item.expected +0 -20
  39. data/test/test2/test2.dto-ruby.SalesOrder.expected +0 -29
  40. data/test/test2/test2.dto-ruby.SalesOrderLine.expected +0 -36
  41. data/test/test2/test2.yaml +0 -94
@@ -4,13 +4,14 @@
4
4
  ## kwatable template file for Ruby DTO class
5
5
  ##
6
6
  ## copyright(c) 2005 kuwata-lab.com all rights reserved.
7
- ## $Release: 0.1.0 $
8
- ## $Rev: 15 $
7
+ ## $Release: 0.2.0 $
8
+ ## $Rev: 17 $
9
9
  ##
10
10
  ## template properties:
11
- ## module - module name
12
- ## parent - parent class
13
- ## reqpath - require path
11
+ ## --module=name : module name
12
+ ## --parent=name : parent class
13
+ ## --reqpath=path : require path
14
+ ## --symbolkey : use symbol as hash key
14
15
  ##
15
16
 
16
17
 
@@ -100,11 +101,15 @@
100
101
  #
101
102
  # require other dto file
102
103
  #
104
+ refclass_table = {}
103
105
  for column in table['columns']
104
106
  if column['ref']
105
107
  refclass = column['ref']['table']['class']
106
108
  refclass ||= camel_case(column['ref']['table']['name'])
107
109
  reqpath = properties[:reqpath]
110
+ next if refclass_table[refclass]
111
+ refclass_table[refclass] = true
112
+
108
113
  %>
109
114
  require '<%= reqpath %><%= reqpath ? '/' : '' %><%= refclass %>'
110
115
  <%
@@ -118,31 +123,38 @@ require '<%= reqpath %><%= reqpath ? '/' : '' %><%= refclass %>'
118
123
  #
119
124
  mod = klass[:module] ? "#{klass[:module]}::" : ""
120
125
  extends = klass[:parent] ? " < #{klass[:parent]}" : ""
126
+
121
127
  %>
122
128
  ## <%= klass[:desc] %>
123
129
 
124
- class <%= mod %><%= klass[:name] %><% extends %>
130
+ class <%= mod %><%= klass[:name] %><%= extends %>
131
+
125
132
 
126
133
  <%
127
134
  #
128
- # populator with hash
135
+ # accessor
129
136
  #
130
- %>
131
- def populate(values={})
137
+ %>
132
138
  <% for var in variables %>
133
- self.<%= "%-14s" % var[:name] %> = values['<%= var[:name] %>']
139
+ attr_accessor :<%= '%-14s' % var[:name] %> # <%= var[:desc] %>
140
+
134
141
  <% end %>
135
- end
136
142
 
137
143
  <%
138
144
  #
139
- # accessor
145
+ # set all values
140
146
  #
141
- %>
147
+ %>
148
+ def populate(_hash={})
149
+ <% flag_symbol = properties[:keytype] == 'symbol' %>
142
150
  <% for var in variables %>
143
- attr_accessor :<%= '%-14s' % var[:name] %> # <%= var[:desc] %>
144
-
151
+ <% if flag_symbol %>
152
+ @<%= "%-14s" % var[:name] %> = _hash[:<%= var[:name] %>]
153
+ <% else %>
154
+ @<%= "%-14s" % var[:name] %> = _hash['<%= var[:name] %>']
155
+ <% end %>
145
156
  <% end %>
157
+ end
146
158
 
147
159
  <%
148
160
  #
@@ -0,0 +1,97 @@
1
+ <%
2
+
3
+ ##
4
+ ## kwatable template file for Ruby Validator
5
+ ##
6
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
7
+ ## $Release: 0.2.0 $
8
+ ## $Rev$
9
+ ##
10
+ ## template properties:
11
+ ## module - module/class name
12
+ ##
13
+
14
+ #
15
+ # context variables
16
+ #
17
+ tables = context['tables']
18
+ properties = context['properties']
19
+
20
+ #
21
+ # constraint keys
22
+ #
23
+ constraint_keys = %w[required with max-length min-length enum pattern]
24
+
25
+ %>
26
+ ##
27
+ ## validator for Ruby
28
+ ## generated by kwatable with template 'validator-ruby.eruby'
29
+ ## at <%= Time.now.to_s %>
30
+
31
+ ##
32
+ <% if properties[:module] %>
33
+
34
+ module <%= properties[:module] %>
35
+
36
+ <% end %>
37
+ <% for table in tables %>
38
+
39
+ ## <%= table['name'] %> - <%= table['desc'] %>
40
+
41
+ def validate_<%= table['name'] %>(values)
42
+ errors = []
43
+ <% for column in table['columns'] %>
44
+ <% colname = column['name'] %>
45
+ <% has_constraints = ! (column.keys & constraint_keys).empty? %>
46
+
47
+ ## <%= table['name'] %>.<%= column['name'] %>
48
+
49
+ <% next if column['serial'] || column['auto'] %>
50
+ <% if column['strip'] != false || has_constraints %>
51
+ val = values[:<%= colname %>]
52
+ <% end %>
53
+ <% if column['strip'] != false %>
54
+ val.strip! if val
55
+ <% end %>
56
+ <% next unless has_constraints %>
57
+ if val == nil || val.empty?
58
+ <% if column['required'] %>
59
+ errors << [:<%= colname %>, :required]
60
+ <% else %>
61
+ # nothing
62
+ <% end %>
63
+ <% if (column['max-length'] || column['width']) && !column['enum'] %>
64
+ elsif val.length > <%= column['max-length'] || column['width'] %>
65
+
66
+ errors << [:<%= colname %>, :max_length]
67
+ <% end %>
68
+ <% if column['min-length'] && !column['width'] %>
69
+ elsif val.length < <%= column['max-length'] %>
70
+
71
+ errors << [:<%= colname %>, :min_length]
72
+ <% end %>
73
+ <% if column['enum'] %>
74
+ <% enum = column['enum'].find_all { |e| e != nil } %>
75
+ <% if enum.length == 1 %>
76
+ elsif val != <%= enum.first.inspect %>
77
+
78
+ <% else %>
79
+ elsif ! <%= column['enum'].inspect %>.include?(val)
80
+ <% end %>
81
+ errors << [:<%= colname %>, :enum]
82
+ <% end %>
83
+ <% if pat = column['pattern'] %>
84
+ elsif val !~ <%= pat.is_a?(Regexp) ? pat.intern : pat %>
85
+
86
+ errors << [:<%= colname %>, :pattern]
87
+ <% end %>
88
+ end
89
+ <% end ## for column %>
90
+
91
+ return errors
92
+ end
93
+ <% end ## for table %>
94
+ <% if properties[:module] %>
95
+
96
+ end
97
+ <% end %>
@@ -1,6 +1,6 @@
1
1
  ###
2
2
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- ### $Release: 0.1.0 $
3
+ ### $Release: 0.2.0 $
4
4
  ### $Rev: 10 $
5
5
  ###
6
6
 
@@ -1,7 +1,7 @@
1
1
  ###
2
2
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- ### $Release: 0.1.0 $
4
- ### $Rev: 15 $
3
+ ### $Release: 0.2.0 $
4
+ ### $Rev: 17 $
5
5
  ###
6
6
 
7
7
  unless defined?(BASE_DIR)
@@ -11,7 +11,7 @@ unless defined?(BASE_DIR)
11
11
  BIN_DIR = BASE_DIR + "/bin"
12
12
  EXAMPLES_DIR = BASE_DIR + "/examples"
13
13
  $LOAD_PATH << LIB_DIR << TEST_DIR
14
-
14
+
15
15
  ENV['RUBYLIB'] = LIB_DIR
16
16
  ENV['PATH'] = "#{BIN_DIR}:#{ENV['PATH']}"
17
17
  #puts ENV['PATH']
@@ -29,181 +29,110 @@ require 'kwatable/main-program'
29
29
  require 'assert-diff'
30
30
 
31
31
 
32
- class KwatableExampleTest < Test::Unit::TestCase
33
-
34
- def _test(options={})
35
- optstr = "#{@option}"
36
- if options[:multiple] # or if @classnames
37
- outdir = prefix()
38
- optstr << " -smd #{outdir}"
39
- end
40
- cmd = "kwatable -f #{@template} #{optstr} #{@datafile}"
41
- #cmd = "kwatable -I #{BASE_DIR}/templates -f #{@template} #{optstr} #{@datafile}"
42
- $logger.write("- command: #{cmd}\n thread: #{Thread.current.__id__}\n timestamp: #{Time.new.to_s}\n")
43
- #actual = `#{cmd}`
44
- argv = cmd.split(/\s+/)
45
- argv.shift
46
- main = Kwatable::MainProgram.new(argv)
47
- actual = main.execute()
48
-
49
- if @classnames
50
- actuals = {}
51
- @classnames.each do |classname|
52
- actual = File.read(@output % classname)
53
- File.unlink(@output % classname)
54
- if options[:ignore_linenum]
55
- actual = _ignore_line(actual, options[:ignore_linenum])
56
- end
57
- actuals[classname] = actual
58
- File.open(@actual % classname, 'w') { |f| f.write(actual) }
59
- end
60
- @classnames.each do |classname|
61
- actual = actuals[classname]
62
- expected = File.read(@expected % classname)
63
- assert_equal_with_diff(expected, actual)
64
- end
65
- else
66
- if options[:ignore_linenum]
67
- actual = _ignore_line(actual, options[:ignore_linenum])
68
- end
69
- File.open(@actual, 'w') { |f| f.write(actual) }
70
- expected = File.open(@expected) { |f| f.read() }
71
- assert_equal_with_diff(expected, actual)
72
- end
73
- end
74
32
 
75
- def prefix
76
- raise NotImplementedError.new("#{self.class.name}#prefix() is not implemented.")
77
- end
33
+ DATE_PATTERN = /^.*?at [\w ]+ \d\d:\d\d:\d\d [A-Z]{3} \d\d\d\d\s*$/
78
34
 
79
- def classnames
80
- raise NotImplementedError.new("#{self.class.name}#classnames() is not implemented.")
81
- end
35
+ class ExampleTest < Test::Unit::TestCase
82
36
 
83
37
 
84
- def _ignore_line(str, linenum)
85
- s = ''
86
- i = 0
87
- str.each_line do |line|
88
- i += 1
89
- s << line if i != linenum
38
+ def _test
39
+ #Dir.chdir TEST_DIR
40
+ Dir.chdir @dir
41
+ argv = ["-f", @template]
42
+ argv.concat @options if @options
43
+ argv << @datafile
44
+ main = Kwatable::MainProgram.new(argv)
45
+ output = main.execute()
46
+ @expfiles.each do |expfile|
47
+ expected = File.read(".expected/#{expfile}")
48
+ actual = output ? output.dup : File.read(expfile)
49
+ actual.sub!(DATE_PATTERN, '')
50
+ expected.sub!(DATE_PATTERN, '')
51
+ assert_equal_with_diff(expected, actual)
90
52
  end
91
- return s
92
53
  end
93
54
 
94
- def _setup_ddl(basename)
95
- dir = prefix()
96
- @datafile = "#{dir}/#{prefix()}.yaml"
97
- @template = "#{basename}.eruby"
98
- @actual = "#{dir}/#{prefix()}.#{basename}.actual"
99
- @expected = "#{dir}/#{prefix()}.#{basename}.expected"
100
- end
101
55
 
102
- def _setup_dto(basename, suffix)
103
- dir = prefix()
104
- @datafile = "#{dir}/#{prefix()}.yaml"
105
- @template = "#{basename}.eruby"
106
- @actual = "#{dir}/#{prefix()}.#{basename}.%s.actual"
107
- @expected = "#{dir}/#{prefix()}.#{basename}.%s.expected"
108
- @classnames = classnames()
109
- @output = "#{dir}/%s#{suffix}"
56
+ def test_ex1_mysql
57
+ @dir = "#{EXAMPLES_DIR}/ex1"
58
+ @template = 'ddl-mysql.eruby'
59
+ @datafile = 'example1.yaml'
60
+ @expfiles = %w[ddl-mysql.sql]
61
+ _test
110
62
  end
111
63
 
112
- end
113
-
114
64
 
115
- class KwatableExample1Test < KwatableExampleTest
116
-
117
- def prefix
118
- return 'test1'
119
- end
120
-
121
- def classnames
122
- return %w[Group User]
65
+ def test_ex1_postgresql
66
+ @dir = "#{EXAMPLES_DIR}/ex1"
67
+ @template = 'ddl-postgresql.eruby'
68
+ @datafile = 'example1.yaml'
69
+ @expfiles = %w[ddl-postgresql.sql]
70
+ _test
123
71
  end
124
72
 
125
73
 
126
- def test1_ddl_mysql
127
- #@datafile = "test1.yaml"
128
- #@template = "ddl-mysql.eruby"
129
- #@actual = "test1.ddl-mysql.actual"
130
- #@expected = "test1.ddl-mysql.expected"
131
- _setup_ddl('ddl-mysql')
132
- _test(:ignore_linenum=>4)
133
- end
134
-
135
- def test1_ddl_postgresql
136
- #@datafile = "test1.yaml"
137
- #@template = "ddl-postgresql.eruby"
138
- #@actual = "test1.ddl-postgresql.actual"
139
- #@expected = "test1.ddl-postgresql.expected"
140
- _setup_ddl('ddl-postgresql')
141
- _test(:ignore_linenum=>4)
142
- end
143
-
144
- def test1_dto_java
145
- #@datafile = "test1.yaml"
146
- #@template = "dto-java.eruby"
147
- #@actual = "test1.dto-java.%s.actual"
148
- #@expected = "test1.dto-java.%s.expected"
149
- #@classnames = dto_classes()
150
- #@output = "%s.java"
151
- _setup_dto('dto-java', '.java')
152
- _test(:ignore_linenum=>4, :multiple=>true)
153
- end
154
-
155
- def test1_dto_ruby
156
- #@datafile = "#test1.yaml"
157
- #@template = "dto-ruby.eruby"
158
- #@actual = "test1.dto-ruby.%s.actual"
159
- #@expected = "test1.dto-ruby.%s.expected"
160
- #@classnames = dto_classes()
161
- #@output = "%s.rb"
162
- _setup_dto('dto-ruby', '.rb')
163
- _test(:ignore_linenum=>4, :multiple=>true)
74
+ def test_ex1_java
75
+ @dir = "#{EXAMPLES_DIR}/ex1"
76
+ @template = 'dto-java.eruby'
77
+ @datafile = 'example1.yaml'
78
+ @expfiles = %w[Group.java User.java]
79
+ @options = %w[-qm --package=my.example --parent=Object --populate --constructor]
80
+ begin
81
+ _test
82
+ ensure
83
+ @expfiles.each { |filename| File.unlink(filename) }
84
+ end
164
85
  end
165
86
 
166
- end
167
-
168
-
169
- class KwatableExample2Test < KwatableExampleTest
170
87
 
171
- def prefix
172
- return 'test2'
88
+ def test_ex1_ruby
89
+ @dir = "#{EXAMPLES_DIR}/ex1"
90
+ @template = 'dto-ruby.eruby'
91
+ @datafile = 'example1.yaml'
92
+ @expfiles = %w[Group.rb User.rb]
93
+ @options = %w[-qm --module=Example --parent=Object --reqpath=lib --populate --symbolkey]
94
+ begin
95
+ _test
96
+ ensure
97
+ @expfiles.each { |filename| File.unlink(filename) }
98
+ end
173
99
  end
100
+
101
+
102
+ ##---
103
+
174
104
 
175
- def classnames
176
- return %w[Address Customer Item SalesOrder SalesOrderLine]
105
+ def test_ex2_mysql
106
+ @dir = "#{EXAMPLES_DIR}/ex2"
107
+ @template = 'ddl-mysql.eruby'
108
+ @datafile = 'example2.yaml'
109
+ @expfiles = %w[ddl-mysql.sql]
110
+ _test
177
111
  end
178
112
 
179
113
 
180
- def test2_ddl_mysql
181
- _setup_ddl('ddl-mysql')
182
- _test(:ignore_linenum=>4)
114
+ def test_ex2_postgresql
115
+ @dir = "#{EXAMPLES_DIR}/ex2"
116
+ @template = 'ddl-postgresql.eruby'
117
+ @datafile = 'example2.yaml'
118
+ @expfiles = %w[ddl-postgresql.sql]
119
+ _test
183
120
  end
184
121
 
185
- def test2_ddl_postgresql
186
- _setup_ddl('ddl-postgresql')
187
- _test(:ignore_linenum=>4)
188
- end
189
122
 
190
- def test2_dto_java
191
- _setup_dto('dto-java', '.java')
192
- _test(:ignore_linenum=>4, :multiple=>true)
123
+ def test_ex2_java
124
+ @dir = "#{EXAMPLES_DIR}/ex2"
125
+ @template = 'dto-java.eruby'
126
+ @datafile = 'example2.yaml'
127
+ @expfiles = %w[Address.java Customer.java Item.java SalesOrder.java SalesOrderLine.java]
128
+ @options = %w[-qm --package=my.example --parent=Object --populate --constructor]
129
+ begin
130
+ _test
131
+ ensure
132
+ @expfiles.each { |filename| File.unlink(filename) if test(?f, filename)}
133
+ end
193
134
  end
194
135
 
195
- def test2_dto_ruby
196
- _setup_dto('dto-ruby', '.rb')
197
- _test(:ignore_linenum=>4, :multiple=>true)
198
- end
199
136
 
200
137
  end
201
138
 
202
-
203
- File.open('test.log', 'a') do |f|
204
- $logger = f
205
- suite = Test::Unit::TestSuite.new()
206
- suite << KwatableExample1Test.suite()
207
- suite << KwatableExample2Test.suite()
208
- Test::Unit::UI::Console::TestRunner.run(suite)
209
- end