rogdl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rogdl.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'lib/node'
2
+ require 'lib/parser'
3
+ require 'lib/schema'
4
+ require 'lib/writer'
5
+ require 'lib/nil'
6
+ require 'lib/string'
7
+ require 'lib/array'
8
+ require 'lib/hash'
9
+ require 'lib/fixnum'
10
+ require 'lib/symbol'
11
+
12
+ module Rogdl
13
+ VERSION = '0.1.0'
14
+ end
data/lib/schema.rb ADDED
@@ -0,0 +1,140 @@
1
+ module Rogdl
2
+ class Schema
3
+
4
+ ExactlyOne = '{1}'
5
+ OneOrMore = '{1..n}'
6
+ ZeroOrOne = '{0..1}'
7
+ ZeroOrMore = '{0..n}'
8
+
9
+ Rules = [ExactlyOne, OneOrMore, ZeroOrOne, ZeroOrMore]
10
+
11
+
12
+
13
+ def initialize(aSchema)
14
+ @schema = aSchema
15
+ end
16
+
17
+ def validate(aNode)
18
+ messages = []
19
+ if @schema.gname != aNode.gname
20
+ return ["Expecting '#{@schema.gname}', but found '#{aNode.gname}'"]
21
+ end
22
+
23
+ val(aNode, @schema, messages)
24
+ return messages
25
+ end
26
+
27
+ private
28
+ def val(aNode, schemaNode, messages)
29
+
30
+ schemaNode.each do |schemaChild|
31
+
32
+ if !Rules.include?(schemaChild.gname)
33
+ # puts schemaChild.gname + schemaChild.gvalue
34
+
35
+ if schemaChild.gvalue == OneOrMore
36
+ if aNode.find_all_named(schemaChild.gname).length < 1
37
+ messages << "Found node named '#{aNode.gname}' that should have one or more children named: '#{schemaChild.gname}'"
38
+ end
39
+ elsif schemaChild.gvalue == ExactlyOne
40
+ if aNode.find_all_named(schemaChild.gname).length != 1
41
+ messages << "Found node named '#{aNode.gname}' that should have exactly one child named: '#{schemaChild.gname}'"
42
+ end
43
+ elsif schemaChild.gvalue == ZeroOrOne
44
+ if aNode.find_all_named(schemaChild.gname).length > 1
45
+ messages << "Found node named '#{aNode.gname}' that should have zero or one child named: '#{schemaChild.gname}'"
46
+ end
47
+ elsif schemaChild.gvalue == ZeroOrMore
48
+ # purposely left blank
49
+ else
50
+ throw "Invalid rule: ''#{schemaChild.gvalue}'"
51
+ end
52
+
53
+
54
+ aNode.find_all_named(schemaChild.gname).each do |aNodeChild|
55
+ val(aNodeChild, schemaChild, messages)
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+ end # val
62
+
63
+ # def val(aNode, schemaNode, messages)
64
+ #
65
+ # if schemaNode.gname != aNode.gname
66
+ # messages << "Expecting '#{schemaNode.gname}', but found '#{aNode.gname}'"
67
+ # return
68
+ # end
69
+ #
70
+ # puts schemaNode.gvalue
71
+ # if schemaNode.gvalue == ExactlyOne
72
+ # if !aNode.gparent.nil?
73
+ # siblings = aNode.gparent.find_all_named(schemaNode.gname)
74
+ # if siblings.length != 1
75
+ # messages << "Found node named '#{gparent.gname}' that should have exactly one child named: '#{aNode.gname}'"
76
+ # end
77
+ # end
78
+ # elsif schemaNode.gvalue == OneOrMore
79
+ # if !aNode.gparent.nil?
80
+ # siblings = aNode.gparent.find_all_named(schemaNode.gname)
81
+ # if siblings.length < 1
82
+ # messages << "Found node named '#{gparent.gname}' that should have one or more children named: '#{aNode.gname}'"
83
+ # end
84
+ # end
85
+ # end
86
+ #
87
+ # schemaNode.each do |schemaChild|
88
+ #
89
+ # if !Rules.include?(schemaChild.gname)
90
+ # puts schemaChild.gname
91
+ # aNode.find_all_named(schemaChild.gname).each do |aNodeChild|
92
+ # val(aNodeChild, schemaChild, messages)
93
+ # end
94
+ # end
95
+ # end
96
+ #
97
+ # end # val
98
+
99
+ # def val(aNode, messages)
100
+ # schemaNode = @schema[aNode.gname]
101
+ #
102
+ #
103
+ #
104
+ # return if schemaNode.nil?
105
+ # schemaNode.entries.each do |rule|
106
+ # if rule.gname == ExactlyOne
107
+ # rule.entries.each do |expected|
108
+ # if aNode.find_all_named(expected.gname).length != 1
109
+ # messages << "Found node named '#{aNode.gname}' that should have exactly one child named: '#{expected.gname}'"
110
+ # end
111
+ # end
112
+ # elsif rule.gname == OneOrMore
113
+ # rule.entries.each do |expected|
114
+ # if aNode.find_all_named(expected.gname).length == 0
115
+ # messages << "Found node named '#{aNode.gname}' missing expected child named: '#{expected.gname}'"
116
+ # end
117
+ # end
118
+ # elsif rule.gname == ZeroOrOne
119
+ # rule.entries.each do |expected|
120
+ # if aNode.find_all_named(expected.gname).length > 1
121
+ # messages << "Found node named '#{aNode.gname}' having too many children named: '#{expected.gname}'"
122
+ # end
123
+ # end
124
+ # elsif rule.gname == ZeroOrMore
125
+ # #purposely left blank
126
+ # elsif rule.gname == ExactlyOneChild
127
+ # if aNode.glength != 1
128
+ # messages << "Found node named '#{aNode.gname}' that should have exactly one child"
129
+ # end
130
+ # end
131
+ #
132
+ # end # schemaNode.entries.each
133
+ #
134
+ # aNode.entries.each {|child| val(child, messages)}
135
+ #
136
+ # end # val
137
+
138
+
139
+ end
140
+ end
data/lib/string.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'lib/rogdl'
2
+
3
+ class String
4
+
5
+ def camel_case
6
+ str = upper_camel_case
7
+ return str[0,1].downcase + str[1, str.length]
8
+ end
9
+
10
+ def upper_camel_case
11
+ str = self
12
+ str.gsub!(/[^a-zA-Z_\- ]/," ")
13
+ str = " " + str.split.join(" ")
14
+ str.gsub!(/ (.)/) { $1.upcase }
15
+
16
+ str.gsub!(/[^a-zA-Z_\- ]/,"_")
17
+ str = "_" + str.split.join("_")
18
+ str.gsub!(/_(.)/) { $1.upcase }
19
+
20
+ return str
21
+ end
22
+
23
+ def pluralize
24
+ # TODO: replace with call to Inflector.pluralize
25
+ return self + 's' if self[length-1, length] != 's'
26
+ return self + 'es' if self[length-1, length] == 's'
27
+ end
28
+
29
+ def to_b
30
+ self == 'true'
31
+ end
32
+
33
+ def to_n
34
+ return Rogdl::Node.new(self)
35
+ end
36
+
37
+
38
+ end
data/lib/symbol.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'lib/rogdl'
2
+
3
+ class Symbol
4
+
5
+ def to_n
6
+ return Rogdl::Node.new(":#{self.to_s}")
7
+ end
8
+
9
+ end
data/lib/writer.rb ADDED
@@ -0,0 +1,117 @@
1
+ require 'lib/node'
2
+ require 'erb'
3
+ require 'find'
4
+
5
+ module Rogdl
6
+
7
+ class Writer
8
+
9
+
10
+ def templates
11
+ @templates ||= {}
12
+ end
13
+
14
+ def write(nodeArray)
15
+ nodes = to_node_array(nodeArray)
16
+ a = []
17
+ nodes.each do |node|
18
+ raise "Writer::write: cannot find template" unless !templates[node.gname].nil?
19
+ node.assign(self)
20
+ a << node.render
21
+ end
22
+ return a
23
+ end
24
+
25
+ def write_to_files(nodeArray, namingProc)
26
+ nodes = to_node_array(nodeArray)
27
+ output_array = write(nodes)
28
+ throw 'Illegal state: lengths do not match' if output_array.length != nodes.length
29
+
30
+ 0..output_array.length.times do |index|
31
+ filename = namingProc.call(nodes[index])
32
+ f = File.new(filename, 'w+')
33
+ f << output_array[index]
34
+ f.close
35
+ end
36
+
37
+ end
38
+
39
+ def load_templates(dir)
40
+ excludes = ["CVS",".svn"]
41
+ Find.find(dir) do |path|
42
+ if FileTest.directory?(path)
43
+ if excludes.include?(File.basename(path))
44
+ Find.prune # Don't look any further into this directory.
45
+ else
46
+ next
47
+ end
48
+ end
49
+
50
+ if File.extname(path) == '.rhtml'
51
+ file = File.new(path)
52
+ s = ''
53
+ file.each_line {|line| s << line }
54
+ templates[File.basename(path).split('.').first] = s
55
+ end
56
+ end
57
+ return self
58
+ end # load_templates
59
+
60
+ private
61
+ def to_node_array(node_or_array)
62
+ if node_or_array.is_a?(Node)
63
+ return [node_or_array]
64
+ elsif node_or_array.is_a?(Array) && node_or_array.all? {|node| node.is_a?(Node)}
65
+ return node_or_array
66
+ else
67
+ raise "nodes must be a Node or an Array of Node"
68
+ end
69
+ end
70
+
71
+
72
+ end
73
+
74
+ class Node
75
+
76
+ def assign(pwriter)
77
+ @@writer = pwriter
78
+ end
79
+
80
+ def render(template_name=gname)
81
+ template = @@writer.templates[template_name]
82
+ throw "no template defined for #{template_name}" if template.nil?
83
+ return ERB.new(template).result(self.context)
84
+ end
85
+
86
+ def render_child_named(aName)
87
+ return self[aName].render
88
+ end
89
+
90
+ def render_children
91
+ s = ""
92
+ self.each {|child| s << child.render}
93
+ return s
94
+ end
95
+
96
+ def render_children_named(aName)
97
+ if aName.is_a?(String)
98
+ s = ""
99
+ self.find_all_named(aName).each {|child| s << child.render}
100
+ return s
101
+ elsif aName.is_a?(Array)
102
+ s = ""
103
+ self.each {|child| s << child.render if aName.include?(child.gname)}
104
+ return s
105
+ else
106
+ throw 'aName must be a String or Array'
107
+ end
108
+ end
109
+
110
+ def context
111
+ binding
112
+ end
113
+
114
+ end
115
+
116
+
117
+ end
@@ -0,0 +1,30 @@
1
+ require 'test/unit'
2
+ require 'lib/rogdl'
3
+
4
+ class TestArray < Test::Unit::TestCase
5
+ include Rogdl
6
+
7
+ def test_empty
8
+ assert_equal nil, [].to_n
9
+ end
10
+
11
+ def test_one
12
+ assert_equal 'a', ['a'].to_n.gname
13
+ end
14
+
15
+ def test_two
16
+ assert_equal 'a', ['a','b'].to_n.gname
17
+ assert_equal 'b', ['a','b'].to_n.b.gname
18
+ end
19
+
20
+ def test_nest
21
+ assert_equal 'a', ['a',['b']].to_n.gname
22
+ assert_equal 'b', ['a',['b']].to_n.b.gname
23
+
24
+ assert_equal 'c', ['a',['b','c']].to_n.b.c.gname
25
+ assert_equal 'c', ['a',['b','c']].to_n.b.gvalue
26
+ end
27
+
28
+
29
+
30
+ end
@@ -0,0 +1,13 @@
1
+ require 'test/unit'
2
+ require 'lib/rogdl'
3
+
4
+ class TestFixnum < Test::Unit::TestCase
5
+ include Rogdl
6
+
7
+ def test_one
8
+ assert_equal '1', 1.to_n.gname
9
+ end
10
+
11
+
12
+
13
+ end
data/test/test_hash.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'lib/rogdl'
3
+
4
+ class TestHash < Test::Unit::TestCase
5
+ include Rogdl
6
+
7
+ def test_empty
8
+ assert_equal nil, {}.to_n
9
+ end
10
+
11
+ def test_one
12
+ assert_equal 'a', {'a'=>'b'}.to_n.gname
13
+ assert_equal 'b', {'a'=>'b'}.to_n.b.gname
14
+ end
15
+
16
+
17
+
18
+
19
+ end
data/test/test_nil.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'test/unit'
2
+ require 'lib/rogdl'
3
+
4
+ class TestNil < Test::Unit::TestCase
5
+ include Rogdl
6
+
7
+ def test_gvalue
8
+ assert_equal nil, nil.gvalue
9
+ end
10
+
11
+ def test_to_b
12
+ assert_equal false, nil.to_b
13
+ end
14
+
15
+ def test_to_n
16
+ assert_equal nil, nil.to_n
17
+ end
18
+
19
+ def test_find_all_named
20
+ assert_equal [], nil.find_all_named('anything')
21
+ end
22
+
23
+ def test_gkids
24
+ assert_equal [], nil.gkids('anything')
25
+ end
26
+
27
+
28
+
29
+
30
+ end
data/test/test_node.rb ADDED
@@ -0,0 +1,279 @@
1
+ require 'test/unit'
2
+ require 'lib/rogdl'
3
+
4
+ class TestNode < Test::Unit::TestCase
5
+ include Rogdl
6
+
7
+ def test_initialize
8
+ parent = Node.new("parent")
9
+ assert_equal parent, parent
10
+ assert_equal "parent", parent.gname
11
+ assert_equal [], parent.entries
12
+
13
+ assert_equal "child", Node.new("parent").add("child")[0].gname
14
+ end
15
+
16
+ def test_add
17
+ parent = Node.new("parent")
18
+ assert_equal true, parent.empty?
19
+ child = Node.new("child")
20
+ parent.add child
21
+ assert_equal "child", parent[0].gname
22
+ assert_equal "child", parent["child"].gname
23
+ assert_equal "parent", child.gparent.gname
24
+
25
+ parent.add(Node.new("child"))
26
+ assert_equal "child", parent["child"].gname
27
+ assert_equal "child", parent.entries[0].gname
28
+ assert_equal "child", parent.entries[1].gname
29
+
30
+ parent.add(Node.new("sibling"))
31
+ assert_equal "sibling", parent.entries[2].gname
32
+ end
33
+
34
+ def test_add_string
35
+ parent = Node.new("parent")
36
+
37
+ parent.add('child').add('child')
38
+ assert_equal "child", parent[0].gname
39
+ assert_equal "child", parent[1].gname
40
+ end
41
+
42
+ def test_assign
43
+ parent = Node.new("parent")
44
+ parent['child'] = 'c1'
45
+ assert_equal 'child', parent.child.gname
46
+ assert_equal 'c1', parent.child.gval
47
+ assert_equal 'c1', parent['child'].gval
48
+
49
+ parent['child'] = 'c2'
50
+ assert_equal 'child', parent.child.gname
51
+ assert_equal 'c1', parent.child.gval
52
+ assert_equal 'c1', parent['child'].gval
53
+
54
+ assert_equal 2, parent.childzz.length
55
+ assert_equal 'c2', parent[1].gval
56
+
57
+ assert_equal 'c3', (parent['child']= 'c3')
58
+ end
59
+
60
+ def test_find_all_named
61
+ parent = Node.new("parent")
62
+ assert_equal [], parent.find_all_named("child")
63
+
64
+ parent.add Node.new("child")
65
+ assert_equal ["child"], parent.find_all_named("child").collect {|node| node.gname}
66
+ assert_equal ["child"], parent.find_all_named(["child"]).collect {|node| node.gname}
67
+
68
+ parent.add Node.new("sibling")
69
+ assert_equal ["child"], parent.find_all_named("child").collect {|node| node.gname}
70
+ assert_equal ["child", "sibling"], parent.find_all_named(["child", "sibling"]).collect {|node| node.gname}
71
+
72
+ parent.add Node.new("child")
73
+ assert_equal ["child", "child"], parent.find_all_named("child").collect {|node| node.gname}
74
+
75
+ end
76
+
77
+ def test_gkids
78
+ # gkids is an alias for find_all_named
79
+ parent = Node.new("parent")
80
+ parent.add Node.new("child")
81
+ parent.add Node.new("child")
82
+ assert_equal ["child", "child"], parent.gkids('child').collect {|node| node.gname}
83
+ end
84
+
85
+ def test_empty?
86
+ parent = Node.new("parent")
87
+ assert_equal true, parent.empty?
88
+
89
+ parent.add Node.new("sibling")
90
+ assert_equal false, parent.empty?
91
+ end
92
+
93
+ def test_brackets
94
+ parent = Node.new("parent")
95
+ assert_equal nil, parent["child"]
96
+
97
+ child = Node.new("child")
98
+ parent.add child
99
+ assert_equal "child", parent["child"].gname
100
+ assert_equal "child", parent[0].gname
101
+ end
102
+
103
+ def test_method_missing
104
+ parent = Node.new("parent")
105
+
106
+ child = Node.new("child")
107
+ parent.add child
108
+ assert_equal "child", parent.child.gname
109
+ assert_equal "child", parent[0].gname
110
+ end
111
+
112
+ def test_methodzz_missing
113
+ parent = Node.new("parent")
114
+ assert_equal [], parent.childzz
115
+
116
+ parent.add('child')
117
+ assert_equal 'child', parent.childzz[0].gname
118
+ assert_equal 1, parent.childzz.length
119
+
120
+ parent.add('child')
121
+ assert_equal 'child', parent.childzz[0].gname
122
+ assert_equal 'child', parent.childzz[1].gname
123
+ assert_equal 2, parent.childzz.length
124
+
125
+ parent.add('childzz')
126
+ assert_equal 'childzz', parent.childzz.gname
127
+ assert_equal 'childzz', parent.childzzzz[0].gname
128
+ end
129
+
130
+ def test_gvalue
131
+ parent = Node.new("parent")
132
+ assert_equal nil, parent.gvalue
133
+
134
+ child = Node.new("child")
135
+ parent.add child
136
+ assert_equal "child", parent.gvalue
137
+
138
+ parent.add Node.new("sibling")
139
+ assert_equal "child", parent.gvalue
140
+ end
141
+
142
+ def test_gval
143
+ # gval is an alias for gvalue
144
+ parent = Node.new("parent")
145
+ assert_equal nil, parent.gval
146
+
147
+ child = Node.new("child")
148
+ parent.add child
149
+ assert_equal "child", parent.gval
150
+
151
+ parent.add Node.new("sibling")
152
+ assert_equal "child", parent.gval
153
+ end
154
+
155
+ def test_gfirst
156
+ parent = Node.new("parent")
157
+
158
+ child = Node.new("child")
159
+ parent.add child
160
+ assert_equal "child", parent.gfirst.gname
161
+ end
162
+
163
+
164
+
165
+ def test_comparable
166
+ assert_equal 0, Node.new("a") <=> Node.new("a")
167
+ assert_equal false, Node.new("a") > Node.new("b")
168
+ assert_equal true, Node.new("a") < Node.new("b")
169
+ end
170
+
171
+ def test_to_xml
172
+ parent = Node.new("parent")
173
+ child = Node.new("child")
174
+ sibling = Node.new("sibling")
175
+
176
+ assert_equal "<parent/>", parent.to_xml
177
+
178
+ parent.add child
179
+ assert_equal "<parent>child</parent>", parent.to_xml
180
+
181
+ parent.add sibling
182
+ assert_equal "<parent><child/><sibling/></parent>", parent.to_xml
183
+
184
+ child.add Node.new("grandchild")
185
+ assert_equal "<parent><child>grandchild</child><sibling/></parent>", parent.to_xml
186
+
187
+ parent.add('child')
188
+ assert_equal "<parent><child>grandchild</child><sibling/><child/></parent>", parent.to_xml
189
+
190
+ end
191
+
192
+ def test_eql?
193
+ a = Node.new('a')
194
+ assert_equal true, a.eql?(Node.new('a'))
195
+ a.add('b')
196
+ assert_equal false, a.eql?(Node.new('a'))
197
+
198
+ assert_equal true, a.eql?(Node.new('a').add('b'))
199
+ assert_equal false, a.eql?(Node.new('a').add('b').add('c'))
200
+ end
201
+
202
+ def test_equal_equal
203
+ a = Node.new('a')
204
+ assert_equal true, a == Node.new('a')
205
+ a.add('b')
206
+ assert_equal false, a == Node.new('a')
207
+
208
+ assert_equal true, a == Node.new('a').add('b')
209
+ assert_equal false, a == Node.new('a').add('b').add('c')
210
+ end
211
+
212
+ def test_flatten
213
+
214
+ assert_equal ['a'], Node.new('a').flatten.collect {|n| n.gname}
215
+ assert_equal ['a', 'b'], Node.new('a').add('b').flatten.collect {|n| n.gname}
216
+ assert_equal ['a', 'b', 'c'], Node.new('a').add('b').add('c').flatten.collect {|n| n.gname}
217
+ a = Node.new('a')
218
+ a.add('b')
219
+ a.add('c')
220
+ assert_equal ['a', 'b', 'c'], a.flatten.collect {|n| n.gname}
221
+ end
222
+
223
+ def test_subs
224
+ a = Node.new('a')
225
+ a.add(':hi')
226
+ a.subs(:hi, 'hello')
227
+ assert_equal 'hello', a.gvalue
228
+
229
+ b = Node.new('b')
230
+ b.add(':hi')
231
+
232
+ # subs are to be at class level
233
+ assert_equal 'hello', b.gvalue
234
+
235
+ end
236
+
237
+ def test_add_others
238
+ a = Node.new('a')
239
+
240
+ a.add(['b'])
241
+ a.add(3)
242
+ a.add({'d'=>'e'})
243
+ a.add('f')
244
+ a.add(:g)
245
+
246
+ assert_equal 'b', a.b.gname
247
+ assert_equal 'b', a[0].gname
248
+ assert_equal 'a', a[0].gparent.gname
249
+
250
+ assert_equal '3', a['3'].gname
251
+ assert_equal '3', a[1].gname
252
+ assert_equal 'a', a[1].gparent.gname
253
+
254
+ assert_equal 'd', a.d.gname
255
+ assert_equal 'd', a[2].gname
256
+ assert_equal 'a', a[2].gparent.gname
257
+
258
+ assert_equal 'f', a.f.gname
259
+ assert_equal 'f', a[3].gname
260
+ assert_equal 'a', a[3].gparent.gname
261
+
262
+ assert_equal ':g', a[':g'].gname
263
+ assert_equal ':g', a[4].gname
264
+ assert_equal 'a', a[4].gparent.gname
265
+
266
+ end
267
+
268
+
269
+
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+ end