kwatable 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/ChangeLog +46 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.txt +4 -2
  4. data/bin/kwatable +4 -4
  5. data/examples/ex1/Makefile +40 -14
  6. data/examples/ex1/{example1.yaml → tabledef.yaml} +42 -11
  7. data/examples/ex2/Makefile +41 -14
  8. data/examples/ex2/{example2.yaml → tabledef.yaml} +45 -30
  9. data/examples/ex3/Makefile +52 -0
  10. data/examples/ex3/tabledef.yaml +136 -0
  11. data/kwatable.gemspec +11 -10
  12. data/lib/kwatable.rb +24 -18
  13. data/lib/kwatable/kwatable.schema.yaml +95 -5
  14. data/lib/kwatable/main.rb +331 -0
  15. data/lib/kwatable/manipulator.rb +320 -192
  16. data/lib/kwatable/messages.rb +59 -0
  17. data/lib/kwatable/template/ddl-mysql.eruby +202 -0
  18. data/lib/kwatable/{templates → template}/ddl-postgresql.eruby +71 -45
  19. data/lib/kwatable/{templates → template}/defaults.yaml +2 -2
  20. data/lib/kwatable/template/dictionary.en.yaml +70 -0
  21. data/lib/kwatable/template/dictionary.ja.yaml +165 -0
  22. data/lib/kwatable/template/dto-java.eruby +77 -0
  23. data/lib/kwatable/template/dto-java.sub.eruby +259 -0
  24. data/lib/kwatable/template/dto-ruby.eruby +63 -0
  25. data/lib/kwatable/template/dto-ruby.sub.eruby +213 -0
  26. data/lib/kwatable/template/helper/column.rb +70 -0
  27. data/lib/kwatable/template/helper/common.rb +151 -0
  28. data/lib/kwatable/template/helper/java.rb +83 -0
  29. data/lib/kwatable/template/helper/label.rb +90 -0
  30. data/lib/kwatable/template/helper/ruby.rb +36 -0
  31. data/lib/kwatable/template/helper/table.rb +62 -0
  32. data/lib/kwatable/template/hibernate.eruby +139 -0
  33. data/lib/kwatable/template/rails-controller.eruby +66 -0
  34. data/lib/kwatable/template/rails-controller.sub.eruby +114 -0
  35. data/lib/kwatable/template/rails-kwartz.eruby +164 -0
  36. data/lib/kwatable/template/rails-kwartz/_attr.plogic.eruby +56 -0
  37. data/lib/kwatable/template/rails-kwartz/_form.plogic.eruby +81 -0
  38. data/lib/kwatable/template/rails-kwartz/_link.plogic.eruby +36 -0
  39. data/lib/kwatable/template/rails-kwartz/edit.cfg.yaml.eruby +16 -0
  40. data/lib/kwatable/template/rails-kwartz/edit.html.eruby +46 -0
  41. data/lib/kwatable/template/rails-kwartz/edit.plogic.eruby +20 -0
  42. data/lib/kwatable/template/rails-kwartz/layout.html.eruby +39 -0
  43. data/lib/kwatable/template/rails-kwartz/layout.plogic.eruby +32 -0
  44. data/lib/kwatable/template/rails-kwartz/list.html.eruby +94 -0
  45. data/lib/kwatable/template/rails-kwartz/list.plogic.eruby +41 -0
  46. data/lib/kwatable/template/rails-kwartz/new.html.eruby +100 -0
  47. data/lib/kwatable/template/rails-kwartz/new.plogic.eruby +26 -0
  48. data/lib/kwatable/template/rails-kwartz/show.html.eruby +51 -0
  49. data/lib/kwatable/template/rails-kwartz/show.plogic.eruby +9 -0
  50. data/lib/kwatable/template/rails-model.eruby +35 -0
  51. data/lib/kwatable/template/rails-model.sub.eruby +136 -0
  52. data/lib/kwatable/{templates → template}/validator-ruby.eruby +18 -11
  53. data/lib/kwatable/util.rb +133 -0
  54. data/lib/kwatable/util/assert-text-equal.rb +47 -0
  55. data/lib/kwatable/util/assertion.rb +115 -0
  56. data/lib/kwatable/validator.rb +50 -0
  57. data/test/assert-diff.rb +1 -1
  58. data/test/test-ex.rb +306 -0
  59. data/test/test.rb +37 -127
  60. metadata +66 -17
  61. data/COPYING +0 -340
  62. data/ChangeLog.txt +0 -65
  63. data/lib/kwatable/error-msg.rb +0 -38
  64. data/lib/kwatable/main-program.rb +0 -216
  65. data/lib/kwatable/templates/ddl-mysql.eruby +0 -172
  66. data/lib/kwatable/templates/dto-java.eruby +0 -260
  67. data/lib/kwatable/templates/dto-ruby.eruby +0 -185
