kwalify 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +24 -19
- data/README.txt +51 -51
- data/bin/kwalify +2 -2
- data/contrib/inline-require +151 -0
- data/contrib/kwalify +2850 -0
- data/doc-api/classes/CommandOptionError.html +184 -0
- data/doc-api/classes/CommandOptionParser.html +325 -0
- data/doc-api/classes/Kwalify.html +270 -0
- data/doc-api/classes/Kwalify/AssertionError.html +148 -0
- data/doc-api/classes/Kwalify/BaseError.html +296 -0
- data/doc-api/classes/Kwalify/CommandOptionError.html +168 -0
- data/doc-api/classes/Kwalify/ErrorHelper.html +218 -0
- data/doc-api/classes/Kwalify/HashInterface.html +240 -0
- data/doc-api/classes/Kwalify/KwalifyError.html +111 -0
- data/doc-api/classes/Kwalify/Main.html +336 -0
- data/doc-api/classes/Kwalify/MetaValidator.html +432 -0
- data/doc-api/classes/Kwalify/Parser.html +155 -0
- data/doc-api/classes/Kwalify/PlainYamlParser.html +520 -0
- data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +165 -0
- data/doc-api/classes/Kwalify/Rule.html +411 -0
- data/doc-api/classes/Kwalify/SchemaError.html +148 -0
- data/doc-api/classes/Kwalify/Types.html +301 -0
- data/doc-api/classes/Kwalify/ValidationError.html +148 -0
- data/doc-api/classes/Kwalify/Validator.html +311 -0
- data/doc-api/classes/Kwalify/YamlParser.html +535 -0
- data/doc-api/classes/Kwalify/YamlSyntaxError.html +168 -0
- data/doc-api/classes/Test.html +107 -0
- data/doc-api/classes/Test/Unit.html +101 -0
- data/doc-api/classes/YamlHelper.html +259 -0
- data/doc-api/created.rid +1 -0
- data/doc-api/files/__/README_txt.html +179 -0
- data/doc-api/files/kwalify/errors_rb.html +114 -0
- data/doc-api/files/kwalify/main_rb.html +117 -0
- data/doc-api/files/kwalify/messages_rb.html +107 -0
- data/doc-api/files/kwalify/meta-validator_rb.html +117 -0
- data/doc-api/files/kwalify/rule_rb.html +116 -0
- data/doc-api/files/kwalify/types_rb.html +114 -0
- data/doc-api/files/kwalify/util/assert-text-equal_rb.html +115 -0
- data/doc-api/files/kwalify/util/hash-interface_rb.html +107 -0
- data/doc-api/files/kwalify/util/option-parser_rb.html +107 -0
- data/doc-api/files/kwalify/util/testcase-helper_rb.html +115 -0
- data/doc-api/files/kwalify/util/yaml-helper_rb.html +114 -0
- data/doc-api/files/kwalify/validator_rb.html +117 -0
- data/doc-api/files/kwalify/yaml-parser_rb.html +117 -0
- data/doc-api/files/kwalify_rb.html +120 -0
- data/doc-api/fr_class_index.html +50 -0
- data/doc-api/fr_file_index.html +41 -0
- data/doc-api/fr_method_index.html +109 -0
- data/doc-api/index.html +24 -0
- data/doc-api/rdoc-style.css +208 -0
- data/doc/users-guide.html +693 -193
- data/examples/address-book/Makefile +5 -0
- data/examples/address-book/address-book.schema.yaml +2 -1
- data/examples/invoice/Makefile +5 -0
- data/examples/invoice/invoice.schema.yaml +3 -2
- data/examples/tapkit/Makefile +5 -0
- data/examples/tapkit/main.rb +7 -0
- data/examples/tapkit/tapkit.schema.yaml +6 -1
- data/lib/kwalify.rb +3 -3
- data/lib/kwalify/errors.rb +2 -2
- data/lib/kwalify/main.rb +161 -84
- data/lib/kwalify/messages.rb +17 -11
- data/lib/kwalify/meta-validator.rb +11 -2
- data/lib/kwalify/rule.rb +13 -3
- data/lib/kwalify/templates/genclass-java.eruby +195 -0
- data/lib/kwalify/templates/genclass-ruby.eruby +84 -0
- data/lib/kwalify/types.rb +18 -18
- data/lib/kwalify/util/assert-text-equal.rb +44 -0
- data/lib/kwalify/util/hash-interface.rb +37 -0
- data/lib/kwalify/util/option-parser.rb +2 -2
- data/lib/kwalify/util/testcase-helper.rb +112 -0
- data/lib/kwalify/util/yaml-helper.rb +2 -2
- data/lib/kwalify/validator.rb +2 -2
- data/lib/kwalify/yaml-parser.rb +12 -9
- data/test/test-main.rb +77 -78
- data/test/test-main.yaml +543 -769
- data/test/test-metavalidator.rb +27 -47
- data/test/test-metavalidator.yaml +21 -2
- data/test/test-rule.rb +6 -39
- data/test/test-rule.yaml +2 -2
- data/test/test-validator.rb +36 -869
- data/test/test-validator.yaml +28 -20
- data/test/test-yamlparser.rb +30 -1248
- data/test/test-yamlparser.yaml +138 -110
- data/test/test.rb +33 -13
- data/test/tmp.dir/Context.java +40 -0
- data/test/tmp.dir/Group.java +33 -0
- data/test/tmp.dir/User.java +43 -0
- data/test/tmp.dir/action1.document +18 -0
- data/test/tmp.dir/action1.schema +32 -0
- data/test/tmp.dir/action2.document +18 -0
- data/test/tmp.dir/action2.schema +32 -0
- data/test/tmp.dir/emacs.document +6 -0
- data/test/tmp.dir/emacs.schema +6 -0
- data/test/tmp.dir/meta1.document +0 -0
- data/test/tmp.dir/meta1.schema +3 -0
- data/test/tmp.dir/meta2.document +0 -0
- data/test/tmp.dir/meta2.schema +3 -0
- data/test/tmp.dir/silent1.document +3 -0
- data/test/tmp.dir/silent1.schema +3 -0
- data/test/tmp.dir/silent2.document +7 -0
- data/test/tmp.dir/silent2.schema +3 -0
- data/test/tmp.dir/stream.invalid +8 -0
- data/test/tmp.dir/stream.schema +3 -0
- data/test/tmp.dir/stream.valid +8 -0
- data/test/tmp.dir/untabify.document +5 -0
- data/test/tmp.dir/untabify.schema +10 -0
- metadata +98 -12
- data/lib/kwalify/util/assert-diff.rb +0 -44
data/lib/kwalify/messages.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
2
|
+
### $Rev: 51 $
|
3
|
+
### $Release: 0.6.0 $
|
4
4
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
@@ -15,33 +15,38 @@ module Kwalify
|
|
15
15
|
|
16
16
|
|
17
17
|
@@messages[:command_help] = <<END
|
18
|
-
|
19
|
-
|
18
|
+
kwalify - tiny schema validator for YAML and JSON
|
19
|
+
Usage1: kwalify [..options..] -f schema.yaml doc.yaml [doc2.yaml ...]
|
20
|
+
Usage2: kwalify [..options..] -m schema.yaml [schema2.yaml ...]
|
21
|
+
Usage3: kwalify [..options..] -a action -f schema.yaml [schema2.yaml ...]
|
20
22
|
-h, --help : help
|
21
23
|
-v : version
|
22
24
|
-s : silent
|
23
25
|
-f schema.yaml : schema definition file
|
24
26
|
-m : meta-validation mode
|
25
|
-
-t : expand tab
|
27
|
+
-t : expand tab characters
|
26
28
|
-l : show linenumber when errored (experimental)
|
27
29
|
-E : show errors in emacs-style (implies '-l')
|
30
|
+
-a action : action ('genclass-ruby' or 'genclass-java')
|
31
|
+
(use '-ha action' option to show action properites)
|
28
32
|
END
|
33
|
+
# -I path : path for template of action
|
29
34
|
|
30
35
|
|
31
36
|
|
32
37
|
##----- begin
|
33
38
|
# filename: lib/kwalify/main.rb
|
39
|
+
@@messages[:command_option_actionnoschema] = "schema filename is not specified."
|
34
40
|
@@messages[:command_option_noaction] = "command-line option '-f' or '-m' required."
|
35
|
-
@@messages[:
|
36
|
-
@@messages[:
|
37
|
-
@@messages[:meta_invalid] = "%s: NG!\n"
|
38
|
-
@@messages[:schema_empty] = "%s#%d: empty schema.\n"
|
41
|
+
@@messages[:command_option_notemplate] = "%s: invalid action (template not found).\n"
|
42
|
+
@@messages[:schema_empty] = "%s: empty schema.\n"
|
39
43
|
@@messages[:validation_empty] = "%s#%d: empty.\n"
|
40
44
|
@@messages[:validation_valid] = "%s#%d: valid.\n"
|
41
45
|
@@messages[:validation_invalid] = "%s#%d: INVALID\n"
|
42
|
-
@@messages[:command_property_invalid] = "%s: invalid property."
|
43
|
-
@@messages[:command_option_noarg] = "-%s: argument required."
|
44
46
|
@@messages[:command_option_noschema] = "-%s: schema filename required."
|
47
|
+
@@messages[:command_option_noaction] = "-%s: action required."
|
48
|
+
@@messages[:command_option_notpath] = "-%s: template path required."
|
49
|
+
@@messages[:command_property_invalid] = "%s: invalid property."
|
45
50
|
@@messages[:command_option_invalid] = "-%s: invalid command option."
|
46
51
|
# --
|
47
52
|
# filename: lib/kwalify/rule.rb
|
@@ -49,6 +54,7 @@ END
|
|
49
54
|
@@messages[:key_unknown] = "unknown key."
|
50
55
|
@@messages[:type_notstr] = "not a string."
|
51
56
|
@@messages[:type_unknown] = "unknown type."
|
57
|
+
@@messages[:classname_notmap] = "available only with map type."
|
52
58
|
@@messages[:required_notbool] = "not a boolean."
|
53
59
|
@@messages[:pattern_notstr] = "not a string (or regexp)"
|
54
60
|
@@messages[:pattern_notmatch] = "should be '/..../'."
|
@@ -1,6 +1,6 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
2
|
+
### $Rev: 51 $
|
3
|
+
### $Release: 0.6.0 $
|
4
4
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
@@ -34,6 +34,8 @@ mapping: &main-rule
|
|
34
34
|
type: str
|
35
35
|
"desc":
|
36
36
|
type: str
|
37
|
+
"classname":
|
38
|
+
type: str
|
37
39
|
"type":
|
38
40
|
type: str
|
39
41
|
#required: yes
|
@@ -135,6 +137,13 @@ END
|
|
135
137
|
# errors << validate_error(:type_unknown, rule, "#{path}/type", type)
|
136
138
|
#end
|
137
139
|
#
|
140
|
+
if hash.key?('classname')
|
141
|
+
val = hash['classname']
|
142
|
+
unless val.nil? || type == 'map'
|
143
|
+
errors << validate_error(:classname_notmap, rule, "#{path}/classname", 'classname:')
|
144
|
+
end
|
145
|
+
end
|
146
|
+
#
|
138
147
|
if hash.key?('pattern')
|
139
148
|
val = hash['pattern']
|
140
149
|
pat = (val =~ /\A\/(.*)\/([mi]?[mi]?)\z/ ? $1 : val)
|
data/lib/kwalify/rule.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
2
|
+
### $Rev: 51 $
|
3
|
+
### $Release: 0.6.0 $
|
4
4
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
@@ -36,7 +36,7 @@ module Kwalify
|
|
36
36
|
attr_reader :length
|
37
37
|
attr_reader :ident
|
38
38
|
attr_reader :unique
|
39
|
-
|
39
|
+
attr_reader :classname
|
40
40
|
|
41
41
|
def init(hash, path="", rule_table={})
|
42
42
|
unless hash.is_a?(Hash)
|
@@ -67,6 +67,7 @@ module Kwalify
|
|
67
67
|
when :unique ; _init_unique_value( val, rule, path)
|
68
68
|
when :sequence ; _init_sequence_value(val, rule, path, rule_table)
|
69
69
|
when :mapping ; _init_mapping_value( val, rule, path, rule_table)
|
70
|
+
when :classname ; _init_classname_value(val, rule, path)
|
70
71
|
else
|
71
72
|
#* key=:key_unknown msg="unknown key."
|
72
73
|
raise schema_error(:key_unknown, rule, curr_path, "#{key}:")
|
@@ -101,6 +102,15 @@ module Kwalify
|
|
101
102
|
end
|
102
103
|
|
103
104
|
|
105
|
+
def _init_classname_value(val, rule, path)
|
106
|
+
@classname = val
|
107
|
+
unless @type == 'map'
|
108
|
+
#* key=:classname_notmap msg="available only with map type."
|
109
|
+
raise schema_error(:classname_notmap, rule, "#{path}/classname", 'classname:')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
104
114
|
def _init_name_value(val, rule, path)
|
105
115
|
@name = val
|
106
116
|
end
|
@@ -0,0 +1,195 @@
|
|
1
|
+
<%
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
if @describe
|
6
|
+
sb = []
|
7
|
+
sb << " --package=name : package name\n"
|
8
|
+
sb << " --extends=name : class name to extend\n"
|
9
|
+
sb << " --implements=name,... : interface names to implement\n"
|
10
|
+
sb << " --dir=path : directory to locate output file\n"
|
11
|
+
sb << " --basedir=path : base directory to locate output file\n"
|
12
|
+
return sb.join
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def java_type(rule)
|
17
|
+
return rule.classname if rule.classname
|
18
|
+
imports = []
|
19
|
+
case rule.type
|
20
|
+
when 'str', 'text' ; return 'String'
|
21
|
+
when 'char' ; return 'char'
|
22
|
+
when 'int' ; return 'int'
|
23
|
+
when 'float' ; return 'float'
|
24
|
+
when 'bool' ; return 'boolean'
|
25
|
+
when 'date' ; return 'Date'
|
26
|
+
when 'timestamp' ; return 'Date'
|
27
|
+
when 'seq' ; return 'List'
|
28
|
+
when 'map' ; return 'Map'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
#def primitive?(type)
|
34
|
+
# @primitives ||= %w[int char float boolean double long byte]
|
35
|
+
# return @primitives.include?(type)
|
36
|
+
#end
|
37
|
+
|
38
|
+
|
39
|
+
def camelize(str, all=true)
|
40
|
+
s = str.split(/[^\w]/).collect { |w| w.capitalize }.join()
|
41
|
+
s[0,1] = s[0].chr.lower unless all
|
42
|
+
return s
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def write_file(classdef_info)
|
47
|
+
classname, classdef = classdef_info
|
48
|
+
if @properties[:dir]
|
49
|
+
dir = @properties[:dir]
|
50
|
+
else
|
51
|
+
basedir = @properties[:basedir] || '.'
|
52
|
+
dir = basedir.dup
|
53
|
+
dir << '/' << @properties[:package].gsub(/\./, '/') if @properties[:package]
|
54
|
+
end
|
55
|
+
FileUtils.mkdir_p(dir) unless test(?d, dir)
|
56
|
+
filename = "#{dir}/#{classname}.java"
|
57
|
+
$stdout.print "generating #{filename}..."
|
58
|
+
File.open(filename, 'w') { |f| f.write(classdef) }
|
59
|
+
$stdout.print "done.\n"
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def generate_classdef(rule)
|
64
|
+
classdef_list = []
|
65
|
+
done = {}
|
66
|
+
_generate(rule, classdef_list, done)
|
67
|
+
classdef_list.each do |classdef_info|
|
68
|
+
write_file(classdef_info)
|
69
|
+
end
|
70
|
+
return nil
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def _generate(rule, classdef_list=[], done={})
|
75
|
+
return if done.key?(rule.__id__)
|
76
|
+
done[rule.__id__] = rule
|
77
|
+
#
|
78
|
+
sb = []
|
79
|
+
if rule.classname
|
80
|
+
unless rule.type == 'map'
|
81
|
+
raise "*** assertion error: rule.type==#{rule.type.inspect}"
|
82
|
+
end
|
83
|
+
assert unless rule.mapping
|
84
|
+
|
85
|
+
## preamble
|
86
|
+
sb << "// generated by kwalify from #{@properties[:schema_filename]}\n"
|
87
|
+
sb << "\n"
|
88
|
+
|
89
|
+
## package and import classes
|
90
|
+
sb << "package #{@properties[:package]};\n" if @properties[:package]
|
91
|
+
sb << "import java.util.*;\n"
|
92
|
+
sb << "\n"
|
93
|
+
|
94
|
+
## start class declaration
|
95
|
+
classname = rule.classname
|
96
|
+
extends = @properties[:extends] ? " extends #{@properties[:extends] % classname}" : nil
|
97
|
+
implements = @properties[:implements] ? " implements #{@properties[:implements]}" : nil
|
98
|
+
classname = classname + '_' if @properties[:ggap]
|
99
|
+
sb << "/**\n"
|
100
|
+
sb << " * #{rule.desc}\n"
|
101
|
+
sb << " */\n"
|
102
|
+
sb << "public class #{classname}#{extends}#{implements} {\n"
|
103
|
+
sb << "\n"
|
104
|
+
|
105
|
+
## instance variables
|
106
|
+
rule.mapping.each do |name, map_rule|
|
107
|
+
type = java_type(map_rule)
|
108
|
+
sb << " private #{type} _#{name};\n"
|
109
|
+
end
|
110
|
+
sb << "\n"
|
111
|
+
|
112
|
+
## default constructor
|
113
|
+
sb << " public #{classname}() {}\n"
|
114
|
+
sb << "\n"
|
115
|
+
|
116
|
+
## constructor
|
117
|
+
sb << " public #{classname}(Map map) {\n"
|
118
|
+
objdecl = " Object obj;\n"
|
119
|
+
seqdecl = " List seq;\n"
|
120
|
+
rule.mapping.each do |name, map_rule|
|
121
|
+
next unless name =~ /\A[a-zA-Z_]\w*\z/
|
122
|
+
if map_rule.classname
|
123
|
+
cname = map_rule.classname
|
124
|
+
type = java_type(map_rule)
|
125
|
+
sb << objdecl; objdecl = ''
|
126
|
+
sb << " if ((obj = map.get(\"#{name}\")) != null && obj instanceof Map) {\n"
|
127
|
+
sb << " _%-8s = new #{cname}((Map)obj);\n" % name
|
128
|
+
sb << " } else {\n"
|
129
|
+
sb << " _%-8s = (#{type})obj;\n" % name
|
130
|
+
sb << " }\n"
|
131
|
+
elsif map_rule.sequence && map_rule.sequence[0].classname
|
132
|
+
item_rule = map_rule.sequence[0]
|
133
|
+
cname = item_rule.classname
|
134
|
+
type = java_type(item_rule)
|
135
|
+
sb << seqdecl; seqdecl = ''
|
136
|
+
sb << objdecl; objdecl = ''
|
137
|
+
sb << " if ((seq = (List)map.get(\"#{name}\")) != null) {\n"
|
138
|
+
sb << " for (int i = 0; i < seq.size(); i++) {\n"
|
139
|
+
sb << " if ((obj = seq.get(i)) instanceof Map) {\n"
|
140
|
+
sb << " seq.set(i, new #{cname}((Map)obj));\n"
|
141
|
+
sb << " }\n"
|
142
|
+
sb << " }\n"
|
143
|
+
sb << " }\n"
|
144
|
+
sb << " _%-12s = seq;\n" % name
|
145
|
+
else
|
146
|
+
type = java_type(map_rule)
|
147
|
+
@wrapper_classes ||= {
|
148
|
+
'int' => 'Number', 'short' => 'Number',
|
149
|
+
'float' => 'Number', 'double' => 'Number',
|
150
|
+
'long' => 'Number', 'byte' => 'Number',
|
151
|
+
'char' => 'Character',
|
152
|
+
'boolean' => 'Boolean',
|
153
|
+
}
|
154
|
+
if wrapper = @wrapper_classes[type]
|
155
|
+
sb << " if (map.get(\"#{name}\") != null) {\n"
|
156
|
+
sb << " _%-8s = ((#{wrapper})map.get(\"#{name}\")).#{type}Value();\n" % name
|
157
|
+
sb << " }\n"
|
158
|
+
else
|
159
|
+
sb << " _%-12s = (#{type})map.get(\"#{name}\");\n" % name
|
160
|
+
end
|
161
|
+
end #if
|
162
|
+
end #each
|
163
|
+
sb << " }\n"
|
164
|
+
sb << "\n"
|
165
|
+
|
166
|
+
## setter/getter
|
167
|
+
rule.mapping.each do |name, map_rule|
|
168
|
+
next unless name =~ /\A[a-zA-Z_]\w*\z/
|
169
|
+
type = java_type(map_rule)
|
170
|
+
sb << " public #{type} get#{camelize(name)}() { return _#{name}; }\n"
|
171
|
+
sb << " public void set#{camelize(name)}(#{type} #{name}_) { _#{name} = #{name}_; }\n"
|
172
|
+
end
|
173
|
+
|
174
|
+
## end of class declaration
|
175
|
+
if @properties[:ggap]
|
176
|
+
sb << "}\n"
|
177
|
+
sb << "public class #{rule.classname} < #{classname}\n"
|
178
|
+
end
|
179
|
+
sb << "}\n"
|
180
|
+
|
181
|
+
##
|
182
|
+
classdef_list << [classname, sb.join]
|
183
|
+
end #if
|
184
|
+
#
|
185
|
+
rule.sequence.each do |seq_rule|
|
186
|
+
_generate(seq_rule, classdef_list, done)
|
187
|
+
end if rule.sequence
|
188
|
+
rule.mapping.each do |name, map_rule|
|
189
|
+
_generate(map_rule, classdef_list, done)
|
190
|
+
end if rule.mapping
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
%>
|
195
|
+
<%= generate_classdef(@rule) %>
|
@@ -0,0 +1,84 @@
|
|
1
|
+
<%
|
2
|
+
|
3
|
+
if @describe
|
4
|
+
sb = []
|
5
|
+
sb << " --module=name : module name in which class defined\n"
|
6
|
+
sb << " --parent=name : parent class name\n"
|
7
|
+
sb << " --include=name : module name which all classes include\n"
|
8
|
+
#sb << " --ggap : use generation gap pattern\n"
|
9
|
+
return sb.join
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def generate_classdef(rule)
|
14
|
+
sb = []
|
15
|
+
sb << "module #{@properties[:module]}\n\n" if @properties[:module]
|
16
|
+
done = {}
|
17
|
+
_generate(rule, sb, done)
|
18
|
+
if @properties[:module]
|
19
|
+
code = sb.join(" ")
|
20
|
+
code << "end\n"
|
21
|
+
else
|
22
|
+
code = sb.join
|
23
|
+
end
|
24
|
+
return code
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def _generate(rule, sb=[], done={})
|
29
|
+
return if done.key?(rule.__id__)
|
30
|
+
done[rule.__id__] = rule
|
31
|
+
#
|
32
|
+
if rule.classname
|
33
|
+
unless rule.type == 'map'
|
34
|
+
raise "*** assertion error: rule.type==#{rule.type.inspect}"
|
35
|
+
end
|
36
|
+
assert unless rule.mapping
|
37
|
+
classname = rule.classname
|
38
|
+
classname = classname + '_' if @properties[:ggap]
|
39
|
+
extend = @properties[:parent] ? " < #{@properties[:parent]}" : nil
|
40
|
+
sb << "## #{rule.desc}\n"
|
41
|
+
sb << "class #{classname}#{extend}\n"
|
42
|
+
sb << " include #{@properties[:include]}\n" if @properties[:include]
|
43
|
+
sb << " def initialize(hash)\n"
|
44
|
+
rule.mapping.each do |name, map_rule|
|
45
|
+
next unless name =~ /\A[a-zA-Z_]\w*\z/
|
46
|
+
#if map_rule.sequence && map_rule.sequence[0].classname
|
47
|
+
# item_rule = map_rule.sequence[0]
|
48
|
+
# cname = item_rule.classname
|
49
|
+
# sb << " hash['#{name}'].map!{|e| #{cname}.new(e)} if hash.key?('#{name}') && !hash['#{name}'][0].is_a?(#{cname})\n"
|
50
|
+
#end
|
51
|
+
if map_rule.classname
|
52
|
+
cname = map_rule.classname
|
53
|
+
sb << " @%-10s = (v=hash['#{name}']) && v.is_a?(Hash) ? #{cname}.new(v) : v\n" % name
|
54
|
+
elsif map_rule.sequence && map_rule.sequence[0].classname
|
55
|
+
item_rule = map_rule.sequence[0]
|
56
|
+
cname = item_rule.classname
|
57
|
+
sb << " @%-10s = (v=hash['#{name}']) ? v.map!{|e| e.is_a?(#{cname}) ? e : #{cname}.new(e)} : v\n" % name
|
58
|
+
else
|
59
|
+
sb << " @%-10s = hash['#{name}']\n" % name
|
60
|
+
end
|
61
|
+
end
|
62
|
+
sb << " end\n"
|
63
|
+
rule.mapping.each do |name, map_rule|
|
64
|
+
next unless name =~ /\A[a-zA-Z_]\w*\z/
|
65
|
+
sb << " attr_accessor :%-10s # %s\n" % [name, rule.mapping[name].type]
|
66
|
+
end
|
67
|
+
if @properties[:ggap]
|
68
|
+
sb << "end\n"
|
69
|
+
sb << "class #{rule.classname} < #{classname}\n"
|
70
|
+
end
|
71
|
+
sb << "end\n\n"
|
72
|
+
end
|
73
|
+
#
|
74
|
+
rule.sequence.each do |seq_rule|
|
75
|
+
_generate(seq_rule, sb, done)
|
76
|
+
end if rule.sequence
|
77
|
+
rule.mapping.each do |name, map_rule|
|
78
|
+
_generate(map_rule, sb, done)
|
79
|
+
end if rule.mapping
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
%>
|
84
|
+
<%= generate_classdef(@rule) %>
|
data/lib/kwalify/types.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
2
|
+
### $Rev: 51 $
|
3
|
+
### $Release: 0.6.0 $
|
4
4
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
@@ -8,13 +8,13 @@ require 'date'
|
|
8
8
|
|
9
9
|
|
10
10
|
module Kwalify
|
11
|
-
module Boolean
|
11
|
+
module Boolean # :nodoc:
|
12
12
|
end
|
13
13
|
end
|
14
|
-
class TrueClass
|
14
|
+
class TrueClass # :nodoc:
|
15
15
|
include Kwalify::Boolean
|
16
16
|
end
|
17
|
-
class FalseClass
|
17
|
+
class FalseClass # :nodoc:
|
18
18
|
include Kwalify::Boolean
|
19
19
|
end
|
20
20
|
#module Boolean; end
|
@@ -27,13 +27,13 @@ end
|
|
27
27
|
|
28
28
|
|
29
29
|
module Kwalify
|
30
|
-
module Text
|
30
|
+
module Text # :nodoc:
|
31
31
|
end
|
32
32
|
end
|
33
|
-
class String
|
33
|
+
class String # :nodoc:
|
34
34
|
include Kwalify::Text
|
35
35
|
end
|
36
|
-
class Numeric
|
36
|
+
class Numeric # :nodoc:
|
37
37
|
include Kwalify::Text
|
38
38
|
end
|
39
39
|
#module Text; end
|
@@ -46,32 +46,32 @@ end
|
|
46
46
|
|
47
47
|
|
48
48
|
module Kwalify
|
49
|
-
module Scalar
|
49
|
+
module Scalar # :nodoc:
|
50
50
|
end
|
51
51
|
end
|
52
|
-
class String
|
52
|
+
class String # :nodoc:
|
53
53
|
include Kwalify::Scalar
|
54
54
|
end
|
55
|
-
class Numeric
|
55
|
+
class Numeric # :nodoc:
|
56
56
|
include Kwalify::Scalar
|
57
57
|
end
|
58
|
-
class Date
|
58
|
+
class Date # :nodoc:
|
59
59
|
include Kwalify::Scalar
|
60
60
|
end
|
61
|
-
class Time
|
61
|
+
class Time # :nodoc:
|
62
62
|
include Kwalify::Scalar
|
63
63
|
end
|
64
|
-
class TrueClass
|
64
|
+
class TrueClass # :nodoc:
|
65
65
|
include Kwalify::Scalar
|
66
66
|
end
|
67
|
-
class FalseClass
|
67
|
+
class FalseClass # :nodoc:
|
68
68
|
include Kwalify::Scalar
|
69
69
|
end
|
70
|
-
class NilClass
|
70
|
+
class NilClass # :nodoc:
|
71
71
|
include Kwalify::Scalar
|
72
72
|
end
|
73
73
|
module Kwalify
|
74
|
-
module Text
|
74
|
+
module Text # :nodoc:
|
75
75
|
include Kwalify::Scalar
|
76
76
|
end
|
77
77
|
end
|
@@ -115,7 +115,7 @@ module Kwalify
|
|
115
115
|
#assert_error('type=#{type.inspect}') unless klass
|
116
116
|
return klass
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
def self.get_type_class(type)
|
120
120
|
return type_class(type)
|
121
121
|
end
|