ruhl 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +33 -3
- data/VERSION +1 -1
- data/lib/ruhl.rb +49 -16
- data/ruhl.gemspec +5 -2
- data/spec/html/form.html +19 -0
- data/spec/html/hash.html +13 -0
- data/spec/html/main_with_form.html +25 -0
- data/spec/ruhl_spec.rb +46 -3
- data/spec/spec_helper.rb +16 -1
- metadata +5 -2
data/README
CHANGED
@@ -84,6 +84,8 @@ _collection - RuHL expects the method reference to return an array of objects.
|
|
84
84
|
RuHL will iteratate over the collection and render the contents
|
85
85
|
of the tag agains the collection item. (example below)
|
86
86
|
|
87
|
+
_use - The object returned is used within the scope of the tag.
|
88
|
+
|
87
89
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
88
90
|
:: Rails ::
|
89
91
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
@@ -337,6 +339,37 @@ if email.nil? then the td is not shown.
|
|
337
339
|
|
338
340
|
if users? == false then the "No Users were found" message appears.
|
339
341
|
|
342
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
343
|
+
:: Scope object to tag block (_use)::
|
344
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
345
|
+
|
346
|
+
<html>
|
347
|
+
<head>
|
348
|
+
<title>My Account</title>
|
349
|
+
</head>
|
350
|
+
<body>
|
351
|
+
<div data-ruhl="_use: current_user">
|
352
|
+
<h1> Editing <span data-ruhl="full_name"/></h1>
|
353
|
+
<table>
|
354
|
+
<thead>
|
355
|
+
<tr>
|
356
|
+
<td>First Name</td>
|
357
|
+
<td>Last Name</td>
|
358
|
+
<td>Email</td>
|
359
|
+
</tr>
|
360
|
+
</thead>
|
361
|
+
<tr>
|
362
|
+
<td data-ruhl="first_name">Andrew</td>
|
363
|
+
<td data-ruhl="last_name">Stone</td>
|
364
|
+
<td data-ruhl="_if: email">andy@stonean.com</td>
|
365
|
+
</tr>
|
366
|
+
</table>
|
367
|
+
</div>
|
368
|
+
</body>
|
369
|
+
</html>
|
370
|
+
|
371
|
+
The table row information will be replace with the current_user details.
|
372
|
+
|
340
373
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
341
374
|
:: Notes ::
|
342
375
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
@@ -345,9 +378,6 @@ if users? == false then the "No Users were found" message appears.
|
|
345
378
|
|
346
379
|
* The data-ruhl attribute is always removed from the output.
|
347
380
|
|
348
|
-
* Each method called must accept a tag parameter.
|
349
|
-
e.g def page_header(tag)
|
350
|
-
|
351
381
|
* Since it's just HTML, syntax highlighting is built-in.
|
352
382
|
For vim, just add this to your ~/.vimrc:
|
353
383
|
au BufNewFile,BufRead *.ruhl set filetype=html
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.0
|
data/lib/ruhl.rb
CHANGED
@@ -5,7 +5,8 @@ require 'ruhl/errors'
|
|
5
5
|
|
6
6
|
module Ruhl
|
7
7
|
class Engine
|
8
|
-
attr_reader :document, :scope, :layout, :layout_source,
|
8
|
+
attr_reader :document, :scope, :layout, :layout_source,
|
9
|
+
:local_object, :block_object
|
9
10
|
|
10
11
|
def initialize(html, options = {})
|
11
12
|
@local_object = options[:local_object]
|
@@ -51,6 +52,7 @@ module Ruhl
|
|
51
52
|
def render_partial(tag, code)
|
52
53
|
file = execute_ruby(tag, code)
|
53
54
|
raise PartialNotFoundError.new(file) unless File.exists?(file)
|
55
|
+
|
54
56
|
render_file( File.read(file) )
|
55
57
|
end
|
56
58
|
|
@@ -83,6 +85,7 @@ module Ruhl
|
|
83
85
|
tag = nodes.first
|
84
86
|
code = tag.remove_attribute('data-ruhl')
|
85
87
|
process_attribute(tag, code.value)
|
88
|
+
|
86
89
|
parse_doc(doc)
|
87
90
|
end
|
88
91
|
|
@@ -91,33 +94,46 @@ module Ruhl
|
|
91
94
|
actions.dup.each_with_index do |pair, ndx|
|
92
95
|
attribute, value = pair.split(':')
|
93
96
|
|
97
|
+
attribute.strip!
|
98
|
+
|
94
99
|
if value.nil?
|
95
|
-
|
100
|
+
results = execute_ruby(tag, attribute)
|
101
|
+
process_results(tag, results)
|
96
102
|
else
|
97
103
|
value.strip!
|
98
104
|
|
99
|
-
|
100
|
-
when "_partial"
|
101
|
-
tag.inner_html = render_partial(tag, value)
|
102
|
-
when "_collection"
|
103
|
-
actions.delete_at(ndx)
|
104
|
-
render_collection(tag, value, actions.join(','))
|
105
|
-
return
|
106
|
-
when "_if"
|
107
|
-
return unless process_if(tag, value)
|
108
|
-
when "_unless"
|
109
|
-
return if process_unless(tag, value)
|
110
|
-
else
|
105
|
+
unless attribute =~ /^_/
|
111
106
|
tag[attribute] = execute_ruby(tag, value)
|
107
|
+
else
|
108
|
+
case attribute
|
109
|
+
when "_use"
|
110
|
+
@block_object = execute_ruby(tag, value)
|
111
|
+
@tag_block = tag
|
112
|
+
when "_partial"
|
113
|
+
tag.inner_html = render_partial(tag, value)
|
114
|
+
when "_collection"
|
115
|
+
actions.delete_at(ndx)
|
116
|
+
render_collection(tag, value, actions.join(','))
|
117
|
+
return
|
118
|
+
when "_if"
|
119
|
+
return unless process_if(tag, value)
|
120
|
+
when "_unless"
|
121
|
+
return if process_unless(tag, value)
|
122
|
+
end
|
112
123
|
end
|
113
124
|
end
|
114
125
|
end
|
126
|
+
|
127
|
+
if @tag_block == tag
|
128
|
+
@tag_block = nil
|
129
|
+
@block_object = nil
|
130
|
+
end
|
115
131
|
end
|
116
132
|
|
117
133
|
def process_if(tag, value)
|
118
134
|
contents = execute_ruby(tag, value)
|
119
135
|
if contents
|
120
|
-
tag
|
136
|
+
process_results(tag, contents) unless contents == true
|
121
137
|
true
|
122
138
|
else
|
123
139
|
tag.remove
|
@@ -135,12 +151,28 @@ module Ruhl
|
|
135
151
|
end
|
136
152
|
end
|
137
153
|
|
154
|
+
def process_results(tag, results)
|
155
|
+
if results.is_a?(Hash)
|
156
|
+
results.each do |key, value|
|
157
|
+
if key == :inner_html
|
158
|
+
tag.inner_html = value
|
159
|
+
else
|
160
|
+
tag[key.to_s] = value
|
161
|
+
end
|
162
|
+
end
|
163
|
+
else
|
164
|
+
tag.inner_html = results
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
138
168
|
def execute_ruby(tag, code)
|
139
169
|
unless code == '_render_'
|
140
170
|
if local_object && local_object.respond_to?(code)
|
141
171
|
local_object.send(code)
|
172
|
+
elsif block_object && block_object.respond_to?(code)
|
173
|
+
block_object.send(code)
|
142
174
|
else
|
143
|
-
scope.send(code
|
175
|
+
scope.send(code)
|
144
176
|
end
|
145
177
|
else
|
146
178
|
_render_
|
@@ -151,6 +183,7 @@ Context:
|
|
151
183
|
tag : #{tag.inspect}
|
152
184
|
code : #{code.inspect}
|
153
185
|
local_object : #{local_object.inspect}
|
186
|
+
block_object : #{block_object.inspect}
|
154
187
|
scope : #{scope.inspect}
|
155
188
|
CONTEXT
|
156
189
|
raise e
|
data/ruhl.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ruhl}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.9.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-
|
12
|
+
s.date = %q{2009-10-13}
|
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 = [
|
@@ -26,10 +26,13 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/ruhl/sinatra.rb",
|
27
27
|
"ruhl.gemspec",
|
28
28
|
"spec/html/basic.html",
|
29
|
+
"spec/html/form.html",
|
29
30
|
"spec/html/fragment.html",
|
31
|
+
"spec/html/hash.html",
|
30
32
|
"spec/html/if.html",
|
31
33
|
"spec/html/layout.html",
|
32
34
|
"spec/html/loop.html",
|
35
|
+
"spec/html/main_with_form.html",
|
33
36
|
"spec/html/main_with_sidebar.html",
|
34
37
|
"spec/html/medium.html",
|
35
38
|
"spec/html/seo.html",
|
data/spec/html/form.html
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
<form action="/account" class="edit_user" id="edit_user" method="post">
|
2
|
+
|
3
|
+
<label for='user_first_name'>First Name:</label>
|
4
|
+
<input id="user_first_name" name="user[first_name]" size="30" type="text"
|
5
|
+
data-ruhl="value: first_name" />
|
6
|
+
|
7
|
+
<label for='user_last_name'>Last Name:</label>
|
8
|
+
<input id="user_last_name" name="user[last_name]" size="30" type="text"
|
9
|
+
data-ruhl="value: last_name" />
|
10
|
+
|
11
|
+
<label for='user_email'>Email:</label>
|
12
|
+
<input id="user_email" name="user[email]" size="30" type="text"
|
13
|
+
data-ruhl="value: email" />
|
14
|
+
|
15
|
+
<div class='btn_row'>
|
16
|
+
<input id="user_submit" name="commit" type="submit" value="Login" />
|
17
|
+
</div>
|
18
|
+
</form>
|
19
|
+
|
data/spec/html/hash.html
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<title>This is a title template</title>
|
4
|
+
</head>
|
5
|
+
<body>
|
6
|
+
<!-- basic hash example -->
|
7
|
+
<h1>Select User</h1>
|
8
|
+
<label class="radio" data-ruhl="_collection: user_list">
|
9
|
+
<span data-ruhl="last_name"/>
|
10
|
+
<input type="radio" data-ruhl="radio_input"/>
|
11
|
+
</label>
|
12
|
+
</body>
|
13
|
+
</html>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<title>This is a title template</title>
|
4
|
+
</head>
|
5
|
+
<body>
|
6
|
+
<h1>My Account</h1>
|
7
|
+
<div id="sidebar" data-ruhl="_use: user, _partial: form_partial">
|
8
|
+
<form action="/account" class="edit_user" id="edit_user" method="post">
|
9
|
+
|
10
|
+
<label for='user_first_name'>First Name:</label>
|
11
|
+
<input id="user_first_name" name="user[first_name]" size="30" type="text" />
|
12
|
+
|
13
|
+
<label for='user_last_name'>Last Name:</label>
|
14
|
+
<input id="user_last_name" name="user[last_name]" size="30" type="text" />
|
15
|
+
|
16
|
+
<label for='user_email'>Email:</label>
|
17
|
+
<input id="user_email" name="user[email]" size="30" type="text" />
|
18
|
+
|
19
|
+
<div class='btn_row'>
|
20
|
+
<input id="user_submit" name="commit" type="submit" value="Login" />
|
21
|
+
</div>
|
22
|
+
</form>
|
23
|
+
</div>
|
24
|
+
</body>
|
25
|
+
</html>
|
data/spec/ruhl_spec.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
3
|
describe Ruhl do
|
6
4
|
before do
|
7
5
|
@co = ContextObject.new
|
@@ -76,6 +74,16 @@ describe Ruhl do
|
|
76
74
|
|
77
75
|
it "should replace sidebar with partial contents" do
|
78
76
|
doc = create_doc
|
77
|
+
html = %{<div id="sidebar">
|
78
|
+
<h3>Real Sidebarlinks</h3>
|
79
|
+
<ul>
|
80
|
+
<li><a href="#">Real Link 1</a></li>
|
81
|
+
<li><a href="#">Real Link 2</a></li>
|
82
|
+
<li><a href="#">Real Link 3</a></li>
|
83
|
+
<li><a href="#">Real Link 4</a></li>
|
84
|
+
</ul>
|
85
|
+
</div>}
|
86
|
+
doc.xpath('//div[@id="sidebar"]').to_s.should == html
|
79
87
|
end
|
80
88
|
end
|
81
89
|
|
@@ -133,11 +141,46 @@ describe Ruhl do
|
|
133
141
|
end
|
134
142
|
|
135
143
|
it "will be injected into layout.html" do
|
136
|
-
doc = create_doc
|
144
|
+
doc = create_doc
|
137
145
|
options = doc.xpath('/html/body/select//option')
|
138
146
|
options.children.length.should == @co.states_for_select.length
|
139
147
|
end
|
140
148
|
end
|
149
|
+
|
150
|
+
describe "form.html" do
|
151
|
+
before do
|
152
|
+
@html = File.read html(:main_with_form)
|
153
|
+
@doc = create_doc
|
154
|
+
end
|
155
|
+
|
156
|
+
it "first name will be set" do
|
157
|
+
@doc.xpath('/html/body/div//input')[0]['value'].should == "Jane"
|
158
|
+
end
|
159
|
+
|
160
|
+
it "last name will be set" do
|
161
|
+
@doc.xpath('/html/body/div//input')[1]['value'].should == "Doe"
|
162
|
+
end
|
163
|
+
|
164
|
+
it "email will be set" do
|
165
|
+
@doc.xpath('/html/body/div//input')[2]['value'].should == "jane@stonean.com"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "hash.html" do
|
170
|
+
before do
|
171
|
+
@html = File.read html(:hash)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "have radio inputsi with proper attributes" do
|
175
|
+
doc = create_doc
|
176
|
+
nodes = doc.xpath('/html/body/label//input')
|
177
|
+
nodes[0]['value'].should == 'doe'
|
178
|
+
nodes[1]['value'].should == 'joe'
|
179
|
+
nodes[2]['value'].should == 'smo'
|
180
|
+
nodes[3]['value'].should == 'tin'
|
181
|
+
nodes[4]['value'].should == 'man'
|
182
|
+
end
|
183
|
+
end
|
141
184
|
end
|
142
185
|
|
143
186
|
|
data/spec/spec_helper.rb
CHANGED
@@ -19,15 +19,26 @@ end
|
|
19
19
|
|
20
20
|
|
21
21
|
class TestUser
|
22
|
-
attr_accessor :first_name, :last_name, :email
|
22
|
+
attr_accessor :id, :first_name, :last_name, :email
|
23
23
|
|
24
24
|
def initialize(first, last , email = nil)
|
25
25
|
@first_name = first
|
26
26
|
@last_name = last
|
27
27
|
@email = email
|
28
|
+
@id = rand(100)
|
29
|
+
end
|
30
|
+
|
31
|
+
def radio_input(tag = nil)
|
32
|
+
{ :inner_html => first_name, :id => "user_#{id.to_s}",
|
33
|
+
:name => "user[id]", :value => last_name.downcase}
|
28
34
|
end
|
29
35
|
end
|
30
36
|
|
37
|
+
def user(tag = nil)
|
38
|
+
TestUser.new('Jane', 'Doe', 'jane@stonean.com')
|
39
|
+
end
|
40
|
+
|
41
|
+
|
31
42
|
class ContextObject
|
32
43
|
def generate_h1(tag = nil)
|
33
44
|
"data from presenter"
|
@@ -53,6 +64,10 @@ class ContextObject
|
|
53
64
|
html(:sidebar)
|
54
65
|
end
|
55
66
|
|
67
|
+
def form_partial(tag = nil)
|
68
|
+
html(:form)
|
69
|
+
end
|
70
|
+
|
56
71
|
def user_list(tag = nil)
|
57
72
|
[
|
58
73
|
TestUser.new('Jane', 'Doe', 'jane@stonean.com'),
|
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.
|
4
|
+
version: 0.9.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-
|
12
|
+
date: 2009-10-13 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -51,10 +51,13 @@ files:
|
|
51
51
|
- lib/ruhl/sinatra.rb
|
52
52
|
- ruhl.gemspec
|
53
53
|
- spec/html/basic.html
|
54
|
+
- spec/html/form.html
|
54
55
|
- spec/html/fragment.html
|
56
|
+
- spec/html/hash.html
|
55
57
|
- spec/html/if.html
|
56
58
|
- spec/html/layout.html
|
57
59
|
- spec/html/loop.html
|
60
|
+
- spec/html/main_with_form.html
|
58
61
|
- spec/html/main_with_sidebar.html
|
59
62
|
- spec/html/medium.html
|
60
63
|
- spec/html/seo.html
|