garterbelt 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/TODO CHANGED
@@ -1,11 +1,28 @@
1
1
  increase performance ~10%
2
- partial passes down params from parent view
2
+
3
3
  anonymous views
4
4
 
5
- rails style helpers
5
+ view
6
+ class level view required defaults to [] not nil
7
+ join on widgets?
8
+ pass block to render method so you can yield!
9
+ better error messaging when rendering buffer, which line in content is causing the problem?
10
+ script and style tags auto escape
11
+
12
+ partial
13
+ pass the block content to partial instances too
14
+ partial passes down params from parent view
15
+
16
+ page
17
+ embed_js, embed_css file content, string content
18
+ make content method = page_content, so that downstream views can just use content as their main method
19
+
6
20
 
7
- render styles
8
- - pretty: current and default
9
- - compact: argument content in the tags, not new line
10
- - minified: no new lines or indenting
11
- - text only: no tags, appropriate new lines according to tag type
21
+ name changes (for less name collision):
22
+ options to initialization_options
23
+ curator to _curator
24
+ look at other names for possible issues
25
+
26
+
27
+ # CoverGirl
28
+ rails style helpers
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{garterbelt}
8
- s.version = "0.0.7"
8
+ s.version = "0.0.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kane Baccigalupi"]
12
- s.date = %q{2011-04-13}
12
+ s.date = %q{2011-04-18}
13
13
  s.description = %q{Garterbelt is a Ruby HTML/XML markup framework inspired by Erector and Markaby. Garterbelt maps html tags to methods allowing the intuitive construction of HTML pages using nothing but Ruby. And because it is all Ruby all the time, views benefit from the dryness of inheritance, modules and all the meta magic that Ruby can imagine. Stockings not included.}
