ruhl 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.1
1
+ 0.8.2
data/lib/ruhl.rb CHANGED
@@ -25,7 +25,7 @@ module Ruhl
25
25
  def render(current_scope)
26
26
  set_scope(current_scope)
27
27
 
28
- parse_doc(@document)
28
+ parse_doc(document)
29
29
 
30
30
  if @layout
31
31
  render_with_layout
@@ -54,9 +54,10 @@ module Ruhl
54
54
  render_file( File.read(file) )
55
55
  end
56
56
 
57
- def render_collection(tag, code)
57
+ def render_collection(tag, code, actions = nil)
58
58
  results = execute_ruby(tag, code)
59
59
 
60
+ tag['data-ruhl'] = actions if actions.to_s.strip.length > 0
60
61
  html = tag.to_html
61
62
 
62
63
  new_content = results.collect do |item|
@@ -73,7 +74,7 @@ module Ruhl
73
74
  end
74
75
 
75
76
  def parse_doc(doc)
76
- if (nodes = doc.xpath('//*[@data-ruhl][1]')).empty?
77
+ if (nodes = doc.xpath('*[@data-ruhl][1]')).empty?
77
78
  nodes = doc.search('*[@data-ruhl]')
78
79
  end
79
80
 
@@ -86,7 +87,8 @@ module Ruhl
86
87
  end
87
88
 
88
89
  def process_attribute(tag, code)
89
- code.split(',').each do |pair|
90
+ actions = code.split(',')
91
+ actions.dup.each_with_index do |pair, ndx|
90
92
  attribute, value = pair.split(':')
91
93
 
92
94
  if value.nil?
@@ -98,7 +100,9 @@ module Ruhl
98
100
  when "_partial"
99
101
  tag.inner_html = render_partial(tag, value)
100
102
  when "_collection"
101
- render_collection(tag, value)
103
+ actions.delete_at(ndx)
104
+ render_collection(tag, value, actions.join(','))
105
+ return
102
106
  when "_if"
103
107
  return unless process_if(tag, value)
104
108
  when "_unless"
@@ -141,6 +145,15 @@ module Ruhl
141
145
  else
142
146
  _render_
143
147
  end
148
+ rescue NoMethodError => e
149
+ puts <<CONTEXT
150
+ Context:
151
+ tag : #{tag.inspect}
152
+ code : #{code.inspect}
153
+ local_object : #{local_object.inspect}
154
+ scope : #{scope.inspect}
155
+ CONTEXT
156
+ raise e
144
157
  end
145
158
 
146
159
  def set_scope(current_scope)
data/ruhl.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruhl}
8
- s.version = "0.8.1"
8
+ s.version = "0.8.2"
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"]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "spec/html/fragment.html",
30
30
  "spec/html/if.html",
31
31
  "spec/html/layout.html",
32
+ "spec/html/loop.html",
32
33
  "spec/html/main_with_sidebar.html",
33
34
  "spec/html/medium.html",
34
35
  "spec/html/seo.html",
@@ -0,0 +1,11 @@
1
+ <html>
2
+ <head>
3
+ <title>Hello World</title>
4
+ </head>
5
+ <body>
6
+ <h1 data-ruhl="generate_h1">I am a templated headline</h1>
7
+ <select id="states">
8
+ <option data-ruhl="_collection: states_for_select, value: abbr, name">
9
+ </select>
10
+ </body>
11
+ </html>
data/spec/ruhl_spec.rb CHANGED
@@ -126,6 +126,18 @@ describe Ruhl do
126
126
  end
127
127
  end
128
128
  end
129
+
130
+ describe "loop.html" do
131
+ before do
132
+ @html = File.read html(:loop)
133
+ end
134
+
135
+ it "will be injected into layout.html" do
136
+ doc = create_doc( html(:loop) )
137
+ options = doc.xpath('/html/body/select//option')
138
+ options.children.length.should == @co.states_for_select.length
139
+ end
140
+ end
129
141
  end
130
142
 