@@ -0,0 +1,83 @@
1
+ ##
2
+ ## $Rev: 44 $
3
+ ## $Release: 0.3.0 $
4
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
5
+ ##
6
+
7
+ module Kwatable
8
+
9
+ module JavaHelper
10
+
11
+
12
+ ## java keywords
13
+ def self.java_keywords()
14
+ keywords = <<-END
15
+ abstract assert boolean break byte case catch char class const
16
+ continue default do double else enum extends final finally float
17
+ for goto if implements import instanceof int interface long
18
+ native new package private protected public return short
19
+ static strictfp super switch synchronized this throw throws
20
+ transient try void volatile while
21
+ END
22
+ return keywords.split(/\s+/)
23
+ end
24
+
25
+ @@java_keyword_table = java_keywords().inject({}) { |h, w| h[w] = true; h }
26
+
27
+
28
+ ## escape java keyword (ex. 'case' => '_case')
29
+ def escape_java_keyword(word)
30
+ return @@java_keyword_table[word] ? "_#{word}" : word
31
+ end
32
+
33
+
34
+ ## determine java class or type from column data
35
+ def column_java_type(column, flag_package=false)
36
+ type = column['type']
37
+ width = column['width']
38
+ case type
39
+ when 'char' ; type = (!width || width == 1) ? 'char' : 'String'
40
+ when 'short' ;
41
+ when 'int', 'integer' ; type = 'int'
42
+ when 'str', 'string' ; type = width == 1 ? 'char' : 'String'
43
+ when 'text' ; type = width == 1 ? 'char' : 'String'
44
+ when 'float' ;
45
+ when 'double' ;
46
+ when 'bool', 'boolean' ; type = 'boolean'
47
+ when 'date' ; type = 'Date'
48
+ when 'timestamp' ; type = 'Date'
49
+ when 'money' ; type = 'double'
50
+ end
51
+ if flag_package
52
+ case type
53
+ when 'String': type = 'java.lang.String'
54
+ when 'Date': type = 'java.util.Date'
55
+ end
56
+ end
57
+ return type
58
+ end
59
+
60
+
61
+ ## return class name of table with package
62
+ def table_java_class(table, package)
63
+ class_name = table_class(table)
64
+ return package ? "#{package}.#{class_name}" : class_name
65
+ end
66
+
67
+
68
+ ## return accessor names
69
+ ##
70
+ ## ex.
71
+ ## getter, setter = accessors("user_name", "String") #=> ["getUserName", "setUserName"]
72
+ ## getter, setter = accessors("expired", "boolean") #=> ["isExpired", "setExpired"]
73
+ def accessors(attr_name, attr_type)
74
+ base = camel_case(attr_name)
75
+ getter = "#{attr_type == 'boolean' ? 'is' : 'get'}#{base}"
76
+ setter = "set#{base}"
77
+ return getter, setter
78
+ end
79
+
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,90 @@
1
+ ##
2
+ ## $Rev: 39 $
3
+ ## $Release: 0.3.0 $
4
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
5
+ ##
6
+
7
+
8
+ module Kwatable
9
+
10
+ module LabelHelper
11
+
12
+ #
13
+ def load_dictionary_files(option_path_list=nil)
14
+ path_list = []
15
+ option_path_list ||= @options[?I].split(/,/) if @options[?I]
16
+ path_list += option_path_list if option_path_list
17
+ path_list += Kwatable.template_path if Kwatable.template_path
18
+ #
19
+ lang = @properties[:lang] || 'en'
20
+ dict_names = ['dictionary']
21
+ dict_names += @properties[:dict].split(/,/) if @properties[:dict]
22
+ filenames = []
23
+ dict_names.each do |dict_name|
24
+ fname = find_file("#{dict_name}.#{lang}.yaml", path_list)
25
+ fname ||= find_file("#{dict_name}.en.yaml", path_list) unless lang == 'en'
26
+ filenames << fname if fname
27
+ end
28
+ #
29
+ dictionary = {}
30
+ filenames.each do |filename|
31
+ hash = YAML.load_file(filename)
32
+ raise "#{filename}: mapping is required." unless hash.is_a?(Hash)
33
+ dictionary.update(hash)
34
+ end
35
+ return dictionary
36
+ end
37
+
38
+ # word
39
+ def w(key, *args)
40
+ return _w(key, true, *args)
41
+ end
42
+
43
+ #
44
+ def w!(key, *args)
45
+ return _w(key, false, *args)
46
+ end
47
+
48
+ #
49
+ def _w(key, error_when_not_found=false, *args)
50
+ @_dict ||= load_dictionary_files()
51
+ word = @_dict[key.to_s]
52
+ unless word
53
+ if error_when_not_found
54
+ err = StandardError.new("word key '#{key.inspect}' is not found. ")
55
+ err.set_backtrace(caller())
56
+ raise err
57
+ else
58
+ return nil
59
+ end
60
+ end
61
+ return args && !args.empty? ? word % args : word
62
+ end
63
+
64
+ # 'foo' => 'Foo', 'foo_bar_baz' => 'Foo bar baz'
65
+ def to_label(key)
66
+ word = w!(key) || key.to_s
67
+ return word =~ /\A\w+\z/ ? word.capitalize.split(/_/).join(' ') : word
68
+ end
69
+
70
+ # ex.
71
+ # colname = column['name']
72
+ # column['enum'].each do |enum_item|
73
+ # value, label = enum_value_and_label(enum_item, colname)
74
+ # p value, label
75
+ # end
76
+ def enum_value_and_label(enum_item, column_name)
77
+ item = enum_item
78
+ #return item.is_a?(Hash) ? [item['value'], item['label']] : [item, camel_case(item)]
79
+ if item.is_a?(Hash)
80
+ return item['value'], item['label']
81
+ else
82
+ key = "#{column_name}_#{item}"
83
+ label = _w(key) ? to_label(key) : to_label(item)
84
+ return item, label
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,36 @@
1
+ ##
2
+ ## $Rev: 36 $
3
+ ## $Release: 0.3.0 $
4
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
5
+ ##
6
+
7
+ module Kwatable
8
+
9
+ module RubyHelper
10
+
11
+
12
+ ## ruby keywords
13
+ def self.ruby_keywords()
14
+ keywords = <<-END
15
+ BEGIN END alias and begin break case
16
+ class def defined do else elsif end
17
+ ensure false for if in module next
18
+ nil not or redo rescue retry return
19
+ self super then true undef unless
20
+ until when while yield
21
+ END
22
+ return keywords.split(/\s+/)
23
+ end
24
+
25
+ @@ruby_keyword_table = ruby_keywords().inject({}) { |h, w| h[w] = true; h }
26
+
27
+
28
+ ## escape ruby keywords
29
+ def escape_ruby_keyword(word)
30
+ return @@ruby_keyword_table[word] ? "_#{word}" : word
31
+ end
32
+
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,62 @@
1
+ ##
2
+ ## $Rev: 44 $
3
+ ## $Release: 0.3.0 $
4
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
5
+ ##
6
+
7
+
8
+ require 'active_support/inflector'
9
+
10
+ module Kwatable
11
+
12
+ module TableHelper
13
+
14
+ def table_name(table)
15
+ #return table['model-name'] || table['name']
16
+ return table['name']
17
+ end
18
+
19
+ def table_class(table)
20
+ return table['class'] || Inflector.classify(table['name'])
21
+ end
22
+
23
+ def table_model(table)
24
+ #return Inflector.singularize(table_name(table))
25
+ return Inflector.underscore(table_class(table))
26
+ end
27
+
28
+ def table_display_column(table)
29
+ column = table['display-column']
30
+ column ||= table['columns'].find { |col| col['name'] == 'code' }
31
+ column ||= table['columns'].find { |col| col['name'] == 'name' }
32
+ return column
33
+ end
34
+
35
+ def table_ident_columns(table)
36
+ return table['columns'].find_all { |col| col['ident'] }
37
+ end
38
+
39
+ def table_ident_column(table)
40
+ return table['columns'].find { |col| col['ident'] }
41
+ end
42
+
43
+ def table_referrer_column(table, column)
44
+ return table['columns'].find { |col| col['ref'] && col['ref'].__id__ == column.__id__ }
45
+ end
46
+
47
+ ## return reason if table should be skip modeling
48
+ def table_check_modeling(table)
49
+ reason = nil
50
+ if table['modeling'] == false
51
+ reason = "table['modeling'] == false"
52
+ elsif table['ident-columns'].length > 1
53
+ reason = "compound primary key"
54
+ elsif table['ident-columns'].length == 0
55
+ reason = "no primary-key"
56
+ end
57
+ return reason
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,139 @@
1
+ <%
2
+ ##
3
+ ## kwatable template to generate Hibernate model class
4
+ ##
5
+ ## $Rev$
6
+ ## $Release: 0.3.0 $
7
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
8
+ ##
9
+ ## <template-desc>generate Hibernate mapping files</template-desc>
10
+ ## <template-properties>
11
+ ## --collection=type : collection type (set/list/map/bag/array/primitive-array)
12
+ ## --package=package : package of class
13
+ ## </template-properties>
14
+ ##
15
+
16
+ require 'kwatable/template/helper/common'
17
+ require 'kwatable/template/helper/table'
18
+ require 'kwatable/template/helper/column'
19
+ require 'kwatable/template/helper/java'
20
+ extend Kwatable::CommonHelper
21
+ extend Kwatable::TableHelper
22
+ extend Kwatable::ColumnHelper
23
+ extend Kwatable::JavaHelper
24
+
25
+ require 'active_support/inflector'
26
+ extend Inflector
27
+
28
+ # context check
29
+ context_var_required('@tables')
30
+
31
+ %>
32
+ <?xml version="1.0" ?>
33
+ <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
34
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
35
+ <hibernate-mapping>
36
+
37
+ <%
38
+
39
+ ##
40
+ ## classes
41
+ ##
42
+ for table in @tables
43
+ reason = table_check_modeling(table)
44
+ if reason
45
+ msg = "*** table '#{table['name']}' skipped. (reason: #{reason})"
46
+ $stderr.puts msg unless @options[?q]
47
+ next
48
+ end
49
+ table_name = table['name']
50
+ class_name = table_java_class(table, @properties[:package])
51
+ vendor = (table['vendor'] || {})['hibernate']
52
+ %>
53
+ <class name="<%= class_name %>" table="<%= table_name %>">
54
+ <%
55
+
56
+ ##
57
+ ## columns
58
+ ##
59
+ for column in table['columns']
60
+ next if column['ref']
61
+ colname = column['name']
62
+ colattr = camel_case(column_attr(column), false)
63
+ coltype = column_java_type(column, true)
64
+ vendor = (column['vendor'] || {})['hibernate']
65
+ generator = vendor['generator'] if vendor
66
+ generator ||= column['serial'] ? 'native' : 'assigned'
67
+
68
+ if column['ident']
69
+ %>
70
+ <id name="<%= colattr %>" type="<%= coltype %>" column="<%= colname %>">
71
+ <generator class="<%= generator %>"/>
72
+ </id>
73
+ <%
74
+ else
75
+ %>
76
+ <property name="<%= colattr %>" type="<%= coltype %>" column="<%= colname %>"/>
77
+ <%
78
+ end
79
+ end if table['columns']
80
+
81
+ ##
82
+ ## references
83
+ ##
84
+ for column in table['columns']
85
+ next unless column['ref']
86
+ refname = column_refname(column)
87
+ colattr = camel_case(column_attr(column), false)
88
+ colclass = table_java_class(column['ref']['table'], @properties[:package])
89
+ %>
90
+ <many-to-one name ="<%= camel_case(refname, false) %>" class="<%= colclass %>" column="<%= column['name'] %>"/>
91
+ <%
92
+ end if table['columns']
93
+
94
+ ##
95
+ ## relations
96
+ ##
97
+ collection = @properties[:collection] || 'set'
98
+ if table['relations']
99
+ for relation in table['relations']
100
+ join_table = join_column = ''
101
+ case relation['kind']
102
+ when '1:1'
103
+ relattr = relation['attr'] || table_name(relation['table'])
104
+ %>
105
+ <one-to-one name="<%= camel_case(relattr, false) %> column="<%= column_attr(relation['columns'].first) %>"/>
106
+ <%
107
+ when '1:n'
108
+ relattr = relation['attr'] || Inflector.pluralize(table_name(relation['table']))
109
+ %>
110
+ <<%= collection %> name="<%= camel_case(relattr, false) %>">
111
+ <key column="<%= column_attr(relation['columns'].first) %>"/>
112
+ <one-to-many class="<%= table_class(relation['table']) %>"/>
113
+ </<%= collection %>>
114
+ <%
115
+ when 'n:n'
116
+ relattr = relation['attr'] || Inflector.pluralize(table_name(relation['table']))
117
+ tag = 'many-to-many'
118
+ join_table = relation['join-table']
119
+ join_table_clause = ' table="' + relation['join-table']['name'] + '"'
120
+ identcol1 = table_ident_column(table)
121
+ identcol2 = table_ident_column(relation['table'])
122
+ relcolumn1 = table_referrer_column(join_table, identcol1)
123
+ relcolumn2 = table_referrer_column(join_table, identcol2)
124
+ %>
125
+ <<%= collection %> name="<%= camel_case(relattr, false) %>" table="<%= join_table['name'] %>">
126
+ <key column="<%= relcolumn1['name'] %>"/>
127
+ <many-to-many class="<%= table_class(relation['table']) %>" column="<%= relcolumn2['name'] %>"/>
128
+ </<%= collection %>>
129
+ <%
130
+ end#case
131
+ end#for
132
+ end#if
133
+ %>
134
+ </class>
135
+
136
+ <%
137
+ end
138
+ %>
139
+ </hibernate-mapping>
@@ -0,0 +1,66 @@
1
+ <%
2
+
3
+ ##
4
+ ## kwatable template to generate Rails model class
5
+ ##
6
+ ## $Rev$
7
+ ## $Release: 0.3.0 $
8
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
9
+ ##
10
+ ## <template-desc>generate Rails model classes</template-desc>
11
+ ## <template-properties>
12
+ ## --lang=lang : language (ex. --lang=ja)
13
+ ## --encoding=encoding : encoding (ex. --encoding=UTF8)
14
+ ## --session-token : use session token
15
+ ## </template-properties>
16
+ ## <template-details>
17
+ ## * if template property '--session-token' is specified, create the library
18
+ ## file 'lib/session-token.rb' with the following code (this library should
19
+ ## be shared with view-helper).
20
+ ## ------- lib/session-token-helper.rb' ----------
21
+ ## require 'digest/md5'
22
+ ## module SessionTokenHelper
23
+ ## private
24
+ ## def session_token()
25
+ ## session['session_token'] ||= Digest::MD5.hexdigest("#{session.id}#{rand()}")
26
+ ## end
27
+ ## end
28
+ ## -----------------------------------------------
29
+ ## and add the following code into 'app/controllers/application.rb'.
30
+ ## ------- app/controllers/application.rb --------
31
+ ## require 'session-token-helper'
32
+ ## class ApplicationController < ActionController::Base
33
+ ## include SessionTokenHelper
34
+ ## private
35
+ ## def valid_session_token?()
36
+ ## if session_token() == params[:session_token]
37
+ ## return true
38
+ ## else
39
+ ## flash[:warning] = 'Invalid session token.'
40
+ ## redirect_to '/403.html'
41
+ ## return false
42
+ ## end
43
+ ## end
44
+ ## end
45
+ ## -----------------------------------------------
46
+ ## </template-details>
47
+ ##
48
+
49
+
50
+ require 'kwatable/template/helper/common'
51
+ require 'kwatable/template/helper/table'
52
+ require 'kwatable/template/helper/column'
53
+ require 'kwatable/template/helper/label'
54
+ extend Kwatable::CommonHelper
55
+ extend Kwatable::TableHelper
56
+ extend Kwatable::ColumnHelper
57
+ extend Kwatable::LabelHelper
58
+
59
+
60
+ @output_files = apply_subtemplate_for_each_tables(@tables) { |table|
61
+ output_filename = "#{Inflector.pluralize(table_model(table))}_controller.rb"
62
+ output_filename
63
+ }
64
+
65
+
66
+ %>