14
14
  s.email = %q{baccigalupi@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -44,12 +44,16 @@ Gem::Specification.new do |s|
44
44
  "lib/view.rb",
45
45
  "spec/garterbelt_spec.rb",
46
46
  "spec/integration/expectations/general_view.html",
47
+ "spec/integration/expectations/render_styles/compact.html",
48
+ "spec/integration/expectations/render_styles/minified.html",
49
+ "spec/integration/expectations/render_styles/text.html",
47
50
  "spec/integration/expectations/unescaping_view.html",
48
51
  "spec/integration/expectations/variables/view_with_user_and_params.html",
49
52
  "spec/integration/expectations/variables/view_with_user_email.html",
50
53
  "spec/integration/expectations/view_partial_nest.html",
51
54
  "spec/integration/expectations/view_with_forms.html",
52
55
  "spec/integration/expectations/view_with_tags.html",
56
+ "spec/integration/integration_spec.rb",
53
57
  "spec/integration/templates/form.rb",
54
58
  "spec/integration/templates/unescaping_view.rb",
55
59
  "spec/integration/templates/view_partial_nest.rb",
@@ -59,7 +63,6 @@ Gem::Specification.new do |s|
59
63
  "spec/integration/templates/view_with_partial_2.rb",
60
64
  "spec/integration/templates/view_with_tags.rb",
61
65
  "spec/integration/templates/view_with_vars.rb",
62
- "spec/integration/view_spec.rb",
63
66
  "spec/page_spec.rb",
64
67
  "spec/performance/profiling.rb",
65
68
  "spec/performance/templates/erector.rb",
@@ -72,6 +75,7 @@ Gem::Specification.new do |s|
72
75
  "spec/renderers/doctype_spec.rb",
73
76
  "spec/renderers/text_spec.rb",
74
77
  "spec/spec_helper.rb",
78
+ "spec/string_spec.rb",
75
79
  "spec/support/mock_view.rb",
76
80
  "spec/support/puters.rb",
77
81
  "spec/view/view_basics_spec.rb",
@@ -88,6 +92,7 @@ Gem::Specification.new do |s|
88
92
  s.summary = %q{Garterbelt is a Ruby HTML/XML markup framework. It is san DSL. Just all Ruby, all the time.}
89
93
  s.test_files = [
90
94
  "spec/garterbelt_spec.rb",
95
+ "spec/integration/integration_spec.rb",
91
96
  "spec/integration/templates/form.rb",
92
97
  "spec/integration/templates/unescaping_view.rb",
93
98
  "spec/integration/templates/view_partial_nest.rb",
@@ -97,7 +102,6 @@ Gem::Specification.new do |s|
97
102
  "spec/integration/templates/view_with_partial_2.rb",
98
103
  "spec/integration/templates/view_with_tags.rb",
99
104
  "spec/integration/templates/view_with_vars.rb",
100
- "spec/integration/view_spec.rb",
101
105
  "spec/page_spec.rb",
102
106
  "spec/performance/profiling.rb",
103
107
  "spec/performance/templates/erector.rb",
@@ -110,6 +114,7 @@ Gem::Specification.new do |s|
110
114
  "spec/renderers/doctype_spec.rb",
111
115
  "spec/renderers/text_spec.rb",
112
116
  "spec/spec_helper.rb",
117
+ "spec/string_spec.rb",
113
118
  "spec/support/mock_view.rb",
114
119
  "spec/support/puters.rb",
115
120
  "spec/view/view_basics_spec.rb",
@@ -45,7 +45,11 @@ module Garterbelt
45
45
  end
46
46
 
47
47
  def template
48
- "#{indent}<#{type}#{rendered_attributes}>\n"
48
+ if style == :text
49
+ type == :br ? "\n" : ""
50
+ else
51
+ "#{indent}<#{type}#{rendered_attributes}>#{line_end}"
52
+ end
49
53
  end
50
54
 
51
55
  def render
@@ -4,9 +4,13 @@ module Garterbelt
4
4
  super
5
5
  end
6
6
 
7
+ def template
8
+ view.render_style == :text ? "" : "#{indent}<!-- #{content} -->#{line_end}"
9
+ end
10
+
7
11
  def render
8
12
  raise_with_block_content
9
- output << "#{indent}<!-- #{content} -->\n"
13
+ output << template
10
14
  end
11
15
  end
12
16
  end
@@ -23,14 +23,26 @@ module Garterbelt
23
23
  self
24
24
  end
25
25
 
26
+ def head_template
27
+ style == :text ? '' : "#{indent}<#{type}#{rendered_attributes}>#{line_end}"
28
+ end
29
+
26
30
  def head
27
- self.output << "#{indent}<#{type}#{rendered_attributes}>\n"
31
+ self.output << head_template
28
32
  super
29
33
  end
30
34
 
35
+ def foot_template
36
+ if style == :text
37
+ [:p, :ul, :ol, :li].include?(type) ? "\n" : ''
38
+ else
39
+ "#{indent}</#{type}>#{line_end}"
40
+ end
41
+ end
42
+
31
43
  def foot
32
44
  super
33
- self.output << "#{indent}</#{type}>\n"
45
+ self.output << foot_template
34
46
  end
35
47
  end
36
48
  end
@@ -19,8 +19,16 @@ module Garterbelt
19
19
  view.level
20
20
  end
21
21
 
22
+ def style
23
+ view.render_style
24
+ end
25
+
22
26
  def indent
23
- ' '*level*2
27
+ style == :minified ? "" : ' '*level*2
28
+ end
29
+
30
+ def line_end
31
+ style == :minified ? "" : "\n"
24
32
  end
25
33
 
26
34
  def render
@@ -4,8 +4,7 @@ module Garterbelt
4
4
 
5
5
  def initialize(opts)
6
6
  super
7
- raise ArgumentError, ":content option required for #{self.class} initialization" unless opts[:content]
8
- self.content = opts[:content]
7
+ self.content = opts[:content] || ''
9
8
  self.escape = view.escape
10
9
  end
11
10
 
@@ -15,11 +14,23 @@ module Garterbelt
15
14
 
16
15
  def render
17
16
  raise_with_block_content
18
- output << "#{indent}#{escaped_content}\n"
17
+ str = template
18
+ output << str
19
+ str
19
20
  end
20
21
 
21
- def escaped_content
22
- escape ? ERB::Util.h(content) : content
22
+ def line_end
23
+ [:pretty, :text].include?(style) ? "\n" : ''
24
+ end
25
+
26
+ def template
27
+ str = escape ? ERB::Util.h(content) : content
28
+
29
+ if style == :pretty
30
+ "#{str.wrap(Garterbelt.wrap_length, :indent => indent)}#{line_end}"
31
+ else
32
+ "#{str}#{line_end}"
33
+ end
23
34
  end
24
35
  end
25
36
  end
@@ -8,4 +8,12 @@ module Garterbelt
8
8
  raise "Cache #{store.inspect} has not yet been configured" unless c
9
9
  c
10
10
  end
11
+
12
+ class << self
13
+ attr_writer :wrap_length
14
+ end
15
+
16
+ def self.wrap_length
17
+ @wrap_length || 80
18
+ end
11
19
  end
@@ -1,3 +1,29 @@
1
+ class String
2
+ def wrap(limit = 50, opts={})
3
+ strip!
4
+ indent = opts[:indent] || ''
5
+ return indent << self if size + indent.size <= limit || !include?(' ')
6
+
7
+ words = self.split(/\s/)
8
+ lines = [[indent]]
9
+ char_count = indent.size
10
+ words.each do |word|
11
+ word_size = word.size + 1
12
+ if (word_size + char_count <= limit) || lines.last.size == 1
13
+ char_count += word_size
14
+ lines.last << word
15
+ else
16
+ char_count = word_size + indent.size
17
+ lines << [indent, word]
18
+ end
19
+ end
20
+ lines.map do |line|
21
+ indent = line.shift
22
+ indent + line.join(' ')
23
+ end.join("\n")
24
+ end
25
+ end
26
+
1
27
  unless String.instance_methods.include?('underscore')
2
28
  begin
3
29
  # being selective if activesupport 3.0.x
@@ -2,13 +2,14 @@ module Garterbelt
2
2
  class View
3
3
  # include RuPol::Swimsuit
4
4
 
5
- attr_accessor :output, :buffer, :level, :escape, :block, :options
5
+ attr_accessor :output, :buffer, :level, :escape, :block, :options, :render_style
6
6
  attr_reader :curator
7
7
 
8
8
  def initialize(opts={}, &block)
9
9
  self.options = opts
10
10
  self.buffer = []
11
- self.level = (options.delete(:level) || 0)
11
+ self.level = options.delete(:level) || 0
12
+ self.render_style = options.delete(:style) || :pretty
12
13
  self.output = ""
13
14
  self.escape = true
14
15
  self.block = block if block_given?
@@ -39,6 +40,7 @@ module Garterbelt
39
40
  self.level = parent_view.level
40
41
  self.output = parent_view.output
41
42
  self.escape = parent_view.escape
43
+ self.render_style = parent_view.render_style
42
44
  end
43
45
  end
44
46
 
@@ -254,13 +256,33 @@ module Garterbelt
254
256
  raise NotImplementedError, "Implement #content in #{self.class}!"
255
257
  end
256
258
 
257
- def render(content_method = :content)
258
- self.output = "" if curated?
259
- if content_method == :content
260
- content
259
+ class << self
260
+ attr_writer :default_render_style, :default_content_method
261
+ end
262
+
263
+ def self.default_render_style
264
+ @default_render_style ||= :pretty
265
+ end
266
+
267
+ def self.default_content_method
268
+ @default_content_method ||= :content
269
+ end
270
+
271
+ def render(*args)
272
+ if args.first.is_a?(Hash)
273
+ options = args.shift
274
+ content_method = options[:method]
261
275
  else
262
- send(content_method)
276
+ content_method = args.shift
277
+ options = args.shift || {}
263
278
  end
279
+
280
+ content_method ||= self.class.default_content_method
281
+ self.render_style = options[:style] || self.class.default_render_style
282
+
283
+ self.output = "" if curated?
284
+ send(content_method)
285
+
264
286
  render_buffer
265
287
  output
266
288
  end
@@ -1,6 +1,21 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Garterbelt do
4
+ describe 'wrap length' do
5
+ after :all do
6
+ Garterbelt.wrap_length = 80
7
+ end
8
+
9
+ it 'is 80 by default' do
10
+ Garterbelt.wrap_length.should == 80
11
+ end
12
+
13
+ it 'can be customized' do
14
+ Garterbelt.wrap_length = 50
15
+ Garterbelt.wrap_length.should == 50
16
+ end
17
+ end
18
+
4
19
  describe 'cache module level methods' do
5
20
  describe '#cache_hash' do
6
21
  it 'creates a default hash when accessed the first time' do
@@ -0,0 +1,15 @@
1
+ <div class="line">
2
+ <div class="unit size1of2">
3
+ <h4>Login</h4>
4
+ <form class="inner" action="/login">
5
+ <label class="input">Username or Email
6
+ <input name="login" type="text">
7
+ </label>
8
+ <label class="input">Password
9
+ <input name="password" type="password">
10
+ </label>
11
+ <hr class="light">
12
+ <input type="submit" value="Login">
13
+ </form>
14
+ </div>
15
+ </div>
@@ -0,0 +1 @@
1
+ <div class="line"><div class="unit size1of2"><h4>Login</h4><form class="inner" action="/login"><label class="input">Username or Email<input name="login" type="text"></label><label class="input">Password<input name="password" type="password"></label><hr class="light"><input type="submit" value="Login"></form></div></div>
@@ -0,0 +1,3 @@
1
+ Login
2
+ Username or Email
3
+ Password
@@ -1,3 +1,2 @@
1
- You should check out my rad new site:<br>
2
- <a class="user_generated_link" href="http://foo.com">http://foo.com</a><br>
3
- It will blow your mind!
1
+ You should check out my rad new site:<br> <a class="user_generated_link"
2
+ href="http://foo.com">http://foo.com</a><br> It will blow your mind!
@@ -17,11 +17,11 @@
17
17
  Provide us with updated information
18
18
  </h4>
19
19
  <label>
20
- Name:
20
+ Name:
21
21
  <input name="name" type="text">
22
22
  </label>
23
23
  <label>
24
- Email:
24
+ Email:
25
25
  <input name="email" type="text">
26
26
  </label>
27
27
  <input type="submit" value="Update">
@@ -65,4 +65,14 @@ describe Garterbelt::View, "Integration" do
65
65
  ViewWithForms.new.render.should == file('view_with_forms')
66
66
  end
67
67
  end
68
+
69
+ describe 'render styles' do
70
+ it 'does minified' do
71
+ ViewWithContentTags.new.render(:style => :minified).should == file("render_styles/minified")
72
+ end
73
+
74
+ it 'does text only' do
75
+ ViewWithContentTags.new.render(:style => :text).should == file("render_styles/text")
76
+ end
77
+ end
68
78
  end
@@ -16,12 +16,12 @@ class ViewWithForms < Garterbelt::View
16
16
  partial FormView, :action => '/user/info', :method => 'put', :class => 'update' do
17
17
  h4 "Provide us with updated information"
18
18
  label do
19
- text "Name: "
19
+ text "Name:"
20
20
  input :type => 'text', :name => 'name'
21
21
  end
22
22
 
23
23
  label do
24
- text "Email: "
24
+ text "Email:"
25
25
  input :type => 'text', :name => 'email'
26
26
  end
27
27
  input :type => 'submit', :value => 'Update'
@@ -4,8 +4,7 @@ describe Garterbelt::ClosedTag do
4
4
  ClosedTag = Garterbelt::ClosedTag unless defined?(ClosedTag)
5
5
 
6
6
  before do
7
- @output = ''
8
- @view = mock(:output => @output, :level => 2)
7
+ @view = MockView.new
9
8
  end
10
9
 
11
10
  describe 'initialize' do
@@ -79,7 +78,7 @@ describe Garterbelt::ClosedTag do
79
78
  end
80
79
 
81
80
  it 'uses its output' do
82
- @tag.output.should == @output
81
+ @tag.output.should == @view.output
83
82
  end
84
83
 
85
84
  it 'uses its level' do
@@ -133,7 +132,7 @@ describe Garterbelt::ClosedTag do
133
132
  end
134
133
  end
135
134
 
136
- describe 'integration' do
135
+ describe 'integration (:pretty style)' do
137
136
  before do
138
137
  @str = @tag.render
139
138
  end
@@ -155,7 +154,38 @@ describe Garterbelt::ClosedTag do
155
154
  end
156
155
 
157
156
  it 'adds the string to the output' do
158
- @output.should include @str
157
+ @view.output.should include @str
158
+ end
159
+ end
160
+
161
+ describe 'styles' do
162
+ before do
163
+ @tag = ClosedTag.new(:type => :input, :view => @view)
164
+ @view.render_style = :pretty
165
+ @pretty = @tag.render
166
+ end
167
+
168
+ describe ':minified' do
169
+ before do
170
+ @view.render_style = :minified
171
+ @min = @tag.render
172
+ end
173
+
174
+ it 'does not end in a line break' do
175
+ @min.should_not match /\n$/
176
+ end
177
+
178
+ it 'does not have any indentation' do
179
+ @pretty.should match /^\s{4}</
180
+ @min.should_not match /^\s{4}</
181
+ end
182
+ end
183
+
184
+ describe ':text' do
185
+ it 'is an empty string' do
186
+ @view.render_style = :text
187
+ @tag.render.should == ''
188
+ end
159
189
  end
160
190
  end
161
191
  end
@@ -16,12 +16,6 @@ describe Garterbelt::Comment do
16
16
  comment.content = "foo"
17
17
  comment.content.should == "foo"
18
18
  end
19
-
20
- it 'raises an error when initializing without content' do
21
- lambda{ Garterbelt::Comment.new(:view => @view) }.should raise_error(
22
- ArgumentError, ":content option required for Garterbelt::Comment initialization"
23
- )
24
- end
25
19
  end
26
20
 
27
21
  describe 'render' do
@@ -30,35 +24,69 @@ describe Garterbelt::Comment do
30
24
  @comment = Garterbelt::Comment.new(:view => @view, :content => 'Render me')
31
25
  end
32
26
 
33
- it 'raises an error with block content' do
34
- @comment.content = lambda { puts "foo" }
35
- lambda{ @comment.render }.should raise_error(ArgumentError, "Garterbelt::Comment does not take block content")
36
- end
27
+ describe 'basics' do
28
+ it 'raises an error with block content' do
29
+ @comment.content = lambda { puts "foo" }
30
+ lambda{ @comment.render }.should raise_error(ArgumentError, "Garterbelt::Comment does not take block content")
31
+ end
37
32
 
38
- it 'it adds the content to the output' do
39
- @comment.render
40
- @view.output.should include "Render me"
41
- end
33
+ it 'it adds the content to the output' do
34
+ @comment.render
35
+ @view.output.should include "Render me"
36
+ end
42
37
 
43
- it 'builds the right header tag' do
44
- @comment.render
45
- @view.output.should match /<!-- Render me/
46
- end
38
+ it 'builds the right header tag' do
39
+ @comment.render
40
+ @view.output.should match /<!-- Render me/
41
+ end
47
42
 
48
- it 'builds the right footer tag' do
49
- @comment.render
50
- @view.output.should match /Render me -->/
51
- end
43
+ it 'builds the right footer tag' do
44
+ @comment.render
45
+ @view.output.should match /Render me -->/
46
+ end
47
+
48
+ it 'indents to the view level' do
49
+ @comment.render
50
+ @view.output.should match /^\W{4}<!-- Render me/
51
+ end
52
52
 
53
- it 'indents to the view level' do
54
- @comment.render
55
- @view.output.should match /^\W{4}<!-- Render me/
53
+ it 'does not escape the content' do
54
+ @comment.content = "<div>foo</div>"
55
+ @comment.render
56
+ @view.output.should include "<div>foo</div>"
57
+ end
56
58
  end
57
59
 
58
- it 'does not escape the content' do
59
- @comment.content = "<div>foo</div>"
60
- @comment.render
61
- @view.output.should include "<div>foo</div>"
60
+ describe 'styles' do
61
+ before do
62
+ @tag = Garterbelt::Comment.new(:view => @view, :content => 'foo')
63
+ @view.render_style = :pretty
64
+ @pretty = @tag.render
65
+ @view.output = ''
66
+ end
67
+
68
+ describe ':minified' do
69
+ before do
70
+ @view.render_style = :minified
71
+ @min = @tag.render
72
+ end
73
+
74
+ it 'does not end in a line break' do
75
+ @min.should_not match /\n$/
76
+ end
77
+
78
+ it 'does not have any indentation' do
79
+ @pretty.should match /^\s{4}</
80
+ @min.should_not match /^\s{4}</
81
+ end
82
+ end
83
+
84
+ describe ':text' do
85
+ it 'is an empty string' do
86
+ @view.render_style = :text
87
+ @tag.render.should == ''
88
+ end
89
+ end
62
90
  end
63
91
  end
64
92
  end
@@ -80,6 +80,51 @@ describe Garterbelt::ContentTag do
80
80
  end
81
81
  end
82
82
 
83
+ describe 'styles' do
84
+ before do
85
+ @tag.content = "My string content"
86
+ @view.render_style = :pretty
87
+ @view.output = ''
88
+ end
89
+
90
+ describe ':minified' do
91
+ before do
92
+ @view.render_style = :minified
93
+ @min = @tag.render
94
+ end
95
+
96
+ it 'does not end in a line break' do
97
+ @min.should_not match /\n$/
98
+ end
99
+
100
+ it 'does not have any indentation' do
101
+ @min.should_not match /^\s{4}</
102
+ end
103
+ end
104
+
105
+ describe ':text' do
106
+ before do
107
+ @view.render_style = :text
108
+ end
109
+
110
+ it 'has no tags' do
111
+ @tag.render.should_not match /<[^>]>*/
112
+ end
113
+
114
+ [:p, :ul, :ol, :li].each do |type|
115
+ it ":#{type} includes a line break" do
116
+ @tag.type = type
117
+ @tag.render.should match /\n/
118
+ end
119
+ end
120
+
121
+ it 'has no line breaks othewise' do
122
+ @tag.type = :div
123
+ @tag.render.should_not match /\n/
124
+ end
125
+ end
126
+ end
127
+
83
128
  describe 'content' do
84
129
  describe 'none' do
85
130
  it 'works' do
@@ -16,12 +16,6 @@ describe Garterbelt::Text do
16
16
  text.content = "foo"
17
17
  text.content.should == "foo"
18
18
  end
19
-
20
- it 'raises an error when initializing without content' do
21
- lambda{ Garterbelt::Text.new(:view => @view) }.should raise_error(
22
- ArgumentError, ":content option required for Garterbelt::Text initialization"
23
- )
24
- end
25
19
  end
26
20
 
27
21
  describe 'render' do
@@ -40,11 +34,6 @@ describe Garterbelt::Text do
40
34
  @view.output.should include "Render me"
41
35
  end
42
36
 
43
- it 'indents to the view level' do
44
- @text.render
45
- @view.output.should match /^\W{4}Render me\n$/
46
- end
47
-
48
37
  describe 'escaping' do
49
38
  it 'escapes if view is set to escape' do
50
39
  str = "<a href='/foo.com'>Foo it!</a>"
@@ -60,5 +49,62 @@ describe Garterbelt::Text do
60
49
  text.render.should include str
61
50
  end
62
51
  end
52
+
53
+ describe 'styles' do
54
+ before do
55
+ @str = "123456789 "*40 # 100 char long
56
+ @tag = Garterbelt::Text.new(:content => @str, :view => @view)
57
+ @view.render_style = :pretty
58
+ @pretty = @tag.render
59
+ end
60
+
61
+ describe ':pretty' do
62
+ it 'indents to the view level' do
63
+ rendered = @tag.render
64
+ @view.output.should include rendered
65
+ end
66
+
67
+ it 'wraps' do
68
+ @tag = Garterbelt::Text.new(:content => "12345678 "*15, :view => @view)
69
+ matcher = "12345678 "*7 + "12345678"
70
+ @tag.render.should match /^ #{matcher}\n/
71
+ end
72
+
73
+ it 'ends in a line break' do
74
+ @tag.render.should match /\n\z/
75
+ end
76
+ end
77
+
78
+ describe ':minified' do
79
+ before do
80
+ @view.render_style = :minified
81
+ @minified = @tag.render
82
+ end
83
+
84
+ it 'does not have any line break(s)' do
85
+ @minified.should_not match /\n/
86
+ end
87
+
88
+ it 'does not have any indentation' do
89
+ @pretty.should match /^\s{4}1/
90
+ @minified.should_not match /^\s{4}a1/
91
+ end
92
+ end
93
+
94
+ describe ':text' do
95
+ before do
96
+ @view.render_style = :text
97
+ @text = @tag.render
98
+ end
99
+
100
+ it 'should end in a line break' do
101
+ @text.should match /\n\z/
102
+ end
103
+
104
+ it 'should not wrap' do
105
+ @text.should_not match /\n1/
106
+ end
107
+ end
108
+ end
63
109
  end
64
110
  end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe String, 'extension' do
4
+ describe '#wrap(limit, opts)' do
5
+ describe 'limiting' do
6
+ it 'does not require arguments' do
7
+ lambda {''.wrap}.should_not raise_error
8
+ end
9
+
10
+ it 'doesn\'t break a string without spaces' do
11
+ breakless = "123"*50
12
+ breakless.wrap.should == breakless
13
+ end
14
+
15
+ it 'defaults to 50 characters if not given a limit' do
16
+ space_at_five = '1234 '*70
17
+ space_at_five.wrap.should match /^#{'1234 '*9}1234\n/
18
+ end
19
+
20
+ it 'takes an alternate limit' do
21
+ space_at_five = '1234 '*70
22
+ space_at_five.wrap(100).should match /^#{'1234 '*19}1234\n/
23
+ end
24
+
25
+ it 'does not break in the middle of a word' do
26
+ '1234567 foo'.wrap(5).should == "1234567\nfoo"
27
+ end
28
+
29
+ it 'does nothing in a string that is shorter than the limit' do
30
+ '12345'.wrap.should == '12345'
31
+ end
32
+ end
33
+
34
+ describe 'indent option' do
35
+ it 'appends it to the front of each line' do
36
+ space_at_five = '1234 '*5
37
+ space_at_five.wrap(10, :indent => ' '*5).split(' '*5).size.should == 6
38
+ end
39
+
40
+ it 'takes the indent into account when choosing a break point' do
41
+ space_at_five = '1234 '*70
42
+ str = " 1234 1234 1234 1234 1234 1234 1234 1234 1234"
43
+ space_at_five.wrap(50, :indent => ' '*5).should match /^#{str}\n/
44
+ end
45
+
46
+ it 'adds the indent to returned strings even if not wrapping' do
47
+ 'string'.wrap(10, :indent => ' ').should == ' string'
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,11 +1,12 @@
1
1
  class MockView
2
- attr_accessor :output, :buffer, :level, :escape, :cache_store
2
+ attr_accessor :output, :buffer, :level, :escape, :cache_store, :render_style
3
3
 
4
4
  def initialize
5
5
  self.buffer = []
6
6
  self.output = ""
7
7
  self.level ||= 2
8
8
  self.escape = true
9
+ self.render_style = :pretty
9
10
  self.cache_store = Moneta::Memory.new
10
11
  end
11
12
 
@@ -60,17 +60,23 @@ describe Garterbelt::View do
60
60
  view.block.is_a?(Proc).should be_true
61
61
  end
62
62
 
63
- it 'can save the options' do
63
+ it 'saves the options' do
64
64
  view = BasicView.new(:foo => 'foo', :bar => 'bar')
65
65
  view.options.should == {:foo => 'foo', :bar => 'bar'}
66
66
  end
67
67
 
68
+ it 'render_style defaults to :pretty' do
69
+ view = BasicView.new
70
+ view.render_style.should == :pretty
71
+ end
72
+
68
73
  describe 'setting the curator: view responsible for displaying the rendered content' do
69
74
  before do
70
75
  @view.level = 42
71
76
  @view.output = "foo"
72
77
  @view.buffer = ["bar"]
73
78
  @view.escape = false
79
+ @view.render_style = :text
74
80
  @child = BasicView.new(:curator => @view)
75
81
  end
76
82
 
@@ -104,6 +110,10 @@ describe Garterbelt::View do
104
110
  it 'sets the escape to the curator\'s' do
105
111
  @child.escape.should == @view.escape
106
112
  end
113
+
114
+ it 'sets the render_style to the curator\'s' do
115
+ @child.render_style.should == @view.render_style
116
+ end
107
117
  end
108
118
  end
109
119
 
@@ -20,6 +20,34 @@ describe Garterbelt::View do
20
20
  @view.output.should_not include 'Foo the magic output!'
21
21
  end
22
22
 
23
+ describe 'argument parsing' do
24
+ describe 'render method' do
25
+ it 'finds in as the first argument' do
26
+ @view.should_receive(:bar)
27
+ @view.render(:bar)
28
+ end
29
+
30
+ it 'finds it in the options' do
31
+ @view.should_receive(:baz)
32
+ @view.render :method => :baz
33
+ end
34
+ end
35
+
36
+ describe ':style option' do
37
+ it 'is found in options as the second argument' do
38
+ @view.stub(:foo)
39
+ @view.render(:foo, {:style => :minified})
40
+ @view.render_style.should == :minified
41
+ end
42
+
43
+ it 'is found when options are the first argument' do
44
+ @view.stub(:content)
45
+ @view.render(:style => :text)
46
+ @view.render_style.should == :text
47
+ end
48
+ end
49
+ end
50
+
23
51
  describe 'methods' do
24
52
  it 'renders the :content method by default' do
25
53
  @view.should_receive(:content)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garterbelt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kane Baccigalupi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-13 00:00:00 -07:00
18
+ date: 2011-04-18 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -241,12 +241,16 @@ files:
241
241
  - lib/view.rb
242
242
  - spec/garterbelt_spec.rb
243
243
  - spec/integration/expectations/general_view.html
244
+ - spec/integration/expectations/render_styles/compact.html
245
+ - spec/integration/expectations/render_styles/minified.html
246
+ - spec/integration/expectations/render_styles/text.html
244
247
  - spec/integration/expectations/unescaping_view.html
245
248
  - spec/integration/expectations/variables/view_with_user_and_params.html
246
249
  - spec/integration/expectations/variables/view_with_user_email.html
247
250
  - spec/integration/expectations/view_partial_nest.html
248
251
  - spec/integration/expectations/view_with_forms.html
249
252
  - spec/integration/expectations/view_with_tags.html
253
+ - spec/integration/integration_spec.rb
250
254
  - spec/integration/templates/form.rb
251
255
  - spec/integration/templates/unescaping_view.rb
252
256
  - spec/integration/templates/view_partial_nest.rb
@@ -256,7 +260,6 @@ files:
256
260
  - spec/integration/templates/view_with_partial_2.rb
257
261
  - spec/integration/templates/view_with_tags.rb
258
262
  - spec/integration/templates/view_with_vars.rb
259
- - spec/integration/view_spec.rb
260
263
  - spec/page_spec.rb
261
264
  - spec/performance/profiling.rb
262
265
  - spec/performance/templates/erector.rb
@@ -269,6 +272,7 @@ files:
269
272
  - spec/renderers/doctype_spec.rb
270
273
  - spec/renderers/text_spec.rb
271
274
  - spec/spec_helper.rb
275
+ - spec/string_spec.rb
272
276
  - spec/support/mock_view.rb
273
277
  - spec/support/puters.rb
274
278
  - spec/view/view_basics_spec.rb
@@ -313,6 +317,7 @@ specification_version: 3
313
317
  summary: Garterbelt is a Ruby HTML/XML markup framework. It is san DSL. Just all Ruby, all the time.
314
318
  test_files:
315
319
  - spec/garterbelt_spec.rb
320
+ - spec/integration/integration_spec.rb
316
321
  - spec/integration/templates/form.rb
317
322
  - spec/integration/templates/unescaping_view.rb
318
323
  - spec/integration/templates/view_partial_nest.rb
@@ -322,7 +327,6 @@ test_files:
322
327
  - spec/integration/templates/view_with_partial_2.rb
323
328
  - spec/integration/templates/view_with_tags.rb
324
329
  - spec/integration/templates/view_with_vars.rb
325
- - spec/integration/view_spec.rb
326
330
  - spec/page_spec.rb
327
331
  - spec/performance/profiling.rb
328
332
  - spec/performance/templates/erector.rb
@@ -335,6 +339,7 @@ test_files:
335
339
  - spec/renderers/doctype_spec.rb
336
340
  - spec/renderers/text_spec.rb
337
341
  - spec/spec_helper.rb
342
+ - spec/string_spec.rb
338
343
  - spec/support/mock_view.rb
339
344
  - spec/support/puters.rb
340
345
  - spec/view/view_basics_spec.rb