ruhl 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,5 +1,6 @@
1
1
  RuHL (Ruby Hypertext Language)
2
2
 
3
+ **gem available on gemcutter: http://gemcutter.org/gems/ruhl
3
4
 
4
5
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6
  :: What? ::
@@ -70,9 +71,10 @@ _render_ - This is used in your layout and lets rule know where to inject
70
71
  _partial - Path to a partial file. RuHL must be able to find/read this file.
71
72
 
72
73
  _if - If the method returns nil, the tag will not be included on output.
73
-
74
- _render_if - If the method returns false, the tag will not be included on output.
75
74
 
75
+ _unless - If the method does not return nil, the tag will not be
76
+ included on output.
77
+
76
78
  _collection - RuHL expects the method reference to return an array of objects.
77
79
  RuHL will iteratate over the collection and render the contents
78
80
  of the tag agains the collection item. (example below)
@@ -236,7 +238,7 @@ of the calling node.
236
238
 
237
239
 
238
240
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239
- :: Conditional display of block (_render_if)::
241
+ :: Conditional display of block (_if)::
240
242
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
241
243
 
242
244
  <html>
@@ -245,7 +247,7 @@ of the calling node.
245
247
  </head>
246
248
  <body>
247
249
  <h1>This is the header template</h1>
248
- <div data-ruhl="_render_if: has_users?">
250
+ <div data-ruhl="_if: users?">
249
251
  <table>
250
252
  <thead>
251
253
  <tr>
@@ -265,7 +267,7 @@ of the calling node.
265
267
  </body>
266
268
  </html>
267
269
 
268
- if has_users? returns false then the div (including it's contents) are not shown
270
+ if users? returns false then the div (including it's contents) are not shown
269
271
  on output.
270
272
 
271
273
 
@@ -297,7 +299,38 @@ on output.
297
299
  </body>
298
300
  </html>
299
301
 
300
- if email == nil then the td is not shown.
302
+ if email.nil? then the td is not shown.
303
+
304
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
305
+ :: Conditional display of tag (_unless)::
306
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
307
+
308
+ <html>
309
+ <head>
310
+ <title>This is a title template</title>
311
+ </head>
312
+ <body>
313
+ <h1>This is the header template</h1>
314
+ <table data-ruhl="_if: users?">
315
+ <thead>
316
+ <tr>
317
+ <td>First Name</td>
318
+ <td>Last Name</td>
319
+ <td>Email</td>
320
+ </tr>
321
+ </thead>
322
+ <tr data-ruhl="_collection: user_list">
323
+ <td data-ruhl="first_name">Andrew</td>
324
+ <td data-ruhl="last_name">Stone</td>
325
+ <td data-ruhl="_if: email">andy@stonean.com</td>
326
+ </tr>
327
+ </table>
328
+ <p data-ruhl="_unless: users?">No Users were found.</p>
329
+ </ul>
330
+ </body>
331
+ </html>
332
+
333
+ if users? == false then the "No Users were found" message appears.
301
334
 
302
335
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303
336
  :: Notes ::
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.0
1
+ 0.8.0
@@ -90,24 +90,19 @@ module Ruhl
90
90
  attribute, value = pair.split(':')
91
91
 
92
92
  if value.nil?
93
- tag.inner_html = execute_ruby(tag, attribute)
93
+ tag.inner_html = execute_ruby(tag, attribute.strip)
94
94
  else
95
95
  value.strip!
96
- if attribute == "_partial"
96
+
97
+ case attribute
98
+ when "_partial"
97
99
  tag.inner_html = render_partial(tag, value)
98
- elsif attribute == "_collection"
100
+ when "_collection"
99
101
  render_collection(tag, value)
100
- elsif attribute == "_if"
101
- contents = execute_ruby(tag, value)
102
- if contents
103
- tag.inner_html = contents
104
- else
105
- tag.remove
106
- end
107
- elsif attribute == "_render_if"
108
- unless execute_ruby(tag, value)
109
- tag.remove
110
- end
102
+ when "_if"
103
+ return unless process_if(tag, value)
104
+ when "_unless"
105
+ return if process_unless(tag, value)
111
106
  else
112
107
  tag[attribute] = execute_ruby(tag, value)
113
108
  end
@@ -115,6 +110,28 @@ module Ruhl
115
110
  end
116
111
  end
117
112
 
113
+ def process_if(tag, value)
114
+ contents = execute_ruby(tag, value)
115
+ if contents
116
+ tag.inner_html = contents unless contents == true
117
+ true
118
+ else
119
+ tag.remove
120
+ false
121
+ end
122
+ end
123
+
124
+ def process_unless(tag, value)
125
+ contents = execute_ruby(tag, value)
126
+ if contents
127
+ tag.remove
128
+ true
129
+ else
130
+ tag.inner_html = contents unless contents == false
131
+ false
132
+ end
133
+ end
134
+
118
135
  def execute_ruby(tag, code)
119
136
  unless code == '_render_'
120
137
  if local_object && local_object.respond_to?(code)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruhl}
8
- s.version = "0.7.0"
8
+ s.version = "0.8.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Stone"]
12
- s.date = %q{2009-10-07}
12
+ s.date = %q{2009-10-08}
13
13
  s.description = %q{Make your HTML dynamic with the addition of a data-ruhl attribute.}