131
143
 
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,23 @@
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 create_doc(layout = nil)
8
+ options = {:layout => layout}
9
+
10
+ html = Ruhl::Engine.new(@html, :layout => layout).
11
+ render(ContextObject.new)
12
+
13
+ do_parse(html)
14
+ end
15
+
16
+ def do_parse(html)
17
+ Nokogiri::HTML(html)
18
+ end
19
+
20
+
3
21
  class TestUser
4
22
  attr_accessor :first_name, :last_name, :email
5
23
 
@@ -52,23 +70,59 @@ class ContextObject
52
70
  def no_users_message(tag = nil)
53
71
  "Sorry no users found"
54
72
  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
73
 
65
- html = Ruhl::Engine.new(@html, :layout => layout).
66
- render(ContextObject.new)
67
-
68
- do_parse(html)
69
- end
74
+ def states_for_select(tag = nil)
75
+ state = Struct.new(:abbr, :name)
76
+ [ state.new('AL', 'Alabama'),
77
+ state.new('AK', 'Alaska'),
78
+ state.new('AZ', 'Arizona'),
79
+ state.new('AR', 'Arkansas'),
80
+ state.new('CA', 'California'),
81
+ state.new('CO', 'Colorado'),
82
+ state.new('CT', 'Connecticut'),
83
+ state.new('DE', 'Delaware'),
84
+ state.new('FL', 'Florida'),
85
+ state.new('GA', 'Georgia'),
86
+ state.new('HI', 'Hawaii'),
87
+ state.new('ID', 'Idaho'),
88
+ state.new('IL', 'Illinois'),
89
+ state.new('IN', 'Indiana'),
90
+ state.new('IA', 'Iowa'),
91
+ state.new('KS', 'Kansas'),
92
+ state.new('KY', 'Kentucky'),
93
+ state.new('LA', 'Louisiana'),
94
+ state.new('ME', 'Maine'),
95
+ state.new('MD', 'Maryland'),
96
+ state.new('MA', 'Massachusetts'),
97
+ state.new('MI', 'Michigan'),
98
+ state.new('MN', 'Minnesota'),
99
+ state.new('MS', 'Mississippi'),
100
+ state.new('MO', 'Missouri'),
101
+ state.new('MT', 'Montana'),
102
+ state.new('NE', 'Nebraska'),
103
+ state.new('NV', 'Nevada'),
104
+ state.new('NH', 'New Hampshire'),
105
+ state.new('NJ', 'New Jersey'),
106
+ state.new('NM', 'New Mexico'),
107
+ state.new('NY', 'New York'),
108
+ state.new('NC', 'North Carolina'),
109
+ state.new('ND', 'North Dakota'),
110
+ state.new('OH', 'Ohio'),
111
+ state.new('OK', 'Oklahoma'),
112
+ state.new('OR', 'Oregon'),
113
+ state.new('PA', 'Pennsylvania'),
114
+ state.new('RI', 'Rhode Island'),
115
+ state.new('SC', 'South Carolina'),
116
+ state.new('SD', 'South Dakota'),
117
+ state.new('TN', 'Tennessee'),
118
+ state.new('TX', 'Texas'),
119
+ state.new('UT', 'Utah'),
120
+ state.new('VT', 'Vermont'),
121
+ state.new('VA', 'Virginia'),
122
+ state.new('WA', 'Washington'),
123
+ state.new('WV', 'West Virginia'),
124
+ state.new('WI', 'Wisconsin'),
125
+ state.new('WY', 'Wyoming')]
126
+ end
70
127
 
71
- def do_parse(html)
72
- Nokogiri::HTML(html)
73
128
  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.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Stone
@@ -54,6 +54,7 @@ files:
54
54
  - spec/html/fragment.html
55
55
  - spec/html/if.html
56
56
  - spec/html/layout.html
57
+ - spec/html/loop.html
57
58
  - spec/html/main_with_sidebar.html
58
59
  - spec/html/medium.html
59
60
  - spec/html/seo.html