kwalify 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +232 -0
- data/MIT-LICENSE +20 -0
- data/README.txt +16 -19
- data/bin/kwalify +3 -3
- data/contrib/inline-require +6 -4
- data/contrib/kwalify +3719 -2427
- data/doc-api/classes/CommandOptionError.html +17 -17
- data/doc-api/classes/CommandOptionParser.html +63 -63
- data/doc-api/classes/Kwalify.html +29 -7
- data/doc-api/classes/Kwalify/AssertionError.html +9 -9
- data/doc-api/classes/Kwalify/BaseError.html +72 -71
- data/doc-api/classes/Kwalify/BaseParser.html +461 -0
- data/doc-api/classes/Kwalify/CommandOptionError.html +11 -11
- data/doc-api/classes/Kwalify/ErrorHelper.html +51 -46
- data/doc-api/classes/Kwalify/HashInterface.html +13 -135
- data/doc-api/classes/Kwalify/Json.html +105 -0
- data/doc-api/classes/Kwalify/Main.html +129 -126
- data/doc-api/classes/Kwalify/MetaValidator.html +248 -232
- data/doc-api/classes/Kwalify/Parser.html +12 -12
- data/doc-api/classes/Kwalify/PlainYamlParser.html +166 -163
- data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +11 -11
- data/doc-api/classes/Kwalify/Rule.html +152 -130
- data/doc-api/classes/Kwalify/SchemaError.html +10 -10
- data/doc-api/classes/Kwalify/SyntaxError.html +185 -0
- data/doc-api/classes/Kwalify/Types.html +26 -25
- data/doc-api/classes/Kwalify/Util.html +389 -0
- data/doc-api/classes/Kwalify/Util/HashLike.html +246 -0
- data/doc-api/classes/Kwalify/Util/OrderedHash.html +330 -0
- data/doc-api/classes/Kwalify/ValidationError.html +10 -10
- data/doc-api/classes/Kwalify/Validator.html +153 -86
- data/doc-api/classes/Kwalify/Yaml.html +181 -0
- data/doc-api/classes/Kwalify/Yaml/Parser.html +1538 -0
- data/doc-api/classes/Kwalify/YamlParser.html +190 -183
- data/doc-api/classes/Kwalify/YamlSyntaxError.html +8 -57
- data/doc-api/created.rid +1 -1
- data/doc-api/files/__/README_txt.html +17 -22
- data/doc-api/files/kwalify/errors_rb.html +2 -2
- data/doc-api/files/kwalify/main_rb.html +4 -3
- data/doc-api/files/kwalify/messages_rb.html +2 -2
- data/doc-api/files/kwalify/meta-validator_rb.html +3 -3
- data/doc-api/files/kwalify/{util/yaml-helper_rb.html → parser/base_rb.html} +8 -6
- data/doc-api/files/kwalify/parser/yaml_rb.html +117 -0
- data/doc-api/files/kwalify/rule_rb.html +2 -2
- data/doc-api/files/kwalify/types_rb.html +2 -2
- data/doc-api/files/kwalify/util/assert-text-equal_rb.html +2 -2
- data/doc-api/files/kwalify/util/hash-interface_rb.html +9 -2
- data/doc-api/files/kwalify/util/hashlike_rb.html +107 -0
- data/doc-api/files/kwalify/util/option-parser_rb.html +2 -2
- data/doc-api/files/kwalify/util/ordered-hash_rb.html +107 -0
- data/doc-api/files/kwalify/util/testcase-helper_rb.html +2 -2
- data/doc-api/files/kwalify/util_rb.html +107 -0
- data/doc-api/files/kwalify/validator_rb.html +2 -2
- data/doc-api/files/kwalify/yaml-parser_rb.html +2 -2
- data/doc-api/files/kwalify_rb.html +3 -2
- data/doc-api/fr_class_index.html +8 -1
- data/doc-api/fr_file_index.html +5 -1
- data/doc-api/fr_method_index.html +128 -69
- data/doc/img/fig01.png +0 -0
- data/doc/users-guide.html +882 -717
- data/examples/address-book/address-book.schema.yaml +2 -2
- data/examples/data-binding/BABEL.data.yaml +63 -0
- data/examples/data-binding/BABEL.schema.yaml +31 -0
- data/examples/data-binding/Makefile +8 -0
- data/examples/data-binding/Rakefile +13 -0
- data/examples/data-binding/main.rb +27 -0
- data/examples/invoice/invoice.schema.yaml +3 -3
- data/examples/tapkit/tapkit.schema.yaml +2 -2
- data/lib/kwalify.rb +41 -4
- data/lib/kwalify/errors.rb +118 -96
- data/lib/kwalify/kwalify.schema.yaml +58 -0
- data/lib/kwalify/main.rb +384 -377
- data/lib/kwalify/messages.rb +41 -27
- data/lib/kwalify/meta-validator.rb +251 -331
- data/lib/kwalify/parser/base.rb +127 -0
- data/lib/kwalify/parser/yaml.rb +837 -0
- data/lib/kwalify/rule.rb +545 -487
- data/lib/kwalify/templates/genclass-java.eruby +189 -162
- data/lib/kwalify/templates/genclass-php.eruby +104 -0
- data/lib/kwalify/templates/genclass-ruby.eruby +95 -66
- data/lib/kwalify/types.rb +107 -106
- data/lib/kwalify/util.rb +157 -0
- data/lib/kwalify/util/assert-text-equal.rb +33 -31
- data/lib/kwalify/util/hash-interface.rb +11 -30
- data/lib/kwalify/util/hashlike.rb +51 -0
- data/lib/kwalify/util/option-parser.rb +144 -144
- data/lib/kwalify/util/ordered-hash.rb +57 -0
- data/lib/kwalify/util/testcase-helper.rb +3 -3
- data/lib/kwalify/validator.rb +267 -212
- data/lib/kwalify/yaml-parser.rb +822 -768
- data/setup.rb +861 -607
- data/test/Rookbook.yaml +10 -0
- data/test/{tmp.dir/Context.java → data/users-guide/AddressBook.java.expected} +11 -11
- data/test/data/users-guide/BABEL.data.yaml +24 -0
- data/test/data/users-guide/BABEL.schema.yaml +30 -0
- data/test/data/users-guide/ExampleAddressBook.java +47 -0
- data/test/{tmp.dir/Group.java → data/users-guide/Group.java.expected} +2 -11
- data/test/data/users-guide/Person.java.expected +44 -0
- data/test/data/users-guide/address_book.rb +52 -0
- data/test/data/users-guide/address_book.schema.yaml +28 -0
- data/test/data/users-guide/address_book.yaml +27 -0
- data/test/data/users-guide/answers-schema.yaml +12 -0
- data/test/data/users-guide/answers-validator.rb +52 -0
- data/test/data/users-guide/babel_genclass.result +26 -0
- data/test/data/users-guide/config.schema.yaml +7 -0
- data/test/data/users-guide/config.yaml +4 -0
- data/test/{tmp.dir/silent1.document → data/users-guide/document01a.yaml} +0 -0
- data/test/data/users-guide/document01b.yaml +3 -0
- data/test/data/users-guide/document02a.yaml +4 -0
- data/test/data/users-guide/document02b.yaml +4 -0
- data/test/data/users-guide/document03a.yaml +6 -0
- data/test/data/users-guide/document03b.yaml +6 -0
- data/test/data/users-guide/document04a.yaml +9 -0
- data/test/data/users-guide/document04b.yaml +9 -0
- data/test/data/users-guide/document05a.yaml +11 -0
- data/test/data/users-guide/document05b.yaml +12 -0
- data/test/data/users-guide/document06a.yaml +15 -0
- data/test/data/users-guide/document06b.yaml +16 -0
- data/test/data/users-guide/document07a.yaml +9 -0
- data/test/data/users-guide/document07b.yaml +7 -0
- data/test/data/users-guide/document12a.json +10 -0
- data/test/data/users-guide/document12b.json +6 -0
- data/test/data/users-guide/document13a.yaml +17 -0
- data/test/data/users-guide/document14a.yaml +3 -0
- data/test/data/users-guide/document14b.yaml +3 -0
- data/test/data/users-guide/document15a.yaml +6 -0
- data/test/data/users-guide/document15b.yaml +5 -0
- data/test/data/users-guide/example_address_book.rb +10 -0
- data/test/data/users-guide/example_address_book_java.result +32 -0
- data/test/data/users-guide/example_address_book_ruby.result +31 -0
- data/test/data/users-guide/genclass_java.result +4 -0
- data/test/data/users-guide/howto-validation-with-parsing.rb +28 -0
- data/test/data/users-guide/howto-validation.rb +25 -0
- data/test/data/users-guide/howto3.rb +6 -0
- data/test/data/users-guide/howto3.result +5 -0
- data/test/data/users-guide/howto3.yaml +8 -0
- data/test/data/users-guide/howto5_databinding.result +111 -0
- data/test/data/users-guide/invalid01.result +3 -0
- data/test/data/users-guide/invalid02.result +5 -0
- data/test/data/users-guide/invalid03.result +5 -0
- data/test/data/users-guide/invalid04.result +4 -0
- data/test/data/users-guide/invalid05.result +11 -0
- data/test/data/users-guide/invalid06.result +4 -0
- data/test/data/users-guide/invalid07.result +3 -0
- data/test/data/users-guide/invalid08.result +3 -0
- data/test/data/users-guide/invalid12.json +8 -0
- data/test/data/users-guide/invalid14.result +4 -0
- data/test/data/users-guide/invalid15.result +4 -0
- data/test/data/users-guide/loadbabel.rb +27 -0
- data/test/data/users-guide/loadconfig.rb +15 -0
- data/test/data/users-guide/loadconfig.result +2 -0
- data/test/data/users-guide/models.rb +22 -0
- data/test/data/users-guide/option_ha.result +6 -0
- data/test/data/users-guide/option_ha_genclass_java.result +7 -0
- data/test/{tmp.dir/meta1.schema → data/users-guide/schema01.yaml} +0 -0
- data/test/data/users-guide/schema02.yaml +12 -0
- data/test/data/users-guide/schema03.yaml +9 -0
- data/test/data/users-guide/schema04.yaml +20 -0
- data/test/data/users-guide/schema05.yaml +29 -0
- data/test/data/users-guide/schema06.yaml +11 -0
- data/test/data/users-guide/schema12.json +12 -0
- data/test/data/users-guide/schema13.yaml +13 -0
- data/test/data/users-guide/schema14.yaml +5 -0
- data/test/data/users-guide/schema15.yaml +21 -0
- data/test/data/users-guide/valid01.result +2 -0
- data/test/data/users-guide/valid02.result +2 -0
- data/test/data/users-guide/valid03.result +2 -0
- data/test/data/users-guide/valid04.result +2 -0
- data/test/data/users-guide/valid05.result +2 -0
- data/test/data/users-guide/valid06.result +2 -0
- data/test/data/users-guide/valid07.result +2 -0
- data/test/data/users-guide/valid08.result +2 -0
- data/test/data/users-guide/valid12.result +2 -0
- data/test/data/users-guide/valid13.result +2 -0
- data/test/data/users-guide/valid14.result +2 -0
- data/test/data/users-guide/valid15.result +2 -0
- data/test/data/users-guide/validate08.rb +37 -0
- data/test/test-action.rb +78 -0
- data/test/test-action.yaml +738 -0
- data/test/test-databinding.rb +80 -0
- data/test/test-databinding.yaml +301 -0
- data/test/test-main.rb +129 -150
- data/test/test-main.yaml +126 -321
- data/test/test-metavalidator.rb +47 -47
- data/test/test-metavalidator.yaml +77 -21
- data/test/test-parser-yaml.rb +57 -0
- data/test/test-parser-yaml.yaml +1749 -0
- data/test/test-rule.rb +14 -15
- data/test/test-rule.yaml +6 -3
- data/test/test-users-guide.rb +75 -0
- data/test/test-validator.rb +77 -52
- data/test/test-validator.yaml +168 -6
- data/test/test-yaml-parser.rb +47 -0
- data/test/{test-yamlparser.yaml → test-yaml-parser.yaml} +159 -52
- data/test/test.rb +37 -21
- metadata +136 -37
- data/COPYING +0 -340
- data/ChangeLog +0 -70
- data/doc-api/classes/YamlHelper.html +0 -259
- data/lib/kwalify/util/yaml-helper.rb +0 -82
- data/test/test-yamlparser.rb +0 -58
- data/test/tmp.dir/User.java +0 -43
- data/test/tmp.dir/action1.document +0 -18
- data/test/tmp.dir/action1.schema +0 -32
- data/test/tmp.dir/action2.document +0 -18
- data/test/tmp.dir/action2.schema +0 -32
- data/test/tmp.dir/emacs.document +0 -6
- data/test/tmp.dir/emacs.schema +0 -6
- data/test/tmp.dir/meta1.document +0 -0
- data/test/tmp.dir/meta2.document +0 -0
- data/test/tmp.dir/meta2.schema +0 -3
- data/test/tmp.dir/silent1.schema +0 -3
- data/test/tmp.dir/silent2.document +0 -7
- data/test/tmp.dir/silent2.schema +0 -3
- data/test/tmp.dir/stream.invalid +0 -8
- data/test/tmp.dir/stream.schema +0 -3
- data/test/tmp.dir/stream.valid +0 -8
- data/test/tmp.dir/untabify.document +0 -5
- data/test/tmp.dir/untabify.schema +0 -10
- data/todo.txt +0 -34
@@ -1,84 +1,113 @@
|
|
1
1
|
<%
|
2
2
|
|
3
|
+
###
|
4
|
+
### $Rev: 96 $
|
5
|
+
### $Release: 0.7.0 $
|
6
|
+
### copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
7
|
+
###
|
8
|
+
|
3
9
|
if @describe
|
4
10
|
sb = []
|
5
11
|
sb << " --module=name : module name in which class defined\n"
|
6
12
|
sb << " --parent=name : parent class name\n"
|
7
13
|
sb << " --include=name : module name which all classes include\n"
|
14
|
+
sb << " --initialize=false : not print initialize() method\n"
|
15
|
+
sb << " --hashlike : include Kwalify::Util::HashLike module\n"
|
8
16
|
#sb << " --ggap : use generation gap pattern\n"
|
9
17
|
return sb.join
|
10
18
|
end
|
11
19
|
|
12
20
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
require 'kwalify/util/ordered-hash'
|
22
|
+
|
23
|
+
def generate_classdef(schema)
|
24
|
+
hash = Kwalify::Util::OrderedHash.new
|
25
|
+
#hash.instance_eval { alias []= add }
|
26
|
+
modname = @properties[:module]
|
27
|
+
Kwalify::Util.traverse_schema(schema) do |rulehash|
|
28
|
+
if rulehash['class']
|
29
|
+
arr = _generate_classdef(rulehash)
|
30
|
+
key = rulehash['class']
|
31
|
+
classdef = modname ? arr.join(' ') : arr.join()
|
32
|
+
#hash[key] = classdef if !hash.key?(key) || classdef != hash[key]
|
33
|
+
hash[key] = classdef if !hash.key?(key) || classdef.length > hash[key].length
|
34
|
+
end
|
35
|
+
end
|
36
|
+
sb = ''
|
37
|
+
sb << "require 'kwalify/util/hashlike'\n" if @properties[:hashlike]
|
38
|
+
sb << "\n" if @properties[:hashlike]
|
39
|
+
sb << "module #{@properties[:module]}\n" if modname
|
40
|
+
sb << "\n" if modname
|
41
|
+
hash.each do |classname, classdef|
|
42
|
+
sb << classdef
|
43
|
+
sb << "\n"
|
44
|
+
end
|
45
|
+
sb << "end\n" if modname
|
46
|
+
sb << "\n" if modname
|
47
|
+
return sb.chomp
|
25
48
|
end
|
26
49
|
|
27
50
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
51
|
+
def _generate_classdef(rulehash)
|
52
|
+
return nil unless rulehash['class']
|
53
|
+
assert unless rulehash['mapping']
|
54
|
+
classname = rulehash['class']
|
55
|
+
classname = classname + '_' if @properties[:ggap]
|
56
|
+
extend = @properties[:parent] ? " < #{@properties[:parent]}" : nil
|
57
|
+
flag_init = @properties[:initialize] != false
|
58
|
+
arr = ['']
|
59
|
+
arr << "## #{rulehash['desc']}\n"
|
60
|
+
arr << "class #{classname}#{extend}\n"
|
61
|
+
arr << " include Kwalify::Util::HashLike\n" if @properties[:hashlike]
|
62
|
+
arr << " include #{@properties[:include]}\n" if @properties[:include]
|
63
|
+
arr1 = []
|
64
|
+
arr2 = [] # accessors
|
65
|
+
arr3 = [] # boolean accessors
|
66
|
+
arr4 = [] # default values
|
67
|
+
v = nil
|
68
|
+
rulehash['mapping'].each do |name, map_rulehash|
|
69
|
+
next unless name =~ /\A[a-zA-Z_][-\w]*\z/
|
70
|
+
name2 = name.gsub(/-/, '_')
|
71
|
+
var = "%-16s" % name2
|
72
|
+
len = 13 - name2.length
|
73
|
+
len = 0 if len < 0
|
74
|
+
spc = ' ' * len
|
75
|
+
arr2 << " attr_accessor :#{var} # #{map_rulehash['type'] || 'str'}\n"
|
76
|
+
flag_bool = (t = map_rulehash['type']) == 'bool' || t == 'boolean'
|
77
|
+
arr3 << " def #{name2}?#{spc}; @#{name2}#{spc}; end\n" if flag_bool
|
78
|
+
arr4 << " @#{var} = #{v.inspect}\n" unless (v=map_rulehash['default']).nil?
|
79
|
+
if !flag_init
|
80
|
+
# pass
|
81
|
+
elsif map_rulehash['class']
|
82
|
+
cname = map_rulehash['class']
|
83
|
+
arr1 << " @#{var} = (v=hash['#{name}']) && v.is_a?(Hash) ? #{cname}.new(v) : v\n"
|
84
|
+
elsif map_rulehash['sequence'] && map_rulehash['sequence'][0]['class']
|
85
|
+
item_rulehash = map_rulehash['sequence'][0]
|
86
|
+
cname = item_rulehash['class']
|
87
|
+
arr1 << " @#{var} = (v=hash['#{name}']) ? v.map!{|e| e.is_a?(#{cname}) ? e : #{cname}.new(e)} : v\n"
|
88
|
+
elsif map_rulehash.key?('default')
|
89
|
+
#arr << " @#{var} = hash.key?('#{name}') ? hash['#{name}'] : #{map_rulehash['default'].inspect}\n"
|
90
|
+
arr1 << " @#{var} = (v=hash['#{name}']).nil? ? #{map_rulehash['default'].inspect} : v\n"
|
91
|
+
else
|
92
|
+
arr1 << " @#{var} = hash['#{name}']\n"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
arr << " def initialize(hash=nil)\n" if flag_init
|
96
|
+
arr << " if hash.nil?\n" if flag_init
|
97
|
+
arr.concat arr4 if flag_init
|
98
|
+
arr << " return\n" if flag_init
|
99
|
+
arr << " end\n" if flag_init
|
100
|
+
arr.concat arr1 if flag_init
|
101
|
+
arr << " end\n" if flag_init
|
102
|
+
arr.concat(arr2)
|
103
|
+
arr.concat(arr3)
|
104
|
+
if @properties[:ggap]
|
105
|
+
arr << "end\n"
|
106
|
+
arr << "class #{rulehash['class']} < #{classname}\n"
|
107
|
+
end
|
108
|
+
arr << "end\n"
|
109
|
+
return arr
|
80
110
|
end
|
81
111
|
|
82
|
-
|
83
112
|
%>
|
84
|
-
<%= generate_classdef(@
|
113
|
+
<%= generate_classdef(@schema) %>
|
data/lib/kwalify/types.rb
CHANGED
@@ -1,155 +1,156 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
4
|
-
### copyright(c) 2005 kuwata-lab all rights reserved.
|
2
|
+
### $Rev: 87 $
|
3
|
+
### $Release: 0.7.0 $
|
4
|
+
### copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
7
7
|
require 'date'
|
8
8
|
|
9
9
|
|
10
10
|
module Kwalify
|
11
|
-
|
12
|
-
|
11
|
+
module Boolean # :nodoc:
|
12
|
+
end
|
13
13
|
end
|
14
14
|
class TrueClass # :nodoc:
|
15
|
-
|
15
|
+
include Kwalify::Boolean
|
16
16
|
end
|
17
|
-
class FalseClass
|
18
|
-
|
17
|
+
class FalseClass # :nodoc:
|
18
|
+
include Kwalify::Boolean
|
19
19
|
end
|
20
20
|
#module Boolean; end
|
21
21
|
#class TrueClass
|
22
|
-
#
|
22
|
+
# include Boolean
|
23
23
|
#end
|
24
24
|
#class FalseClass
|
25
|
-
#
|
25
|
+
# include Boolean
|
26
26
|
#end
|
27
27
|
|
28
28
|
|
29
29
|
module Kwalify
|
30
|
-
|
31
|
-
|
30
|
+
module Text # :nodoc:
|
31
|
+
end
|
32
32
|
end
|
33
33
|
class String # :nodoc:
|
34
|
-
|
34
|
+
include Kwalify::Text
|
35
35
|
end
|
36
|
-
class Numeric
|
37
|
-
|
36
|
+
class Numeric # :nodoc:
|
37
|
+
include Kwalify::Text
|
38
38
|
end
|
39
39
|
#module Text; end
|
40
40
|
#class String
|
41
|
-
#
|
41
|
+
# include Text
|
42
42
|
#end
|
43
43
|
#class Numeric
|
44
|
-
#
|
44
|
+
# include Text
|
45
45
|
#end
|
46
46
|
|
47
47
|
|
48
48
|
module Kwalify
|
49
|
-
|
50
|
-
|
49
|
+
module Scalar # :nodoc:
|
50
|
+
end
|
51
51
|
end
|
52
|
-
class String
|
53
|
-
|
52
|
+
class String # :nodoc:
|
53
|
+
include Kwalify::Scalar
|
54
54
|
end
|
55
|
-
class Numeric
|
56
|
-
|
55
|
+
class Numeric # :nodoc:
|
56
|
+
include Kwalify::Scalar
|
57
57
|
end
|
58
|
-
class Date
|
59
|
-
|
58
|
+
class Date # :nodoc:
|
59
|
+
include Kwalify::Scalar
|
60
60
|
end
|
61
|
-
class Time
|
62
|
-
|
61
|
+
class Time # :nodoc:
|
62
|
+
include Kwalify::Scalar
|
63
63
|
end
|
64
|
-
class TrueClass
|
65
|
-
|
64
|
+
class TrueClass # :nodoc:
|
65
|
+
include Kwalify::Scalar
|
66
66
|
end
|
67
|
-
class FalseClass
|
68
|
-
|
67
|
+
class FalseClass # :nodoc:
|
68
|
+
include Kwalify::Scalar
|
69
69
|
end
|
70
70
|
class NilClass # :nodoc:
|
71
|
-
|
71
|
+
include Kwalify::Scalar
|
72
72
|
end
|
73
73
|
module Kwalify
|
74
|
-
|
75
|
-
|
76
|
-
|
74
|
+
module Text # :nodoc:
|
75
|
+
include Kwalify::Scalar
|
76
|
+
end
|
77
77
|
end
|
78
78
|
|
79
79
|
|
80
80
|
module Kwalify
|
81
81
|
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
83
|
+
module Types
|
84
|
+
|
85
|
+
|
86
|
+
DEFAULT_TYPE = "str" ## use "str" as default of @type
|
87
|
+
|
88
|
+
@@type_table = {
|
89
|
+
"seq" => Array,
|
90
|
+
"map" => Hash,
|
91
|
+
"str" => String,
|
92
|
+
#"string" => String,
|
93
|
+
"text" => Text,
|
94
|
+
"int" => Integer,
|
95
|
+
#"integer" => Integer,
|
96
|
+
"float" => Float,
|
97
|
+
"number" => Numeric,
|
98
|
+
#"numeric" => Numeric,
|
99
|
+
"date" => Date,
|
100
|
+
"time" => Time,
|
101
|
+
"timestamp" => Time,
|
102
|
+
"bool" => Boolean,
|
103
|
+
#"boolean" => Boolean,
|
104
|
+
#"object" => Object,
|
105
|
+
"any" => Object,
|
106
|
+
"scalar" => Scalar,
|
107
|
+
}
|
108
|
+
|
109
|
+
def self.type_table
|
110
|
+
return @@type_table
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.type_class(type)
|
114
|
+
klass = @@type_table[type]
|
115
|
+
#assert_error('type=#{type.inspect}') unless klass
|
116
|
+
return klass
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.get_type_class(type)
|
120
|
+
return type_class(type)
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
#--
|
126
|
+
#def collection_class?(klass)
|
127
|
+
# return klass.is_a?(Array) || klass.is_a?(Hash)
|
128
|
+
#end
|
129
|
+
#
|
130
|
+
#def scalar_class?(klass)
|
131
|
+
# return !klass.is_a?(Array) && !klass.is_a?(Hash) && klass != Object
|
132
|
+
#end
|
133
|
+
|
134
|
+
def collection?(val)
|
135
|
+
return val.is_a?(Array) || val.is_a?(Hash)
|
136
|
+
end
|
137
|
+
|
138
|
+
def scalar?(val)
|
139
|
+
#return !val.is_a?(Array) && !val.is_a?(Hash) && val.class != Object
|
140
|
+
return val.is_a?(Kwalify::Scalar) #&& val.class != Object
|
141
|
+
end
|
142
|
+
|
143
|
+
def collection_type?(type)
|
144
|
+
return type == 'seq' || type == 'map'
|
145
|
+
end
|
146
|
+
|
147
|
+
def scalar_type?(type)
|
148
|
+
return type != 'seq' && type != 'map' && type == 'any'
|
149
|
+
end
|
150
|
+
|
151
|
+
module_function 'collection?', 'scalar?', 'collection_type?', 'scalar_type?'
|
152
|
+
end
|
153
|
+
|
154
|
+
extend Types
|
154
155
|
|
155
156
|
end
|
data/lib/kwalify/util.rb
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
###
|
2
|
+
### $Rev: 88 $
|
3
|
+
### $Release: 0.7.0 $
|
4
|
+
### copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
5
|
+
###
|
6
|
+
|
7
|
+
module Kwalify
|
8
|
+
|
9
|
+
module Util
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
##
|
14
|
+
## expand tab character to spaces
|
15
|
+
##
|
16
|
+
## ex.
|
17
|
+
## untabified_str = YamlHelper.untabify(tabbed_str)
|
18
|
+
##
|
19
|
+
def untabify(str, width=8)
|
20
|
+
list = str.split(/\t/)
|
21
|
+
last = list.pop
|
22
|
+
sb = ''
|
23
|
+
list.each do |s|
|
24
|
+
column = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
|
25
|
+
n = width - (column % width)
|
26
|
+
sb << s << (' ' * n)
|
27
|
+
end
|
28
|
+
sb << last if last
|
29
|
+
return sb
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
## traverse schema
|
34
|
+
##
|
35
|
+
## ex.
|
36
|
+
## schema = YAML.load_file('myschema.yaml')
|
37
|
+
## Kwalify::Util.traverse_schema(schema) do |rulehash|
|
38
|
+
## ## add module prefix to class name
|
39
|
+
## if rulehash['class']
|
40
|
+
## rulehash['class'] = "MyModule::' + rulehash['class']
|
41
|
+
## end
|
42
|
+
## end
|
43
|
+
def traverse_schema(schema, &block) #:yield: rulehash
|
44
|
+
hash = schema
|
45
|
+
_done = {}
|
46
|
+
_traverse_schema(hash, _done, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def _traverse_schema(hash, _done={}, &block)
|
50
|
+
return if _done.key?(hash.__id__)
|
51
|
+
_done[hash.__id__] = hash
|
52
|
+
yield hash
|
53
|
+
if hash['mapping']
|
54
|
+
hash['mapping'].each {|k, v| _traverse_schema(v, _done, &block) }
|
55
|
+
elsif hash['sequence']
|
56
|
+
_traverse_schema(hash['sequence'][0], _done, &block)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
private :_traverse_schema
|
60
|
+
|
61
|
+
|
62
|
+
## traverse rule
|
63
|
+
##
|
64
|
+
## ex.
|
65
|
+
## schema = YAML.load_file('myschema.yaml')
|
66
|
+
## validator = Kwalify::Validator.new(schema)
|
67
|
+
## Kwalify::Util.traverse_rule(validator) do |rule|
|
68
|
+
## p rule if rule.classname
|
69
|
+
## end
|
70
|
+
def traverse_rule(validator, &block) #:yield: rule
|
71
|
+
rule = validator.is_a?(Rule) ? validator : validator.rule
|
72
|
+
_done = {}
|
73
|
+
_traverse_rule(rule, _done, &block)
|
74
|
+
end
|
75
|
+
|
76
|
+
def _traverse_rule(rule, _done={}, &block)
|
77
|
+
return if _done.key?(rule.__id__)
|
78
|
+
_done[rule.__id__] = rule
|
79
|
+
yield rule
|
80
|
+
rule.sequence.each do |seq_rule|
|
81
|
+
_traverse_rule(seq_rule, _done, &block)
|
82
|
+
end if rule.sequence
|
83
|
+
rule.mapping.each do |name, map_rule|
|
84
|
+
_traverse_rule(map_rule, _done, &block)
|
85
|
+
end if rule.mapping
|
86
|
+
end
|
87
|
+
private :_traverse_rule
|
88
|
+
|
89
|
+
|
90
|
+
##
|
91
|
+
## get class object. if not found, NameError raised.
|
92
|
+
##
|
93
|
+
def get_class(classname)
|
94
|
+
klass = Object
|
95
|
+
classname.split('::').each do |name|
|
96
|
+
klass = klass.const_get(name)
|
97
|
+
end
|
98
|
+
return klass
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
##
|
103
|
+
## create a hash table from list of hash with primary key.
|
104
|
+
##
|
105
|
+
## ex.
|
106
|
+
## hashlist = [
|
107
|
+
## { "name"=>"Foo", "gender"=>"M", "age"=>20, },
|
108
|
+
## { "name"=>"Bar", "gender"=>"F", "age"=>25, },
|
109
|
+
## { "name"=>"Baz", "gender"=>"M", "age"=>30, },
|
110
|
+
## ]
|
111
|
+
## hashtable = YamlHelper.create_hashtable(hashlist, "name")
|
112
|
+
## p hashtable
|
113
|
+
## # => { "Foo" => { "name"=>"Foo", "gender"=>"M", "age"=>20, },
|
114
|
+
## # "Bar" => { "name"=>"Bar", "gender"=>"F", "age"=>25, },
|
115
|
+
## # "Baz" => { "name"=>"Baz", "gender"=>"M", "age"=>30, }, }
|
116
|
+
##
|
117
|
+
def create_hashtable(hashlist, primarykey, flag_duplicate_check=true)
|
118
|
+
hashtable = {}
|
119
|
+
hashlist.each do |hash|
|
120
|
+
key = hash[primarykey]
|
121
|
+
unless key
|
122
|
+
riase "primary key '#{key}' not found."
|
123
|
+
end
|
124
|
+
if flag_duplicate_check && hashtable.key?(key)
|
125
|
+
raise "primary key '#{key}' duplicated (value '#{hashtable[key]}')"
|
126
|
+
end
|
127
|
+
hashtable[key] = hash
|
128
|
+
end if hashlist
|
129
|
+
return hashtable
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
##
|
134
|
+
## get nested value directly.
|
135
|
+
##
|
136
|
+
## ex.
|
137
|
+
## val = YamlHelper.get_value(obj, ['aaa', 0, 'xxx'])
|
138
|
+
##
|
139
|
+
## This is equal to the following:
|
140
|
+
## begin
|
141
|
+
## val = obj['aaa'][0]['xxx']
|
142
|
+
## rescue NameError
|
143
|
+
## val = nil
|
144
|
+
## end
|
145
|
+
##
|
146
|
+
def get_value(obj, path)
|
147
|
+
val = obj
|
148
|
+
path.each do |key|
|
149
|
+
return nil unless val.is_a?(Hash) || val.is_a?(Array)
|
150
|
+
val = val[key]
|
151
|
+
end if path
|
152
|
+
return val
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|