14
14
  s.email = %q{andy@stonean.com}
15
15
  s.extra_rdoc_files = [
@@ -31,7 +31,6 @@ Gem::Specification.new do |s|
31
31
  "spec/html/layout.html",
32
32
  "spec/html/main_with_sidebar.html",
33
33
  "spec/html/medium.html",
34
- "spec/html/render_if.html",
35
34
  "spec/html/seo.html",
36
35
  "spec/html/sidebar.html",
37
36
  "spec/rcov.opts",
@@ -4,7 +4,7 @@
4
4
  </head>
5
5
  <body>
6
6
  <h1>This is the header template</h1>
7
- <table>
7
+ <table data-ruhl="_if: users?">
8
8
  <thead>
9
9
  <tr>
10
10
  <td>First Name</td>
@@ -18,6 +18,6 @@
18
18
  <td data-ruhl="_if: email">andy@stonean.com</td>
19
19
  </tr>
20
20
  </table>
21
- </ul>
21
+ <p data-ruhl="_unless: users?, no_users_message"/>
22
22
  </body>
23
23
  </html>
@@ -1,40 +1,11 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
- def generate_h1(tag = nil)
4
- "data from presenter"
5
- end
6
3
 
7
- def data_from_method(tag = nil)
8
- "I am data from a method"
9
- end
10
-
11
- def generate_description(tag = nil)
12
- "I am a custom meta description"
13
- end
14
-
15
- def generate_keywords(tag = nil)
16
- "I, am, custom, keywords"
17
- end
18
-
19
- def my_content(tag = nil)
20
- "hello from my content."
21
- end
22
-
23
- def sidebar_partial(tag = nil)
24
- html(:sidebar)
25
- end
26
-
27
- def user_list(tag = nil)
28
- [
29
- TestUser.new('Jane', 'Doe', 'jane@stonean.com'),
30
- TestUser.new('John', 'Joe', 'john@stonean.com'),
31
- TestUser.new('Jake', 'Smo', 'jake@stonean.com'),
32
- TestUser.new('Paul', 'Tin', 'paul@stonean.com'),
33
- TestUser.new('NoMail', 'Man')
34
- ]
35
- end
36
4
 
37
5
  describe Ruhl do
6
+ before do
7
+ @co = ContextObject.new
8
+ end
38
9
 
39
10
  describe "basic.html" do
40
11
  before do
@@ -43,7 +14,7 @@ describe Ruhl do
43
14
 
44
15
  it "content of p should be content from data_from_method" do
45
16
  doc = create_doc
46
- doc.xpath('//h1').first.content.should == generate_h1
17
+ doc.xpath('//h1').first.content.should == @co.generate_h1
47
18
  end
48
19
  end
49
20
 
@@ -55,13 +26,13 @@ describe Ruhl do
55
26
  it "meta keywords should be replaced" do
56
27
  doc = create_doc
57
28
  doc.xpath('//meta[@name="keywords"]').first['content'].
58
- should == generate_keywords
29
+ should == @co.generate_keywords
59
30
  end
60
31
 
61
32
  it "meta title should be replaced" do
62
33
  doc = create_doc
63
34
  doc.xpath('//meta[@name="description"]').first['content'].
64
- should == generate_description
35
+ should == @co.generate_description
65
36
  end
66
37
  end
67
38
 
@@ -109,52 +80,50 @@ describe Ruhl do
109
80
  end
110
81
 
111
82
  describe "if.html" do
112
- before do
113
- @html = File.read html(:if)
114
- @doc = create_doc
115
- end
116
-
117
- it "first data row should equal first user " do
118
- table = @doc.xpath('/html/body/table/tr//td')
119
- table.children[0].to_s.should == "Jane"
120
- table.children[1].to_s.should == "Doe"
121
- table.children[2].to_s.should == "jane@stonean.com"
122
- end
123
-
124
- it "last data row should equal last user " do
125
- table = @doc.xpath('/html/body/table/tr//td')
126
- table.children[12].to_s.should == "NoMail"
127
- table.children[13].to_s.should == "Man"
128
- table.children[14].should == nil
129
- end
130
- end
83
+ describe "no users" do
84
+ before do
85
+ class ContextObject
86
+ def users?(tag = nil)
87
+ false
88
+ end
89
+ end
90
+
91
+ @html = File.read html(:if)
92
+ @doc = create_doc
93
+ end
131
94
 
132
- describe "render if.html" do
133
- it "table should not render" do
134
- def has_users?(tag = nil)
135
- false
95
+ it "table should not render" do
96
+ nodes = @doc.xpath('/html/body//*')
97
+ nodes.children.length.should == 2
98
+ nodes.children[0].to_s.should == "This is the header template"
136
99
  end
137
100
 
138
- @html = File.read html(:render_if)
139
- @doc = create_doc
140
- nodes = @doc.xpath('/html/body//*')
141
- nodes.children.length.should == 1
142
- nodes.children.to_s.should == "This is the header template"
101
+ it "no user message should render" do
102
+ nodes = @doc.xpath('/html/body//*')
103
+ nodes.children[1].to_s.should == @co.no_users_message
104
+ end
143
105
  end
144
106
 
145
- it "table shouldrender" do
146
- def has_users?(tag = nil)
147
- true
107
+ describe "has users" do
108
+ before do
109
+ class ContextObject
110
+ def users?(tag = nil)
111
+ true
112
+ end
113
+ end
114
+
115
+ @html = File.read html(:if)
116
+ @doc = create_doc
148
117
  end
149
118
 
150
- @html = File.read html(:render_if)
151
- @doc = create_doc
152
- nodes = @doc.xpath('/html/body//*')
153
- nodes.children.length.should > 1
119
+ it "table should render" do
120
+ nodes = @doc.xpath('/html/body//*')
121
+ nodes.children.length.should > 1
154
122
 
155
- table = @doc.xpath('/html/body/div/table/tr//td')
156
- table.children[12].to_s.should == "NoMail"
157
- table.children[13].to_s.should == "Man"
123
+ table = @doc.xpath('/html/body/table/tr//td')
124
+ table.children[12].to_s.should == "NoMail"
125
+ table.children[13].to_s.should == "Man"
126
+ end
158
127
  end
159
128
  end
160
129
  end
@@ -1,19 +1,5 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib ruhl]))
2
2
 
