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 CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- undies (1.1.0)
4
+ undies (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -5,27 +5,30 @@ require 'undies/element_stack'
5
5
  module Undies
6
6
  class Element < Node
7
7
 
8
- attr_reader :name, :attrs
9
- attr_accessor :nodes
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
- super(@nodes = NodeList.new)
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
- self.content = block
21
+ @___name = name.to_s
22
+ @___attrs = attrs
23
+ self.___content = block
21
24
  end
22
25
 
23
26
  def start_tag
24
- "<#{@name}#{html_attrs(@attrs)}" + (@content_writes > 0 ? ">" : " />")
27
+ "<#{@___name}#{html_attrs(@___attrs)}" + (@content_writes > 0 ? ">" : " />")
25
28
  end
26
29
 
27
30
  def end_tag
28
- @content_writes > 0 ? "</#{@name}>" : nil
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.name == self.name &&
56
- other.attrs == self.attrs &&
57
- other.nodes == self.nodes
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=#{@name.inspect}, @attrs=#{@attrs.inspect}, @nodes=#{@nodes.inspect}"
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
- @nodes
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
- @attrs.merge!(:id => value)
89
- @attrs.merge!(attrs)
90
- self.content = block
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
- @attrs[:class] = [@attrs[:class], value].compact.join(' ')
96
- @attrs.merge!(attrs)
97
- self.content = block
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 content=(block)
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 :content
4
+ attr_reader :___content
5
5
 
6
6
  def initialize(content)
7
- @content = content
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.content,
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
@@ -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
@@ -2,10 +2,10 @@ require 'undies/partial_data'
2
2
  require 'undies/template'
3
3
 
4
4
  module Undies
5
- class Partial < Template
5
+ module PartialTemplate
6
6
 
7
7
  def initialize(path, *args)
8
- locals = PartialData.new(path)
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
@@ -1,5 +1,5 @@
1
1
  module Undies
2
- class PartialData < ::Hash
2
+ class PartialLocals < ::Hash
3
3
 
4
4
  attr_reader :path, :name
5
5
 
@@ -29,4 +29,4 @@ module Undies
29
29
  end
30
30
 
31
31
  end
32
- end
32
+ end
data/lib/undies/source.rb CHANGED
@@ -24,4 +24,4 @@ module Undies
24
24
  end
25
25
 
26
26
  end
27
- end
27
+ end
@@ -5,22 +5,20 @@ require 'undies/element'
5
5
  module Undies
6
6
  class Template
7
7
 
8
- attr_accessor :nodes
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.___locals, self.___io, source = self.___template_args(args, block)
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.nodes = NodeList.new
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.nodes.collect{|n| n.to_s(0, pp_indent)}.join
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.nodes.append(node)
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
- Source.new(block || args.first.to_s)
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
@@ -1,3 +1,3 @@
1
1
  module Undies
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/undies.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'undies/template'
2
+ require 'undies/partial'
2
3
 
3
4
  module Undies
4
5
 
@@ -9,4 +10,4 @@ module Undies
9
10
  # end
10
11
  # end
11
12
 
12
- end
13
+ end
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 :name, :attrs
18
- should have_accessor :nodes
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.name
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.nodes
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.nodes.object_id, subject.content.object_id
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.nodes)
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!.attrs)
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!.attrs)
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').attrs)
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!.attrs)
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.attrs)
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.attrs)
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").attrs)
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.attrs)
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!.attrs)
192
+ }).good.thing3!.___attrs)
193
193
  end
194
194
 
195
195
  end
@@ -0,0 +1,6 @@
1
+ require 'undies/template'
2
+ require 'undies/partial'
3
+
4
+ class TestPartial < Undies::Template
5
+ include Undies::PartialTemplate
6
+ 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
@@ -4,7 +4,7 @@ require "undies/node"
4
4
 
5
5
  class Undies::NodeList
6
6
 
7
- class BasicTest < Test::Unit::TestCase
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(Undies::Node.new "hey!")
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
- node = Undies::Node.new "hey!"
43
- assert_equal node.object_id, subject.append(node).object_id
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 :content
12
+ should have_reader :___content
13
13
 
14
14
  should "know it's content" do
15
- assert_equal "a text node here", subject.content.to_s
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
@@ -1,13 +1,13 @@
1
1
  require "test_belt"
2
2
  require "undies/partial_data"
3
3
 
4
- class Undies::PartialData
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::PartialData.new 'test/templates/index.html.rb' }
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::PartialData.new
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::PartialData.new('test/templates/current.html.rb')
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::PartialData.new('test/templates/_partial.html.rb')
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::PartialData.new('test/templates/__partial.html.rb')
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::PartialData.new(@path) }
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 "undies/partial"
4
+ require "test/fixtures/partial_template"
5
5
 
6
- class Undies::Partial
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 = Undies::Partial.new @path
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
- Undies::Partial.new
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 = Undies::Partial.new(@path, :name => 'A Name')
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 = Undies::Partial.new(@path, "thing")
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
- Undies::Partial.new @path, @outstream
58
- assert_equal "<html><head></head><body><div class=\"file\">FILE!!</div></body></html>", @output
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
@@ -19,7 +19,7 @@ class Undies::Source
19
19
 
20
20
  should "complain if no block given and file does not exist" do
21
21
  assert_raises ArgumentError do
22
- Undies::Template.new "noexist.html.rb"
22
+ Undies::Source.new "noexist.html.rb"
23
23
  end
24
24
  end
25
25
 
@@ -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 :nodes
15
+ should have_accessor :___nodes
16
16
 
17
17
  should "have a NodeList as its nodes" do
18
- assert_kind_of Undies::NodeList, subject.nodes
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.nodes.size
70
+ assert_equal 1, subject.___nodes.size
43
71
  subject._(@data)
44
- assert_equal 2, subject.nodes.size
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.nodes.size
78
- assert_equal Undies::Element.new(Undies::ElementStack.new, :br), subject.nodes.first
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 NoMethodError do
131
+ assert_raises ArgumentError do
104
132
  (Undies::Template.new("some_data") {}).some
105
133
  end
106
- assert_raises NoMethodError do
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
- should "maintain the template's scope throughout content blocks" do
137
- templ = Undies::Template.new do
138
- _div {
139
- _div {
140
- __ self.object_id
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
- assert_equal "<div><div>#{templ.object_id}</div></div>", templ.to_s
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 "be able to access its locals in the template definition" do
148
- templ = Undies::Template.new(:name => "awesome") do
149
- _div {
150
- _div { _ name }
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
- assert_equal(
158
- "<html><head></head><body><div class=\"loud element\" id=\"header\">YEA!!</div></body></html>",
159
- Undies::Template.new do
160
- _html {
161
- _head {}
162
- _body {
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
- file = 'test/templates/test.html.rb'
174
- assert_equal(
175
- "<html><head></head><body><div class=\"file\">FILE!!</div></body></html>",
176
- Undies::Template.new(File.expand_path(file)).to_s
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 pretty printed markup" do
181
- file = 'test/templates/test.html.rb'
182
- assert_equal(
183
- %{<html>
184
- <head>
185
- </head>
186
- <body>
187
- <div class="file">
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" }
@@ -0,0 +1,6 @@
1
+ _html {
2
+ _head {}
3
+ _body {
4
+ yield
5
+ }
6
+ }
@@ -1,8 +1,6 @@
1
1
  _html {
2
2
  _head {}
3
3
  _body {
4
- _div.file {
5
- __ "FILE!!"
6
- }
4
+ _div { _ "Hi" }
7
5
  }
8
6
  }
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: 19
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
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-08 00:00:00 Z
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