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,114 @@
1
+ <%
2
+
3
+ ###
4
+ ### $Rev$
5
+ ### $Release: 0.3.0 $
6
+ ### copyright(c) 2005 kuwata-lab.com all rights reserved.
7
+ ###
8
+
9
+ flag_session_token = @properties[:session_token] || @properties[:stoken]
10
+ per_page = @properties[:per_page] || 10
11
+
12
+ class_name = table_class(@table)
13
+ model_name = table_model(@table)
14
+ controller_name = Inflector.pluralize(model_name)
15
+
16
+ m = model_name
17
+ c = controller_name
18
+ %>
19
+ class <%= camel_case(controller_name) %>Controller < ApplicationController
20
+ def index
21
+ list
22
+ render :action => 'list'
23
+ end
24
+
25
+ # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
26
+ <% if flag_session_token %>
27
+ verify :method => :post, :only => [ :create, :update ], # :destroy
28
+ :redirect_to => { :action => :list }
29
+ <% else %>
30
+ verify :method => :post, :only => [ :destroy, :create, :update ],
31
+ :redirect_to => { :action => :list }
32
+ <% end %>
33
+
34
+ def list
35
+ @<%= m %>_pages, @<%= c %> = paginate :<%= c %>, :per_page => <%= per_page %>
36
+
37
+ end
38
+
39
+ def show
40
+ @<%= m %> = <%= class_name %>.find(params[:id])
41
+ end
42
+
43
+ def new
44
+ @<%= m %> = <%= class_name %>.new
45
+ end
46
+
47
+ def create
48
+ <% if flag_session_token %>
49
+ return unless valid_session_token?
50
+ <% end %>
51
+ @<%= m %> = <%= class_name %>.new(params[:<%= m %>])
52
+ if @<%= m %>.save
53
+ flash[:notice] = <%=q w(:xxx_was_created, to_label(m)) %>
54
+
55
+ redirect_to :action => 'list'
56
+ else
57
+ render :action => 'new'
58
+ end
59
+ end
60
+
61
+ def edit
62
+ @user = <%= class_name %>.find(params[:id])
63
+ end
64
+
65
+ def update
66
+ <% if flag_session_token %>
67
+ return unless valid_session_token?
68
+ <% end %>
69
+ @<%= m %> = <%= class_name %>.find(params[:id])
70
+ if @<%= m %>.update_attributes(params[:<%= m %>])
71
+ flash[:notice] = <%=q w(:xxx_was_updated, to_label(m)) %>
72
+
73
+ redirect_to :action => 'show', :id => @<%= m %>
74
+
75
+ else
76
+ render :action => 'edit'
77
+ end
78
+ end
79
+
80
+ def destroy
81
+ <% if flag_session_token %>
82
+ return unless valid_session_token?
83
+ <% end %>
84
+ <%= class_name %>.find(params[:id]).destroy
85
+ redirect_to :action => 'list'
86
+ end
87
+ <%# if flag_session_token %>
88
+ <%#
89
+ # def session_token()
90
+ # session['session_token'] ||= Digest::MD5.hexdigest("#{session.id}#{rand().to_s}")
91
+ # end
92
+ # private :session_token
93
+ #
94
+ # def valid_session_token?()
95
+ # if session_token() == params[:session_token]
96
+ # return true
97
+ # else
98
+ # flash[:notice] = ${q w(:invalid_session_token)}
99
+ # redirect_to :action => 'list'
100
+ # return false
101
+ # end
102
+ # end
103
+ # private :'valid_session_token?'
104
+ %>
105
+ <%# end %>
106
+
107
+ <% if @user_custom_code %>
108
+ <%= @user_custom_code %><% else %>
109
+ ## <user-custom-code>
110
+ ## ... put user custom code here
111
+ ## </user-custom-code>
112
+ <% end %>
113
+
114
+ end
@@ -0,0 +1,164 @@
1
+ <%
2
+
3
+ ##
4
+ ## kwatable template to generate Rails view-template in kwartz format
5
+ ##
6
+ ## $Rev: 45 $
7
+ ## $Release: 0.3.0 $
8
+ ## copyright(c) 2005 kuwata-lab.com all rights reserved.
9
+ ##
10
+ ## <template-desc>generate Rails view-template in kwartz format</template-desc>
11
+ ## <template-properties>
12
+ ## --lang=lang : language (ex. --lang=ja)
13
+ ## --charset=charset : charset (ex. --charset=UTF8)
14
+ ## --encoding=encoding : encoding (ex. --encoding=UTF8)
15
+ ## --stylesheet=s1,s2,... : stylesheets (default: scaffold)
16
+ ## --dict=dict1,dict2,... : dictionaries
17
+ ## --session-token : add session token to form
18
+ ## </template-properties>
19
+ ## <template-details>
20
+ ## * add the following code to 'app/controllers/application.rb' to use Kwartz.
21
+ ## ------ app/controllers/application.rb ---------
22
+ ## class ApplicationController < ActionController::Base
23
+ ## end
24
+ ## require 'kwartz/helper/rails'
25
+ ## ActionView::Base.register_template_handler('html', Kwartz::Helper::RailsTemplate)
26
+ ## #Kwartz::Helper::RailsTemplate.lang = 'rails' # or eruby/ruby/erubis/pierubis
27
+ ## #Kwartz::Helper::RailsTemplate.pdata_suffix = '.html'
28
+ ## #Kwartz::Helper::RailsTemplate.plogic_suffix = '.plogic'
29
+ ## #Kwartz::Helper::RailsTemplate.default_properties = { :escape=>true }
30
+ ## #Kwartz::Helper::RailsTemplate.use_cache = true
31
+ ## #Kwartz::Helper::RailsTemplate.debug = false
32
+ ## -----------------------------------------------
33
+ ## * in addition, add the following into 'app/helpers/application_helper.rb'.
34
+ ## module Kwartz::Helper::ActionViewHelper defines methods start_link_tag()
35
+ ## and start_remote_link_tag().
36
+ ## ------ app/helpers/application_helper.rb ------
37
+ ## require 'kwartz/helper/rails'
38
+ ## module ApplicationHelper
39
+ ## include Kwartz::Helper::ActionViewHelper
40
+ ## end
41
+ ## -----------------------------------------------
42
+ ## * if template property '--session-token' is specified, create the library
43
+ ## file 'lib/session-token.rb' with the following code (this library should
44
+ ## be shared with application controller).
45
+ ## ------- lib/session-token-helper.rb ----------
46
+ ## require 'digest/md5'
47
+ ## module SessionTokenHelper
48
+ ## private
49
+ ## def session_token()
50
+ ## session['session_token'] ||= Digest::MD5.hexdigest("#{session.id}#{rand()}")
51
+ ## end
52
+ ## end
53
+ ## -----------------------------------------------
54
+ ## and add the following code into 'app/helpers/application_helper.rb'.
55
+ ## ------ app/helpers/application_helper.rb ------
56
+ ## require 'session-token-helper'
57
+ ## module ApplicationHelper
58
+ ## include SessionTokenHelper
59
+ ## end
60
+ ## -----------------------------------------------
61
+ ## </template-details>
62
+ ##
63
+
64
+
65
+ require 'kwatable/template/helper/common'
66
+ require 'kwatable/template/helper/table'
67
+ require 'kwatable/template/helper/column'
68
+ require 'kwatable/template/helper/label'
69
+ extend Kwatable::CommonHelper
70
+ extend Kwatable::TableHelper
71
+ extend Kwatable::ColumnHelper
72
+ extend Kwatable::LabelHelper
73
+
74
+ require 'active_support/inflector'
75
+ extend Inflector
76
+
77
+
78
+ ## context check
79
+ context_var_required('@tables')
80
+
81
+
82
+ ## escape string
83
+ def _(obj)
84
+ return ERB::Util.h(obj.to_s)
85
+ end
86
+
87
+
88
+ ##
89
+ def stylesheets(indent_spaces=' ', options={})
90
+ stylesheet = @properties.key?(:stylesheet) ? @properties[:stylesheet] : 'scaffold'
91
+ path = options.key?(:path) ? options[:path] : '../../../public/stylesheets'
92
+ path = path + '/' if path && path[-1] != ?/
93
+ list = stylesheet.split(/,/).collect { |sheet|
94
+ %Q|<link rel="stylesheet" type="text/css" href="#{path}#{sheet}.css" />|
95
+ }
96
+ return list.join("\n" + indent_spaces)
97
+ end
98
+
99
+ ##
100
+ def stylesheet_names()
101
+ stylesheet = @properties.key?(:stylesheet) ? @properties[:stylesheet] : 'scaffold'
102
+ return stylesheet ? stylesheet.split(/,/) : []
103
+ end
104
+
105
+ ##
106
+ def vars(table)
107
+ item_name = table_model(table)
108
+ list_name = Inflector.pluralize(item_name)
109
+ lang = @properties[:lang]
110
+ charset = _getval(@properties[:charset], @properties[:encoding])
111
+ encoding = _getval(@properties[:encoding], @properties[:charset])
112
+ return list_name, item_name, lang, charset, encoding
113
+ end
114
+
115
+ def _getval(val1, val2)
116
+ return val1 == false ? nil : (val1 || val2)
117
+ end
118
+
119
+
120
+
121
+ ##
122
+ ## apply template and output files
123
+ ##
124
+
125
+ # subtemplate filenames and contents
126
+ kwatable_rb = Util.find_file('kwatable.rb', $LOAD_PATH)
127
+ template_basename = File.basename(@template_filename).sub(/\.eruby/, '')
128
+ template_dir = kwatable_rb.sub(/\.rb$/, "/template/#{template_basename}")
129
+ subtemplates = Dir.glob("#{template_dir}/*.eruby").collect { |fname|
130
+ filename = "#{template_basename}/#{File.basename(fname)}"
131
+ filepath = Util.find_file(filename, @template_pathlist)
132
+ unless filepath
133
+ raise "#{template_basename}: subtemplate `#{filename}' not found."
134
+ end
135
+ [filepath, File.read(filepath)]
136
+ }.sort { |a1, a2| a1.first <=> a2.first }
137
+
138
+ # apply template for each table
139
+ encoding = @properties[:encoding] || @properties[:charset]
140
+ @output_files = []
141
+ each_table(@tables) do |table|
142
+ controller_name = Inflector.pluralize(table_model(table))
143
+ layouts = []
144
+ subtemplates.each do |subtemplate_filepath, subtemplate|
145
+ # set output filename
146
+ basename = File.basename(subtemplate_filepath).sub(/\.eruby\z/, '')
147
+ output_filename = basename.sub!(/\Alayout\b/, controller_name) \
148
+ ? "layouts/#{basename}" \
149
+ : "#{controller_name}/#{basename}"
150
+ dir = @options[?d]
151
+ output_filename = "#{dir}/#{output_filename}" if dir
152
+ # apply template
153
+ context = self
154
+ @table = table
155
+ opts = { :outdir=>@options[?d], :encoding=>encoding, :template=>subtemplate }
156
+ output, action = eval_template_with_custom_code(subtemplate_filepath, context, output_filename, opts)
157
+ @table = nil
158
+ list = output_filename =~ /\Alayouts\// ? layouts : @output_files
159
+ list << [output_filename, output, action]
160
+ end
161
+ @output_files.concat(layouts)
162
+ end
163
+
164
+ %>
@@ -0,0 +1,56 @@
1
+ <%
2
+ ###
3
+ ### $Rev$
4
+ ### $Release: 0.3.0 $
5
+ ### copyright(c) 2005 kuwata-lab.com all rights reserved.
6
+ ###
7
+
8
+
9
+ list_name, item_name, lang, charset, encoding = vars(@table)
10
+ %>
11
+ <%
12
+ @table['columns'].each do |column|
13
+ colname = column_refname(column)
14
+ if column['enum']
15
+ %>
16
+ #<%=_ item_name %>_<%=_ colname %> {
17
+ /* Value: @<%=_ item_name %>.<%=_ colname %>; */
18
+ Value: label;
19
+ logic: {
20
+ case @<%=_ item_name %>.<%=_ colname %> #
21
+ <%
22
+ enum_list = column['enum'].collect { |enum| enum_value_and_label(enum, colname) }
23
+ width = enum_list.inject(0) { |max, arr| max < (len=arr.first.length) ? len : max }
24
+ enum_list.each do |evalue, elabel|
25
+ space = ' ' * (width - evalue.length)
26
+ %>
27
+ when '<%=_ evalue %>'<%=_ space %> ; label = '<%=_ elabel %>'
28
+ <%
29
+ end
30
+ %>
31
+ else <%=_ ' ' * width %> ; label = ' - '
32
+ end
33
+ _elem
34
+ }
35
+ }
36
+ <%
37
+ elsif column['ref']
38
+ display_column = table_display_column(column['ref']['table'])
39
+ display_column ||= @table['ident-columns'].first
40
+ %>
41
+ #<%=_ item_name %>_<%=_ colname %> {
42
+ Value: @<%=_ item_name %>.<%=_ colname %>.<%=_ column_attr(display_column) %>;
43
+ }
44
+ <%
45
+ else
46
+ %>
47
+ #<%=_ item_name %>_<%=_ colname %> {
48
+ Value: @<%=_ item_name %>.<%=_ colname %>;
49
+ }
50
+ <%
51
+ end
52
+ %>
53
+
54
+ <%
55
+ end
56
+ %>
@@ -0,0 +1,81 @@
1
+ <%
2
+ ###
3
+ ### $Rev$
4
+ ### $Release: 0.3.0 $
5
+ ### copyright(c) 2005 kuwata-lab.com all rights reserved.
6
+ ###
7
+
8
+
9
+ list_name, item_name, lang, charset, encoding = vars(@table)
10
+ %>
11
+ #error_messages {
12
+ elem: error_messages_for '<%=_ item_name %>';
13
+ }
14
+
15
+ <% if @properties[:session_token] || @properties[:stoken] %>
16
+ #session_token {
17
+ attrs: 'value' session_token();
18
+ }
19
+
20
+ <% end %>
21
+ <%
22
+ @table['columns'].each do |column|
23
+ next unless column_editable?(column)
24
+ colname = column_refname(column)
25
+ control_type = column_control_type(column)
26
+ flag = control_type != 'radio'
27
+ %>
28
+ <% if flag %>
29
+ #<%=_ item_name %>_<%=_ colname %> {
30
+ <% end %>
31
+ <% case control_type %>
32
+ <% when 'text_field' %>
33
+ elem: text_field '<%= item_name %>', '<%=_ colname %>';
34
+ <% when 'password_field' %>
35
+ elem: password_field '<%=_ item_name %>', '<%=_ colname %>';
36
+ <% when 'text_area' %>
37
+ elem: text_area '<%=_ item_name %>', '<%=_ colname %>';
38
+ <% when 'select' %>
39
+ <%
40
+ t = column['type']
41
+ if column['ref']
42
+ ref_table = column['ref']['table']
43
+ classname = table_class(column['ref']['table'])
44
+ varname = colname + '_options'
45
+ display_column = table_display_column(column['ref']['table'])
46
+ display_column ||= column['ref']['table']['ident-columns'].first
47
+ %>
48
+ elem: select '<%=_ item_name %>', '<%=_ column['name'] %>', <%=_ varname %>, :include_blank=>true;
49
+ logic: {
50
+ <%=_ varname %> = @<%=_ varname %> || <%=_ classname %>.find_all().collect {|e| [e.<%= display_column['name'] %>, e.id]}
51
+ _elem
52
+ }
53
+ <% elsif t == 'date' %>
54
+ elem: date_select '<%=_ item_name %>', '<%=_ column['name'] %>';
55
+ <% elsif t == 'time' %>
56
+ elem: datetime_select '<%=_ item_name %>', '<%=_ column['name'] %>';
57
+ <% elsif t == 'timestamp' || t == 'datetime' %>
58
+ elem: datetime_select '<%=_ item_name %>', '<%=_ column['name'] %>';
59
+ <% else %>
60
+ /* elem: select '<%=_ item_name %>', '<%=_ column['name'] %>', ... ; */
61
+ <% end %>
62
+ <% when 'checkbox' %>
63
+ elem: check_box '<%=_ item_name %>', '<%=_ colname %>';
64
+ <% when 'radio' %>
65
+ <%
66
+ column['enum'].each do |elem|
67
+ evalue, elabel = enum_value_and_label(elem, colname)
68
+ %>
69
+ #<%=_ item_name %>_<%=_ colname %>_<%=_ evalue.to_s.downcase %> {
70
+ elem: radio_button '<%=_ item_name %>', '<%=_ colname %>', '<%=_ evalue %>';
71
+ }
72
+ <%
73
+ end
74
+ %>
75
+
76
+ <% end %>
77
+ <% if flag %>
78
+ }
79
+
80
+ <% end %>
81
+ <% end %>
@@ -0,0 +1,36 @@
1
+ <%
2
+ ###
3
+ ### $Rev$
4
+ ### $Release: 0.3.0 $
5
+ ### copyright(c) 2005 kuwata-lab.com all rights reserved.
6
+ ###
7
+
8
+
9
+ list_name, item_name, lang, charset, encoding = vars(@table)
10
+ ident_column = @table['ident-columns'].first
11
+ id_name = ident_column['name']
12
+ %>
13
+ #link_to_list {
14
+ stag: start_link_tag :action=>'list';
15
+ }
16
+
17
+ #link_to_new {
18
+ stag: start_link_tag :action=>'new';
19
+ }
20
+
21
+ #link_to_show {
22
+ stag: start_link_tag :action=>'show', :id=>@<%= item_name %>.<%= id_name %>;
23
+ }
24
+
25
+ #link_to_edit {
26
+ stag: start_link_tag :action=>'edit', :id=>@<%= item_name %>.<%= id_name %>;
27
+ }
28
+
29
+ #link_to_destroy {
30
+ <% if @properties[:session_token] || @properties[:stoken] %>
31
+ stag: start_link_tag({:action=>'destroy', :id=>@<%= item_name %>.<%= id_name %>, :session_token=>session_token()},
32
+ {:confirm=><%=q w(:are_you_sure) %>});
33
+ <% else %>
34
+ stag: start_link_tag({:action=>'destroy', :id=>@<%= item_name %>.<%= id_name %>}, :confirm=>'<%= w(:are_you_sure) %>');
35
+ <% end %>
36
+ }