lilu 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +2 -0
- data/lib/hpricot_ext.rb +42 -0
- data/lib/lilu.rb +38 -55
- data/spec/lilu_spec.rb +8 -8
- metadata +6 -4
data/AUTHORS
ADDED
data/lib/hpricot_ext.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'hpricot'
|
2
|
+
|
3
|
+
class Hpricot::Elem
|
4
|
+
attr_accessor :cache_search
|
5
|
+
alias :_search :search
|
6
|
+
def search(expr,&block)
|
7
|
+
if @_inner_html
|
8
|
+
self.inner_html= @_inner_html
|
9
|
+
@_inner_html = nil
|
10
|
+
end
|
11
|
+
if cache_search
|
12
|
+
@_search ||= {}
|
13
|
+
@_search[expr] || @_search[expr] = _search(expr,&block)
|
14
|
+
else
|
15
|
+
_search(expr,&block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def _inner_html=(html)
|
20
|
+
@_inner_html = html
|
21
|
+
end
|
22
|
+
|
23
|
+
alias :_output :output
|
24
|
+
def output(out, opts={})
|
25
|
+
if @_inner_html
|
26
|
+
if empty? and ElementContent[@stag.name] == :EMPTY
|
27
|
+
@stag.output(out, opts.merge(:style => :empty))
|
28
|
+
else
|
29
|
+
@stag.output(out, opts)
|
30
|
+
out << @_inner_html
|
31
|
+
if @etag
|
32
|
+
@etag.output(out, opts)
|
33
|
+
elsif !opts[:preserve]
|
34
|
+
ETag.new(@stag.name).output(out,opts)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
else
|
38
|
+
_output(out,opts)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/lilu.rb
CHANGED
@@ -1,50 +1,12 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'hpricot'
|
3
2
|
require 'active_support'
|
4
3
|
|
5
|
-
|
6
|
-
attr_accessor :cache_search
|
7
|
-
alias :_search :search
|
8
|
-
def search(expr,&block)
|
9
|
-
if @_inner_html
|
10
|
-
self.inner_html= @_inner_html
|
11
|
-
@_inner_html = nil
|
12
|
-
end
|
13
|
-
if cache_search
|
14
|
-
@_search ||= {}
|
15
|
-
@_search[expr] || @_search[expr] = _search(expr,&block)
|
16
|
-
else
|
17
|
-
_search(expr,&block)
|
18
|
-
end
|
19
|
-
end
|
4
|
+
require File.dirname(__FILE__) + '/hpricot_ext'
|
20
5
|
|
21
|
-
def _inner_html=(html)
|
22
|
-
@_inner_html = html
|
23
|
-
end
|
24
6
|
|
25
|
-
alias :_output :output
|
26
|
-
def output(out, opts={})
|
27
|
-
if @_inner_html
|
28
|
-
if empty? and ElementContent[@stag.name] == :EMPTY
|
29
|
-
@stag.output(out, opts.merge(:style => :empty))
|
30
|
-
else
|
31
|
-
@stag.output(out, opts)
|
32
|
-
out << @_inner_html
|
33
|
-
if @etag
|
34
|
-
@etag.output(out, opts)
|
35
|
-
elsif !opts[:preserve]
|
36
|
-
ETag.new(@stag.name).output(out,opts)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
else
|
40
|
-
_output(out,opts)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
7
|
module Lilu
|
46
8
|
|
47
|
-
module Version ; MAJOR, MINOR, TINY = 0, 1,
|
9
|
+
module Version ; MAJOR, MINOR, TINY = 0, 1, 3 ; end
|
48
10
|
|
49
11
|
class Action
|
50
12
|
attr_accessor :element
|
@@ -102,7 +64,7 @@ module Lilu
|
|
102
64
|
when Proc
|
103
65
|
with(new_element.call.to_s)
|
104
66
|
when nil
|
105
|
-
with(block
|
67
|
+
with renderer.instance_eval(&block) if block_given?
|
106
68
|
else
|
107
69
|
element.swap new_element.to_s
|
108
70
|
end
|
@@ -118,8 +80,8 @@ module Lilu
|
|
118
80
|
arg.each_pair do |path,value|
|
119
81
|
value = value.call if value.is_a?(Proc)
|
120
82
|
case path
|
121
|
-
when
|
122
|
-
elem =
|
83
|
+
when ElementAt
|
84
|
+
elem = path.find_at(element)
|
123
85
|
raise ElementNotFound.new(elem) unless elem
|
124
86
|
|
125
87
|
saved_element = element
|
@@ -129,7 +91,7 @@ module Lilu
|
|
129
91
|
res
|
130
92
|
when Replacing
|
131
93
|
Replace.new(path.element,renderer).with value.to_s
|
132
|
-
when
|
94
|
+
when ElementText
|
133
95
|
element._inner_html = value.to_s
|
134
96
|
else
|
135
97
|
element[path] = value.to_s
|
@@ -155,7 +117,9 @@ module Lilu
|
|
155
117
|
end
|
156
118
|
end
|
157
119
|
|
158
|
-
|
120
|
+
|
121
|
+
# Helpers
|
122
|
+
|
159
123
|
class Replacing
|
160
124
|
attr_reader :element
|
161
125
|
def initialize(renderer,element)
|
@@ -168,6 +132,18 @@ module Lilu
|
|
168
132
|
end
|
169
133
|
end
|
170
134
|
end
|
135
|
+
|
136
|
+
class ElementAt
|
137
|
+
attr_reader :path
|
138
|
+
def initialize(renderer,path)
|
139
|
+
@renderer, @path = @renderer, path
|
140
|
+
end
|
141
|
+
def find_at(element)
|
142
|
+
element.at(path)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
class ElementText ; include Singleton ; end
|
171
147
|
#
|
172
148
|
|
173
149
|
class ElementNotFound < Exception
|
@@ -222,25 +198,32 @@ module Lilu
|
|
222
198
|
render({:partial => name}.merge(opts))
|
223
199
|
end
|
224
200
|
|
225
|
-
# Helper for lambda
|
226
|
-
alias_method :L, :lambda
|
227
|
-
|
228
201
|
def element_at(path)
|
229
202
|
doc.at(path)
|
230
203
|
end
|
231
204
|
|
205
|
+
# Helper for Replacing
|
206
|
+
def replacing(element)
|
207
|
+
Replacing.new(self,element)
|
208
|
+
end
|
209
|
+
|
210
|
+
# Helper for ElementAt
|
211
|
+
def at(element)
|
212
|
+
ElementAt.new(self,element)
|
213
|
+
end
|
214
|
+
|
215
|
+
# Helper for ElementText
|
216
|
+
def text
|
217
|
+
ElementText.instance
|
218
|
+
end
|
219
|
+
|
232
220
|
def method_missing(sym,*args)
|
233
221
|
return @view.send(sym,*args) if @view and @view.respond_to?(sym)
|
234
|
-
return instance_variable_get("@#{sym}") if args.empty? and instance_variables.member?
|
222
|
+
return instance_variable_get("@#{sym}") if args.empty? and instance_variables.member?("@#{sym}")
|
235
223
|
return @controller.send(sym, *args) if @controller and @controller.respond_to?(sym)
|
236
224
|
raise NoMethodError.new(sym.to_s)
|
237
225
|
end
|
238
|
-
|
239
|
-
# Helper for replacing
|
240
|
-
def replacing(element)
|
241
|
-
Replacing.new(self,element)
|
242
|
-
end
|
243
|
-
|
226
|
+
|
244
227
|
protected
|
245
228
|
|
246
229
|
def find_elements(*path)
|
data/spec/lilu_spec.rb
CHANGED
@@ -72,7 +72,7 @@ describe Lilu::Renderer do
|
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should update element details on update(path).with Hash construct" do
|
75
|
-
@instructions = %{update("#some-data").with :id => "some-lilu-data", "a" => { :href => "/",
|
75
|
+
@instructions = %{update("#some-data").with :id => "some-lilu-data", at("a") => { :href => "/", text => "is here" } }
|
76
76
|
@html_source = %{<div id="some-data">Lola <a href="#">is there</a></div>}
|
77
77
|
@renderer = Lilu::Renderer.new(@instructions,@html_source)
|
78
78
|
result = Hpricot(@renderer.apply)
|
@@ -83,7 +83,7 @@ describe Lilu::Renderer do
|
|
83
83
|
|
84
84
|
it "should populate element details on populate(path).for(:each,@blogs) { block } construct" do
|
85
85
|
@blogs = [OpenStruct.new(:url => "http://railsware.com", :blog_id => 1, :name => "Railsware"),OpenStruct.new(:url => "http://railsware.com/", :blog_id => 2, :name => "Railsware!")]
|
86
|
-
@instructions = %{populate("#blog-example").for(:each,@blogs) {|blog| mapping 'a' => {:href => blog.url,
|
86
|
+
@instructions = %{populate("#blog-example").for(:each,@blogs) {|blog| mapping at('a') => {:href => blog.url, text => blog.name}, :id => blog.blog_id } }
|
87
87
|
@html_source = %{<ul id="blogs"><li id="blog-example"><a href="#">My Blog</a></li></ul>}
|
88
88
|
@renderer = Lilu::Renderer.new(@instructions,@html_source,self)
|
89
89
|
result = Hpricot(@renderer.apply)
|
@@ -97,7 +97,7 @@ describe Lilu::Renderer do
|
|
97
97
|
|
98
98
|
it "should populate element details on populate(:all,path).for(:each,@blogs) { block } construct" do
|
99
99
|
@blogs = [OpenStruct.new(:url => "http://railsware.com", :blog_id => 1, :name => "Railsware")]
|
100
|
-
@instructions = %{populate(:all,".blog-example").for(:each,@blogs) {|blog| mapping 'a' => {:href => blog.url,
|
100
|
+
@instructions = %{populate(:all,".blog-example").for(:each,@blogs) {|blog| mapping at('a') => {:href => blog.url, text => blog.name}, :id => blog.blog_id } }
|
101
101
|
@html_source = %{<ul id="blogs1"><li id="blog-example" class="blog-example"><a href="#">My Blog</a></li></ul><ul id="blogs2"><li id="blog-example" class="blog-example"><a href="#">My Blog</a></li></ul>}
|
102
102
|
@renderer = Lilu::Renderer.new(@instructions,@html_source,self)
|
103
103
|
result = Hpricot(@renderer.apply)
|
@@ -111,7 +111,7 @@ describe Lilu::Renderer do
|
|
111
111
|
end
|
112
112
|
|
113
113
|
it "should update all elements, matched by path on update(:all, path).with Hash construct" do
|
114
|
-
@instructions = %{update(:all, "a").with :href => "#",
|
114
|
+
@instructions = %{update(:all, "a").with :href => "#", text => 'Stay here'}
|
115
115
|
@html_source = %{<a href="http://java.net">C'mon, Java!</a><a href="http://www.php.net">Go away!</a>}
|
116
116
|
@renderer = Lilu::Renderer.new(@instructions,@html_source)
|
117
117
|
result = Hpricot(@renderer.apply)
|
@@ -119,7 +119,7 @@ describe Lilu::Renderer do
|
|
119
119
|
end
|
120
120
|
|
121
121
|
it "should update all elements, matched by path on update(:all, path).with Hash construct, taking in account each element content" do
|
122
|
-
@instructions = 'update(:all, "a").with :href =>
|
122
|
+
@instructions = 'update(:all, "a").with :href => lambda{"#{element[:href]}/download"}, text => lambda{"Download #{element.inner_html}"}'
|
123
123
|
links = {"http://java.net" => "C'mon, Java!", "http://www.php.net" => "Go away!"}
|
124
124
|
@html_source = ""
|
125
125
|
links.each_pair {|url,name| @html_source << %{<a href="#{url}">#{name}</a>} }
|
@@ -131,7 +131,7 @@ describe Lilu::Renderer do
|
|
131
131
|
end
|
132
132
|
|
133
133
|
it "should update all elements, matched by path on update(:all, path).with Block construct, taking in account each element content" do
|
134
|
-
@instructions = 'update(:all, "a").with { mapping :href => "#{element[:href]}/download",
|
134
|
+
@instructions = 'update(:all, "a").with { mapping :href => "#{element[:href]}/download", text => "Download #{element.inner_html }" }'
|
135
135
|
links = {"http://java.net" => "C'mon, Java!", "http://www.php.net" => "Go away!"}
|
136
136
|
@html_source = ""
|
137
137
|
links.each_pair {|url,name| @html_source << %{<a href="#{url}">#{name}</a>} }
|
@@ -143,7 +143,7 @@ describe Lilu::Renderer do
|
|
143
143
|
end
|
144
144
|
|
145
145
|
it "should update all elements, matched by path on update(:all, path).with Lambda construct, taking in account each element content" do
|
146
|
-
@instructions = 'update(:all, "a").with
|
146
|
+
@instructions = 'update(:all, "a").with lambda { mapping :href => "#{element[:href]}/download", text => "Download #{element.inner_html }" }'
|
147
147
|
links = {"http://java.net" => "C'mon, Java!", "http://www.php.net" => "Go away!"}
|
148
148
|
@html_source = ""
|
149
149
|
links.each_pair {|url,name| @html_source << %{<a href="#{url}">#{name}</a>} }
|
@@ -186,7 +186,7 @@ describe Lilu::Renderer do
|
|
186
186
|
end
|
187
187
|
|
188
188
|
it "should replace element with another using replace().with Lambda construct" do
|
189
|
-
@instructions = "replace('#main').with
|
189
|
+
@instructions = "replace('#main').with lambda { 'Hello!' }"
|
190
190
|
@html_source = '<html><body><div id="main">Blablabla</div></body></html>'
|
191
191
|
@renderer = Lilu::Renderer.new(@instructions,@html_source)
|
192
192
|
result = Hpricot(@renderer.apply)
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: lilu
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.1.3
|
7
|
+
date: 2007-07-08 00:00:00 +02:00
|
8
8
|
summary: View subsystem that allows to keep pure HTML for views
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -15,10 +15,11 @@ description: View subsystem that allows to keep pure HTML for views
|
|
15
15
|
autorequire:
|
16
16
|
- rake
|
17
17
|
- hpricot
|
18
|
+
- lib/hpricot_ext
|
18
19
|
- lib/lilu
|
19
20
|
- lib/lilu_view
|
20
21
|
- lib/lilu_camping
|
21
|
-
default_executable:
|
22
|
+
default_executable:
|
22
23
|
bindir: bin
|
23
24
|
has_rdoc: false
|
24
25
|
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
@@ -33,8 +34,9 @@ cert_chain:
|
|
33
34
|
post_install_message:
|
34
35
|
authors:
|
35
36
|
- Yurii Rashkovskii
|
36
|
-
- Michael Holub
|
37
37
|
files:
|
38
|
+
- AUTHORS
|
39
|
+
- lib/hpricot_ext.rb
|
38
40
|
- lib/lilu.rb
|
39
41
|
- lib/lilu_view.rb
|
40
42
|
- lib/lilu_camping.rb
|