undies 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/undies/element.rb +23 -20
- data/lib/undies/node.rb +4 -4
- data/lib/undies/node_list.rb +9 -2
- data/lib/undies/partial.rb +2 -2
- data/lib/undies/partial_data.rb +2 -2
- data/lib/undies/source.rb +1 -1
- data/lib/undies/template.rb +53 -12
- data/lib/undies/version.rb +1 -1
- data/lib/undies.rb +2 -1
- data/test/element_test.rb +15 -15
- data/test/fixtures/partial_template.rb +6 -0
- data/test/helper.rb +1 -1
- data/test/node_list_test.rb +25 -4
- data/test/node_test.rb +2 -2
- data/test/partial_data_test.rb +7 -7
- data/test/partial_test.rb +8 -8
- data/test/source_test.rb +1 -1
- data/test/template_test.rb +112 -56
- data/test/templates/content.html.rb +1 -0
- data/test/templates/layout.html.rb +6 -0
- data/test/templates/test.html.rb +1 -3
- metadata +10 -4
data/Gemfile.lock
CHANGED
data/lib/undies/element.rb
CHANGED
@@ -5,27 +5,30 @@ require 'undies/element_stack'
|
|
5
5
|
module Undies
|
6
6
|
class Element < Node
|
7
7
|
|
8
|
-
attr_reader :
|
9
|
-
attr_accessor :
|
8
|
+
attr_reader :___name, :___attrs
|
9
|
+
attr_accessor :___nodes
|
10
10
|
|
11
11
|
def initialize(stack, name, attrs={}, &block)
|
12
12
|
if !stack.kind_of?(ElementStack)
|
13
13
|
raise ArgumentError, "stack must be an Undies::ElementStack"
|
14
14
|
end
|
15
|
-
|
15
|
+
if !attrs.kind_of?(::Hash)
|
16
|
+
raise ArgumentError, "#{name.inspect} attrs must be provided as a Hash."
|
17
|
+
end
|
18
|
+
super(@___nodes = NodeList.new)
|
16
19
|
@stack = stack
|
17
|
-
@name = name.to_s
|
18
|
-
@attrs = attrs
|
19
20
|
@content_writes = 0
|
20
|
-
|
21
|
+
@___name = name.to_s
|
22
|
+
@___attrs = attrs
|
23
|
+
self.___content = block
|
21
24
|
end
|
22
25
|
|
23
26
|
def start_tag
|
24
|
-
"<#{@
|
27
|
+
"<#{@___name}#{html_attrs(@___attrs)}" + (@content_writes > 0 ? ">" : " />")
|
25
28
|
end
|
26
29
|
|
27
30
|
def end_tag
|
28
|
-
@content_writes > 0 ? "</#{@
|
31
|
+
@content_writes > 0 ? "</#{@___name}>" : nil
|
29
32
|
end
|
30
33
|
|
31
34
|
# CSS proxy methods ============================================
|
@@ -52,19 +55,19 @@ module Undies
|
|
52
55
|
# ==============================================================
|
53
56
|
|
54
57
|
def ==(other)
|
55
|
-
other.
|
56
|
-
other.
|
57
|
-
other.
|
58
|
+
other.___name == self.___name &&
|
59
|
+
other.___attrs == self.___attrs &&
|
60
|
+
other.___nodes == self.___nodes
|
58
61
|
end
|
59
62
|
|
60
63
|
def to_str(*args)
|
61
64
|
"Undies::Element:#{self.object_id} " +
|
62
|
-
"@name=#{
|
65
|
+
"@name=#{self.___name.inspect}, @attrs=#{self.___attrs.inspect}, @nodes=#{self.___nodes.inspect}"
|
63
66
|
end
|
64
67
|
alias_method :inspect, :to_str
|
65
68
|
|
66
69
|
def to_ary(*args)
|
67
|
-
|
70
|
+
self.___nodes
|
68
71
|
end
|
69
72
|
|
70
73
|
protected
|
@@ -85,20 +88,20 @@ module Undies
|
|
85
88
|
private
|
86
89
|
|
87
90
|
def proxy_id_attr(value, attrs={}, &block)
|
88
|
-
|
89
|
-
|
90
|
-
self.
|
91
|
+
self.___attrs.merge!(:id => value)
|
92
|
+
self.___attrs.merge!(attrs)
|
93
|
+
self.___content = block
|
91
94
|
self
|
92
95
|
end
|
93
96
|
|
94
97
|
def proxy_class_attr(value, attrs={}, &block)
|
95
|
-
|
96
|
-
|
97
|
-
self.
|
98
|
+
self.___attrs[:class] = [self.___attrs[:class], value].compact.join(' ')
|
99
|
+
self.___attrs.merge!(attrs)
|
100
|
+
self.___content = block
|
98
101
|
self
|
99
102
|
end
|
100
103
|
|
101
|
-
def
|
104
|
+
def ___content=(block)
|
102
105
|
if block
|
103
106
|
@content_writes += 1
|
104
107
|
@stack.push(self)
|
data/lib/undies/node.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Undies
|
2
2
|
class Node
|
3
3
|
|
4
|
-
attr_reader :
|
4
|
+
attr_reader :___content
|
5
5
|
|
6
6
|
def initialize(content)
|
7
|
-
@
|
7
|
+
@___content = content
|
8
8
|
end
|
9
9
|
|
10
10
|
def start_tag
|
@@ -17,7 +17,7 @@ module Undies
|
|
17
17
|
|
18
18
|
def to_s(pp_level=0, pp_indent=nil)
|
19
19
|
[ self.start_tag,
|
20
|
-
self.
|
20
|
+
self.___content,
|
21
21
|
self.end_tag
|
22
22
|
].compact.collect do |item|
|
23
23
|
pretty_print(item, pp_level, pp_indent)
|
@@ -35,4 +35,4 @@ module Undies
|
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
38
|
-
end
|
38
|
+
end
|
data/lib/undies/node_list.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
require "undies/node"
|
2
2
|
|
3
3
|
module Undies
|
4
|
+
|
5
|
+
# a node list is an ordered collection of node data
|
6
|
+
# * the Template class builds one as the template source is evaluated
|
7
|
+
# * a list may contains nodes and/or other node lists
|
8
|
+
# * serialize a list using 'to_s' using optional pretty printing args
|
9
|
+
# * any pretty printing args are used to render the individual nodes/lists
|
4
10
|
class NodeList < ::Array
|
5
11
|
|
6
12
|
def initialize(*args)
|
@@ -14,8 +20,8 @@ module Undies
|
|
14
20
|
end
|
15
21
|
|
16
22
|
def <<(item)
|
17
|
-
unless item.kind_of?(Node)
|
18
|
-
raise ArgumentError, 'you can only append nodes to a NodeList'
|
23
|
+
unless item.kind_of?(Node) || item.kind_of?(NodeList)
|
24
|
+
raise ArgumentError, 'you can only append nodes or other node lists to a NodeList'
|
19
25
|
end
|
20
26
|
super
|
21
27
|
end
|
@@ -25,4 +31,5 @@ module Undies
|
|
25
31
|
end
|
26
32
|
|
27
33
|
end
|
34
|
+
|
28
35
|
end
|
data/lib/undies/partial.rb
CHANGED
@@ -2,10 +2,10 @@ require 'undies/partial_data'
|
|
2
2
|
require 'undies/template'
|
3
3
|
|
4
4
|
module Undies
|
5
|
-
|
5
|
+
module PartialTemplate
|
6
6
|
|
7
7
|
def initialize(path, *args)
|
8
|
-
locals =
|
8
|
+
locals = PartialLocals.new(path)
|
9
9
|
locals.values, io, locals.object = self.___partial_args(*args)
|
10
10
|
super(path, io, locals)
|
11
11
|
end
|
data/lib/undies/partial_data.rb
CHANGED
data/lib/undies/source.rb
CHANGED
data/lib/undies/template.rb
CHANGED
@@ -5,22 +5,20 @@ require 'undies/element'
|
|
5
5
|
module Undies
|
6
6
|
class Template
|
7
7
|
|
8
|
-
|
8
|
+
# prefixing with a triple underscore to not pollut metaclass locals scope
|
9
|
+
|
10
|
+
attr_accessor :___nodes
|
9
11
|
|
10
12
|
def initialize(*args, &block)
|
11
|
-
self.
|
13
|
+
self.___nodes = NodeList.new
|
14
|
+
targs = self.___template_args(args.compact, block)
|
15
|
+
self.___locals, self.___io, self.___layout, self.___markup = targs
|
12
16
|
self.___stack = ElementStack.new(self, self.___io)
|
13
|
-
self.
|
14
|
-
|
15
|
-
if (source).file?
|
16
|
-
instance_eval(source.data, source.source, 1)
|
17
|
-
else
|
18
|
-
instance_eval(&source.data)
|
19
|
-
end
|
17
|
+
self.___compile { self.___render(self.___markup) if self.___layout }
|
20
18
|
end
|
21
19
|
|
22
20
|
def to_s(pp_indent=nil)
|
23
|
-
self.
|
21
|
+
self.___nodes.to_s(0, pp_indent)
|
24
22
|
end
|
25
23
|
|
26
24
|
# Add a text node (data escaped) to the nodes of the current node
|
@@ -84,6 +82,18 @@ module Undies
|
|
84
82
|
# prefixing non-public methods with a triple underscore to not pollute
|
85
83
|
# metaclass locals scope
|
86
84
|
|
85
|
+
def ___compile
|
86
|
+
self.___render(self.___layout || self.___markup)
|
87
|
+
end
|
88
|
+
|
89
|
+
def ___render(source)
|
90
|
+
if source.file?
|
91
|
+
instance_eval(source.data, source.source, 1)
|
92
|
+
else
|
93
|
+
instance_eval(&source.data)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
87
97
|
def ___locals=(data)
|
88
98
|
if !data.kind_of?(::Hash)
|
89
99
|
raise ArgumentError
|
@@ -99,7 +109,7 @@ module Undies
|
|
99
109
|
end
|
100
110
|
|
101
111
|
def ___add(node)
|
102
|
-
self.___stack.last.
|
112
|
+
self.___stack.last.___nodes.append(node)
|
103
113
|
end
|
104
114
|
|
105
115
|
def ___stack
|
@@ -120,10 +130,31 @@ module Undies
|
|
120
130
|
@io = value
|
121
131
|
end
|
122
132
|
|
133
|
+
def ___layout
|
134
|
+
@layout
|
135
|
+
end
|
136
|
+
|
137
|
+
def ___layout=(value)
|
138
|
+
if value && !(value.kind_of?(Source) && value.file?)
|
139
|
+
raise ArgumentError, "layout must be a file source"
|
140
|
+
end
|
141
|
+
@layout = value
|
142
|
+
end
|
143
|
+
|
144
|
+
def ___markup
|
145
|
+
@markup
|
146
|
+
end
|
147
|
+
|
148
|
+
def ___markup=(value)
|
149
|
+
raise ArgumentError if value && !value.kind_of?(Source)
|
150
|
+
@markup = value
|
151
|
+
end
|
152
|
+
|
123
153
|
def ___template_args(args, block)
|
124
154
|
[ args.last.kind_of?(::Hash) ? args.pop : {},
|
125
155
|
self.___is_a_stream?(args.last) ? args.pop : nil,
|
126
|
-
|
156
|
+
self.___layout_arg?(args, block) ? Source.new(args.pop) : nil,
|
157
|
+
Source.new(args.first || block)
|
127
158
|
]
|
128
159
|
end
|
129
160
|
|
@@ -136,6 +167,16 @@ module Undies
|
|
136
167
|
!thing.kind_of?(::String) && thing.respond_to?(:<<)
|
137
168
|
end
|
138
169
|
|
170
|
+
def ___layout_arg?(args, block)
|
171
|
+
if args.size >= 2
|
172
|
+
true
|
173
|
+
elsif args.size <= 0
|
174
|
+
false
|
175
|
+
else # args.size == 1
|
176
|
+
!block.nil?
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
139
180
|
private
|
140
181
|
|
141
182
|
# you can't define locals that conflict with the template's public methods
|
data/lib/undies/version.rb
CHANGED
data/lib/undies.rb
CHANGED
data/test/element_test.rb
CHANGED
@@ -14,23 +14,23 @@ class Undies::Element
|
|
14
14
|
context 'an element'
|
15
15
|
before { @e = Undies::Element.new(Undies::ElementStack.new, :div) }
|
16
16
|
subject { @e }
|
17
|
-
should have_readers :
|
18
|
-
should have_accessor :
|
17
|
+
should have_readers :___name, :___attrs
|
18
|
+
should have_accessor :___nodes
|
19
19
|
|
20
20
|
should "be a Node" do
|
21
21
|
assert_kind_of Undies::Node, subject
|
22
22
|
end
|
23
23
|
|
24
24
|
should "store it's name as a string" do
|
25
|
-
assert_equal "div", subject.
|
25
|
+
assert_equal "div", subject.___name
|
26
26
|
end
|
27
27
|
|
28
28
|
should "have a NodeList as its nodes" do
|
29
|
-
assert_kind_of Undies::NodeList, subject.
|
29
|
+
assert_kind_of Undies::NodeList, subject.___nodes
|
30
30
|
end
|
31
31
|
|
32
32
|
should "have its nodes be its content" do
|
33
|
-
assert_equal subject.
|
33
|
+
assert_equal subject.___nodes.object_id, subject.___content.object_id
|
34
34
|
end
|
35
35
|
|
36
36
|
should "have an element stack as its stack" do
|
@@ -54,7 +54,7 @@ class Undies::Element
|
|
54
54
|
subject { @e }
|
55
55
|
|
56
56
|
should "have no nodes" do
|
57
|
-
assert_equal([], subject.
|
57
|
+
assert_equal([], subject.___nodes)
|
58
58
|
end
|
59
59
|
|
60
60
|
end
|
@@ -119,7 +119,7 @@ class Undies::Element
|
|
119
119
|
should "proxy id attr with methods ending in '!'" do
|
120
120
|
assert_equal({
|
121
121
|
:id => 'thing1'
|
122
|
-
}, subject.thing1!.
|
122
|
+
}, subject.thing1!.___attrs)
|
123
123
|
end
|
124
124
|
|
125
125
|
should "nest elements from proxy id call" do
|
@@ -132,19 +132,19 @@ class Undies::Element
|
|
132
132
|
should "proxy id attr with last method call ending in '!'" do
|
133
133
|
assert_equal({
|
134
134
|
:id => 'thing2'
|
135
|
-
}, subject.thing1!.thing2!.
|
135
|
+
}, subject.thing1!.thing2!.___attrs)
|
136
136
|
end
|
137
137
|
|
138
138
|
should "set id attr to explicit if called last " do
|
139
139
|
assert_equal({
|
140
140
|
:id => 'thing3'
|
141
|
-
}, subject.thing1!.thing2!(:id => 'thing3').
|
141
|
+
}, subject.thing1!.thing2!(:id => 'thing3').___attrs)
|
142
142
|
end
|
143
143
|
|
144
144
|
should "set id attr to proxy if called last" do
|
145
145
|
assert_equal({
|
146
146
|
:id => 'thing1'
|
147
|
-
}, subject.thing2!(:id => 'thing3').thing1!.
|
147
|
+
}, subject.thing2!(:id => 'thing3').thing1!.___attrs)
|
148
148
|
end
|
149
149
|
|
150
150
|
should "respond to any other method as a class proxy" do
|
@@ -154,7 +154,7 @@ class Undies::Element
|
|
154
154
|
should "proxy single html class attr" do
|
155
155
|
assert_equal({
|
156
156
|
:class => 'thing'
|
157
|
-
}, subject.thing.
|
157
|
+
}, subject.thing.___attrs)
|
158
158
|
end
|
159
159
|
|
160
160
|
should "nest elements from proxy class call" do
|
@@ -167,19 +167,19 @@ class Undies::Element
|
|
167
167
|
should "proxy multi html class attrs" do
|
168
168
|
assert_equal({
|
169
169
|
:class => 'list thing awesome'
|
170
|
-
}, subject.list.thing.awesome.
|
170
|
+
}, subject.list.thing.awesome.___attrs)
|
171
171
|
end
|
172
172
|
|
173
173
|
should "set class attr with explicit if called last " do
|
174
174
|
assert_equal({
|
175
175
|
:class => 'list'
|
176
|
-
}, subject.thing.awesome(:class => "list").
|
176
|
+
}, subject.thing.awesome(:class => "list").___attrs)
|
177
177
|
end
|
178
178
|
|
179
179
|
should "update class attr with proxy if called last" do
|
180
180
|
assert_equal({
|
181
181
|
:class => 'list is good'
|
182
|
-
}, subject.thing.awesome(:class => "list is").good.
|
182
|
+
}, subject.thing.awesome(:class => "list is").good.___attrs)
|
183
183
|
end
|
184
184
|
|
185
185
|
should "proxy mixed class and id selector attrs" do
|
@@ -189,7 +189,7 @@ class Undies::Element
|
|
189
189
|
}, subject.thing1!.awesome({
|
190
190
|
:class => "list is",
|
191
191
|
:id => "thing2"
|
192
|
-
}).good.thing3!.
|
192
|
+
}).good.thing3!.___attrs)
|
193
193
|
end
|
194
194
|
|
195
195
|
end
|
data/test/helper.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
# this file is automatically required in when you require 'test_belt'
|
2
|
-
# put test helpers here
|
2
|
+
# put test helpers here
|
data/test/node_list_test.rb
CHANGED
@@ -4,7 +4,7 @@ require "undies/node"
|
|
4
4
|
|
5
5
|
class Undies::NodeList
|
6
6
|
|
7
|
-
class
|
7
|
+
class BasicTests < Test::Unit::TestCase
|
8
8
|
include TestBelt
|
9
9
|
|
10
10
|
context 'a node list'
|
@@ -29,18 +29,39 @@ class Undies::NodeList
|
|
29
29
|
end
|
30
30
|
assert_nothing_raised do
|
31
31
|
subject.append(Undies::Node.new('hey!'))
|
32
|
+
subject.append(Undies::NodeList.new)
|
32
33
|
subject << Undies::Node.new('hey!')
|
33
34
|
end
|
34
35
|
end
|
35
36
|
|
37
|
+
end
|
38
|
+
|
39
|
+
class NodeHandlingTests < BasicTests
|
40
|
+
setup do
|
41
|
+
@hey = Undies::Node.new "hey!"
|
42
|
+
@you = Undies::Node.new " you..."
|
43
|
+
@there = Undies::Node.new " there."
|
44
|
+
@node_list = Undies::NodeList.new
|
45
|
+
@node_list.append(@you)
|
46
|
+
@node_list.append(@there)
|
47
|
+
end
|
48
|
+
|
36
49
|
should "append nodes with the 'append' method" do
|
37
|
-
subject.append(
|
50
|
+
subject.append(@hey)
|
38
51
|
assert_equal 1, subject.size
|
39
52
|
end
|
40
53
|
|
41
54
|
should "return the node when appending" do
|
42
|
-
|
43
|
-
|
55
|
+
assert_equal @hey.object_id, subject.append(@hey).object_id
|
56
|
+
end
|
57
|
+
|
58
|
+
should "serialize to a string" do
|
59
|
+
assert_equal " you... there.", @node_list.to_s
|
60
|
+
|
61
|
+
to_serialize = Undies::NodeList.new
|
62
|
+
to_serialize.append(@hey)
|
63
|
+
to_serialize.append(@node_list)
|
64
|
+
assert_equal "hey! you... there.", to_serialize.to_s
|
44
65
|
end
|
45
66
|
|
46
67
|
end
|
data/test/node_test.rb
CHANGED
@@ -9,10 +9,10 @@ class Undies::Node
|
|
9
9
|
context 'a node'
|
10
10
|
subject { Undies::Node.new("a text node here") }
|
11
11
|
should have_instance_method :to_s, :start_tag, :end_tag
|
12
|
-
should have_reader :
|
12
|
+
should have_reader :___content
|
13
13
|
|
14
14
|
should "know it's content" do
|
15
|
-
assert_equal "a text node here", subject.
|
15
|
+
assert_equal "a text node here", subject.___content.to_s
|
16
16
|
end
|
17
17
|
|
18
18
|
should "know how to serialize itself" do
|
data/test/partial_data_test.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require "test_belt"
|
2
2
|
require "undies/partial_data"
|
3
3
|
|
4
|
-
class Undies::
|
4
|
+
class Undies::PartialLocals
|
5
5
|
|
6
6
|
class BasicTest < Test::Unit::TestCase
|
7
7
|
include TestBelt
|
8
8
|
|
9
9
|
context 'partial data'
|
10
|
-
subject { Undies::
|
10
|
+
subject { Undies::PartialLocals.new 'test/templates/index.html.rb' }
|
11
11
|
should have_readers :path, :name
|
12
12
|
|
13
13
|
should "be a kind of Hash" do
|
@@ -16,7 +16,7 @@ class Undies::PartialData
|
|
16
16
|
|
17
17
|
should "complain if now path given" do
|
18
18
|
assert_raises ArgumentError do
|
19
|
-
Undies::
|
19
|
+
Undies::PartialLocals.new
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -25,17 +25,17 @@ class Undies::PartialData
|
|
25
25
|
class NameTest < BasicTest
|
26
26
|
|
27
27
|
should "know its name given a file" do
|
28
|
-
data = Undies::
|
28
|
+
data = Undies::PartialLocals.new('test/templates/current.html.rb')
|
29
29
|
assert_equal 'current', data.name
|
30
30
|
end
|
31
31
|
|
32
32
|
should "know its name given a file name with a leading char" do
|
33
|
-
data = Undies::
|
33
|
+
data = Undies::PartialLocals.new('test/templates/_partial.html.rb')
|
34
34
|
assert_equal 'partial', data.name
|
35
35
|
end
|
36
36
|
|
37
37
|
should "know its name given a file name with multiple leading chars" do
|
38
|
-
data = Undies::
|
38
|
+
data = Undies::PartialLocals.new('test/templates/__partial.html.rb')
|
39
39
|
assert_equal 'partial', data.name
|
40
40
|
end
|
41
41
|
|
@@ -45,7 +45,7 @@ class Undies::PartialData
|
|
45
45
|
before do
|
46
46
|
@path = 'test/templates/index.html.rb'
|
47
47
|
end
|
48
|
-
subject { Undies::
|
48
|
+
subject { Undies::PartialLocals.new(@path) }
|
49
49
|
|
50
50
|
should "not have any values by default" do
|
51
51
|
assert_equal({}, subject)
|
data/test/partial_test.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require "test_belt"
|
2
2
|
|
3
3
|
require "stringio"
|
4
|
-
require "
|
4
|
+
require "test/fixtures/partial_template"
|
5
5
|
|
6
|
-
class Undies::
|
6
|
+
class Undies::PartialTests
|
7
7
|
|
8
8
|
class BasicTest < Test::Unit::TestCase
|
9
9
|
include TestBelt
|
@@ -11,7 +11,7 @@ class Undies::Partial
|
|
11
11
|
context 'partial'
|
12
12
|
before do
|
13
13
|
@path = 'test/templates/test.html.rb'
|
14
|
-
@p =
|
14
|
+
@p = TestPartial.new @path
|
15
15
|
end
|
16
16
|
subject { @p }
|
17
17
|
|
@@ -21,7 +21,7 @@ class Undies::Partial
|
|
21
21
|
|
22
22
|
should "complain if no path given" do
|
23
23
|
assert_raises ArgumentError do
|
24
|
-
|
24
|
+
TestPartial.new
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -33,12 +33,12 @@ class Undies::Partial
|
|
33
33
|
end
|
34
34
|
|
35
35
|
should "know its data" do
|
36
|
-
partial =
|
36
|
+
partial = TestPartial.new(@path, :name => 'A Name')
|
37
37
|
assert_equal("A Name", partial.name)
|
38
38
|
end
|
39
39
|
|
40
40
|
should "know its object" do
|
41
|
-
partial =
|
41
|
+
partial = TestPartial.new(@path, "thing")
|
42
42
|
assert_equal("thing", partial.index)
|
43
43
|
end
|
44
44
|
|
@@ -54,8 +54,8 @@ class Undies::Partial
|
|
54
54
|
|
55
55
|
|
56
56
|
should "should write to the stream as its being constructed" do
|
57
|
-
|
58
|
-
assert_equal "<html><head></head><body><div
|
57
|
+
TestPartial.new @path, @outstream
|
58
|
+
assert_equal "<html><head></head><body><div>Hi</div></body></html>", @output
|
59
59
|
end
|
60
60
|
|
61
61
|
end
|
data/test/source_test.rb
CHANGED
data/test/template_test.rb
CHANGED
@@ -12,16 +12,44 @@ class Undies::Template
|
|
12
12
|
subject { Undies::Template.new {} }
|
13
13
|
should have_instance_method :to_s, :escape_html
|
14
14
|
should have_instance_methods :_, :__, :element, :tag
|
15
|
-
should have_accessor :
|
15
|
+
should have_accessor :___nodes
|
16
16
|
|
17
17
|
should "have a NodeList as its nodes" do
|
18
|
-
assert_kind_of Undies::NodeList, subject.
|
18
|
+
assert_kind_of Undies::NodeList, subject.___nodes
|
19
19
|
end
|
20
20
|
|
21
21
|
should "have no io stream by default" do
|
22
22
|
assert_nil subject.send(:___io)
|
23
23
|
end
|
24
24
|
|
25
|
+
should "maintain the template's scope throughout content blocks" do
|
26
|
+
templ = Undies::Template.new do
|
27
|
+
_div {
|
28
|
+
_div {
|
29
|
+
__ self.object_id
|
30
|
+
}
|
31
|
+
}
|
32
|
+
end
|
33
|
+
assert_equal "<div><div>#{templ.object_id}</div></div>", templ.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
should "generate pretty printed markup" do
|
37
|
+
file = 'test/templates/test.html.rb'
|
38
|
+
assert_equal(
|
39
|
+
%{<html>
|
40
|
+
<head>
|
41
|
+
</head>
|
42
|
+
<body>
|
43
|
+
<div>
|
44
|
+
Hi
|
45
|
+
</div>
|
46
|
+
</body>
|
47
|
+
</html>
|
48
|
+
},
|
49
|
+
Undies::Template.new(File.expand_path(file)).to_s(2)
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
25
53
|
end
|
26
54
|
|
27
55
|
|
@@ -39,9 +67,9 @@ class Undies::Template
|
|
39
67
|
|
40
68
|
should "also add the node using the '__' and '_' methods" do
|
41
69
|
subject.__(@data)
|
42
|
-
assert_equal 1, subject.
|
70
|
+
assert_equal 1, subject.___nodes.size
|
43
71
|
subject._(@data)
|
44
|
-
assert_equal 2, subject.
|
72
|
+
assert_equal 2, subject.___nodes.size
|
45
73
|
end
|
46
74
|
|
47
75
|
should "add the text un-escaped using the '__' method" do
|
@@ -74,8 +102,8 @@ class Undies::Template
|
|
74
102
|
|
75
103
|
should "add a new Element object" do
|
76
104
|
subject.element(:br)
|
77
|
-
assert_equal 1, subject.
|
78
|
-
assert_equal Undies::Element.new(Undies::ElementStack.new, :br), subject.
|
105
|
+
assert_equal 1, subject.___nodes.size
|
106
|
+
assert_equal Undies::Element.new(Undies::ElementStack.new, :br), subject.___nodes.first
|
79
107
|
end
|
80
108
|
|
81
109
|
should "respond to underscore-prefix methods" do
|
@@ -100,10 +128,10 @@ class Undies::Template
|
|
100
128
|
class LocalsTest < BasicTest
|
101
129
|
|
102
130
|
should "only accept the data if it is a Hash" do
|
103
|
-
assert_raises
|
131
|
+
assert_raises ArgumentError do
|
104
132
|
(Undies::Template.new("some_data") {}).some
|
105
133
|
end
|
106
|
-
assert_raises
|
134
|
+
assert_raises ArgumentError do
|
107
135
|
(Undies::Template.new('test/templates/test.html.rb', "some_data")).some
|
108
136
|
end
|
109
137
|
assert_respond_to(
|
@@ -127,72 +155,100 @@ class Undies::Template
|
|
127
155
|
assert_equal "data", templ.some
|
128
156
|
end
|
129
157
|
|
158
|
+
should "be able to access its locals in the template definition" do
|
159
|
+
templ = Undies::Template.new(:name => "awesome") do
|
160
|
+
_div {
|
161
|
+
_div { _ name }
|
162
|
+
}
|
163
|
+
end
|
164
|
+
assert_equal "<div><div>awesome</div></div>", templ.to_s
|
165
|
+
end
|
166
|
+
|
130
167
|
end
|
131
168
|
|
132
169
|
|
133
170
|
|
134
171
|
class DefinitionTest < BasicTest
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
172
|
+
setup do
|
173
|
+
@expected_output = "<html><head></head><body><div>Hi</div></body></html>"
|
174
|
+
@proc = Proc.new do
|
175
|
+
_html {
|
176
|
+
_head {}
|
177
|
+
_body {
|
178
|
+
_div { _ "Hi" }
|
141
179
|
}
|
142
180
|
}
|
143
181
|
end
|
144
|
-
|
182
|
+
@content_proc = Proc.new do
|
183
|
+
_div { _ "Hi" }
|
184
|
+
end
|
185
|
+
@layout_proc = Proc.new do
|
186
|
+
_html { _head {}; _body { yield if block_given? } }
|
187
|
+
end
|
188
|
+
@layout_file = File.expand_path "test/templates/layout.html.rb"
|
189
|
+
@content_file = File.expand_path "test/templates/content.html.rb"
|
190
|
+
@test_content_file = File.expand_path "test/templates/test.html.rb"
|
145
191
|
end
|
146
192
|
|
147
|
-
should "
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
193
|
+
should "generate markup given the content in a passed block" do
|
194
|
+
template = Undies::Template.new(&@proc)
|
195
|
+
assert_equal @expected_output, template.to_s
|
196
|
+
end
|
197
|
+
|
198
|
+
should "complain if given a proc both as the first arg and passed as a block" do
|
199
|
+
assert_raises ArgumentError do
|
200
|
+
Undies::Template.new(@proc) do
|
201
|
+
_div { _ "Should not render b/c argument error" }
|
202
|
+
end
|
152
203
|
end
|
153
|
-
assert_equal "<div><div>awesome</div></div>", templ.to_s
|
154
204
|
end
|
155
205
|
|
156
|
-
should "generate markup given a block" do
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
_div.header!.loud.element {
|
164
|
-
__ "YEA!!"
|
165
|
-
}
|
166
|
-
}
|
167
|
-
}
|
168
|
-
end.to_s
|
169
|
-
)
|
206
|
+
should "generate markup given the content in a file, even if passed a block" do
|
207
|
+
template_no_block = Undies::Template.new(@test_content_file)
|
208
|
+
template_w_block = Undies::Template.new(@test_content_file) do
|
209
|
+
_div { _ "Should not render b/c template prefers a file" }
|
210
|
+
end
|
211
|
+
assert_equal @expected_output, template_no_block.to_s
|
212
|
+
assert_equal @expected_output, template_w_block.to_s
|
170
213
|
end
|
171
214
|
|
172
|
-
should "generate markup given a file" do
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
)
|
215
|
+
should "generate markup given the layout in a file and the content in a passed block" do
|
216
|
+
template = Undies::Template.new(@layout_file) do
|
217
|
+
_div { _ "Hi" }
|
218
|
+
end
|
219
|
+
assert_equal @expected_output, template.to_s
|
178
220
|
end
|
179
221
|
|
180
|
-
should "generate
|
181
|
-
|
182
|
-
assert_equal
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
FILE!!
|
189
|
-
</div>
|
190
|
-
</body>
|
191
|
-
</html>
|
192
|
-
},
|
193
|
-
Undies::Template.new(File.expand_path(file)).to_s(2)
|
194
|
-
)
|
222
|
+
should "generate markup given the layout in a Proc and the content in a Proc as first arg" do
|
223
|
+
template = Undies::Template.new(@content_proc, @layout_file)
|
224
|
+
assert_equal @expected_output, template.to_s
|
225
|
+
end
|
226
|
+
|
227
|
+
should "generate markup given the layout in a file and the content in a file" do
|
228
|
+
template = Undies::Template.new(@content_file, @layout_file)
|
229
|
+
assert_equal @expected_output, template.to_s
|
195
230
|
end
|
231
|
+
|
232
|
+
should "complain if given the layout in a Proc and the content in a passed block" do
|
233
|
+
assert_raises ArgumentError do
|
234
|
+
Undies::Template.new(@layout_proc) do
|
235
|
+
_div { _ "Hi" }
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
should "complain given the layout in a Proc and the content in a Proc as first arg" do
|
241
|
+
assert_raises ArgumentError do
|
242
|
+
Undies::Template.new(@content_proc, @layout_proc)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
should "complain given the layout in a Proc and the content in a file" do
|
247
|
+
assert_raises ArgumentError do
|
248
|
+
Undies::Template.new(@content_file, @layout_proc)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
196
252
|
end
|
197
253
|
|
198
254
|
|
@@ -0,0 +1 @@
|
|
1
|
+
_div { _ "Hi" }
|
data/test/templates/test.html.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: undies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-30 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
type: :development
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/undies/version.rb
|
75
75
|
- test/element_stack_test.rb
|
76
76
|
- test/element_test.rb
|
77
|
+
- test/fixtures/partial_template.rb
|
77
78
|
- test/helper.rb
|
78
79
|
- test/irb.rb
|
79
80
|
- test/node_list_test.rb
|
@@ -82,7 +83,9 @@ files:
|
|
82
83
|
- test/partial_test.rb
|
83
84
|
- test/source_test.rb
|
84
85
|
- test/template_test.rb
|
86
|
+
- test/templates/content.html.rb
|
85
87
|
- test/templates/index.html.rb
|
88
|
+
- test/templates/layout.html.rb
|
86
89
|
- test/templates/test.html.rb
|
87
90
|
- undies.gemspec
|
88
91
|
homepage: http://github.com/kelredd/undies
|
@@ -121,6 +124,7 @@ summary: A pure-Ruby HTML and plain text templating DSL.
|
|
121
124
|
test_files:
|
122
125
|
- test/element_stack_test.rb
|
123
126
|
- test/element_test.rb
|
127
|
+
- test/fixtures/partial_template.rb
|
124
128
|
- test/helper.rb
|
125
129
|
- test/irb.rb
|
126
130
|
- test/node_list_test.rb
|
@@ -129,5 +133,7 @@ test_files:
|
|
129
133
|
- test/partial_test.rb
|
130
134
|
- test/source_test.rb
|
131
135
|
- test/template_test.rb
|
136
|
+
- test/templates/content.html.rb
|
132
137
|
- test/templates/index.html.rb
|
138
|
+
- test/templates/layout.html.rb
|
133
139
|
- test/templates/test.html.rb
|