3
- def html(name)
4
- File.join( File.dirname(__FILE__), 'html', "#{name}.html" )
5
- end
6
-
7
- def do_parse(html)
8
- Nokogiri::HTML(html)
9
- end
10
-
11
- def create_doc(layout = nil)
12
- options = {:layout => layout}
13
- html = Ruhl::Engine.new(@html, :layout => layout).render(self)
14
- do_parse(html)
15
- end
16
-
17
3
  class TestUser
18
4
  attr_accessor :first_name, :last_name, :email
19
5
 
@@ -24,3 +10,65 @@ class TestUser
24
10
  end
25
11
  end
26
12
 
13
+ class ContextObject
14
+ def generate_h1(tag = nil)
15
+ "data from presenter"
16
+ end
17
+
18
+ def data_from_method(tag = nil)
19
+ "I am data from a method"
20
+ end
21
+
22
+ def generate_description(tag = nil)
23
+ "I am a custom meta description"
24
+ end
25
+
26
+ def generate_keywords(tag = nil)
27
+ "I, am, custom, keywords"
28
+ end
29
+
30
+ def my_content(tag = nil)
31
+ "hello from my content."
32
+ end
33
+
34
+ def sidebar_partial(tag = nil)
35
+ html(:sidebar)
36
+ end
37
+
38
+ def user_list(tag = nil)
39
+ [
40
+ TestUser.new('Jane', 'Doe', 'jane@stonean.com'),
41
+ TestUser.new('John', 'Joe', 'john@stonean.com'),
42
+ TestUser.new('Jake', 'Smo', 'jake@stonean.com'),
43
+ TestUser.new('Paul', 'Tin', 'paul@stonean.com'),
44
+ TestUser.new('NoMail', 'Man')
45
+ ]
46
+ end
47
+
48
+ def users?(tag = nil)
49
+ true
50
+ end
51
+
52
+ def no_users_message(tag = nil)
53
+ "Sorry no users found"
54
+ end
55
+ end
56
+
57
+
58
+ def html(name)
59
+ File.join( File.dirname(__FILE__), 'html', "#{name}.html" )
60
+ end
61
+
62
+ def create_doc(layout = nil)
63
+ options = {:layout => layout}
64
+
65
+ html = Ruhl::Engine.new(@html, :layout => layout).
66
+ render(ContextObject.new)
67
+
68
+ do_parse(html)
69
+ end
70
+
71
+ def do_parse(html)
72
+ Nokogiri::HTML(html)
73
+ end
74
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruhl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Stone
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-07 00:00:00 -04:00
12
+ date: 2009-10-08 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -56,7 +56,6 @@ files:
56
56
  - spec/html/layout.html
57
57
  - spec/html/main_with_sidebar.html
58
58
  - spec/html/medium.html
59
- - spec/html/render_if.html
60
59
  - spec/html/seo.html
61
60
  - spec/html/sidebar.html
62
61
  - spec/rcov.opts
@@ -1,25 +0,0 @@
1
- <html>
2
- <head>
3
- <title>This is a title template</title>
4
- </head>
5
- <body>
6
- <h1>This is the header template</h1>
7
- <div data-ruhl="_render_if: has_users?">
8
- <table>
9
- <thead>
10
- <tr>
11
- <td>First Name</td>
12
- <td>Last Name</td>
13
- <td>Email</td>
14
- </tr>
15
- </thead>
16
- <tr data-ruhl="_collection: user_list">
17
- <td data-ruhl="first_name">Andrew</td>
18
- <td data-ruhl="last_name">Stone</td>
19
- <td data-ruhl="email">andy@stonean.com</td>
20
- </tr>
21
- </table>
22
- </div>
23
- </ul>
24
- </body>
25
- </html>