stonean-ruhl 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -37,14 +37,46 @@ with results from meta_description method.
37
37
  :: Don't use iterators in views ::
38
38
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39
39
 
40
- <ul id="aab" ruby="present_results">
41
- <li>Lorem ipsum dolor sit amet</li>
42
- <li>consectetur adipiscing elit</li>
43
- </ul>
40
+ <table id="aab" ruby="_collection: user_list">
41
+ <tr>
42
+ <td ruby="name">John Doe</td>
43
+ <td ruby="email">john@doe.com</td>
44
+ </tr>
45
+ </table>
46
+
47
+ A few things to note about this:
48
+
49
+ 1) The method _collection calls (in this case :user_list) must implement :each
50
+
51
+ 2) Ruhl iterates over the results and renders the inner html
52
+ against each object.
53
+
54
+ 3) Ruhl will first try to call the ruby method (:name) against the
55
+ local object. If the local object does not respond_to? the ruby method,
56
+ Ruhl will try to call it against the scope.
57
+
58
+ If user_list return an array of User objects like:
59
+
60
+ [ User.create(:name => 'Rupert Boy', :email => 'rupert@stonean.com'),
61
+ User.create(:name => 'Kaylee Girl', :email => 'kaylee@stonean.com'),
62
+ User.create(:name => 'Monty Man', :email => 'monty@stonean.com')]
63
+
64
+ <table id="aab">
65
+ <tr>
66
+ <td>Rupert Boy</td>
67
+ <td>rupert@stonean.com</td>
68
+ </tr>
69
+ <tr>
70
+ <td>Kaylee Girl</td>
71
+ <td>kaylee@stonean.com</td>
72
+ </tr>
73
+ <tr>
74
+ <td>Monty Man</td>
75
+ <td>monty@stonean.com</td>
76
+ </tr>
77
+ </table>
78
+
44
79
 
45
- Method :present_results would know how to represent itself in the
46
- context of the ul element. In other words, it would know how to
47
- produce <li> elements.
48
80
 
49
81
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50
82
  :: Using a Layout ::
@@ -113,7 +145,7 @@ Sidebar:
113
145
 
114
146
  To use:
115
147
 
116
- Ruhl::Engine.new(File.read(fragment)).render(self)
148
+ Ruhl::Engine.new(File.read(path_to_main)).render(self)
117
149
 
118
150
  Returns the expected result of parsed Main with sidebar div contents
119
151
  replaced with parsed sidebar partial contents.
@@ -143,5 +175,5 @@ of the calling node.
143
175
  :: TODO ::
144
176
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145
177
 
146
- 1) Work on supporting partials (shouldn't be hard)
178
+ 1) Test more scenarios
147
179
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/lib/ruhl.rb CHANGED
@@ -5,12 +5,16 @@ require 'ruhl/errors'
5
5
 
6
6
  module Ruhl
7
7
  class Engine
8
- attr_reader :document, :scope, :layout
8
+ attr_reader :document, :scope, :layout, :local_object
9
9
 
10
10
  def initialize(html, options = {})
11
+ @local_object = options[:local_object]
12
+
11
13
  if @layout = options[:layout]
12
14
  raise LayoutNotFoundError.new(@layout) unless File.exists?(@layout)
15
+ end
13
16
 
17
+ if @layout || @local_object
14
18
  @document = Nokogiri::HTML.fragment(html)
15
19
  else
16
20
  @document = Nokogiri::HTML(html)
@@ -48,6 +52,9 @@ module Ruhl
48
52
 
49
53
  if attribute == "_partial"
50
54
  tag.inner_html = render_partial(tag, value)
55
+ elsif attribute == "_collection"
56
+ doc = render_collection(tag, value)
57
+ tag.inner_html = doc
51
58
  else
52
59
  tag[attribute] = execute_ruby(tag, value)
53
60
  end
@@ -64,6 +71,13 @@ module Ruhl
64
71
  render_file( File.read(file) )
65
72
  end
66
73
 
74
+ def render_collection(tag, code)
75
+ results = execute_ruby(tag, code)
76
+ results.collect do |item|
77
+ Ruhl::Engine.new(tag.inner_html, :local_object => item).render(scope)
78
+ end.to_s
79
+ end
80
+
67
81
  def render_file(contents)
68
82
  doc = Nokogiri::HTML( contents )
69
83
  parse_doc(doc)
@@ -72,7 +86,7 @@ module Ruhl
72
86
 
73
87
  def parse_doc(doc)
74
88
  if (nodes = doc.xpath('//*[@ruby]')).empty?
75
- nodes = doc.xpath('*[@ruby]')
89
+ nodes = doc.search('*[@ruby]')
76
90
  end
77
91
 
78
92
  nodes.each do |tag|
@@ -85,6 +99,10 @@ module Ruhl
85
99
  end
86
100
 
87
101
  tag.remove_attribute('ruby')
102
+
103
+ # We are changing the NodeSet, stop and reparse
104
+ parse_doc(doc)
105
+ break;
88
106
  end
89
107
  end
90
108
 
@@ -95,7 +113,11 @@ module Ruhl
95
113
 
96
114
  def execute_ruby(tag, code)
97
115
  unless code == '_render_'
98
- scope.send(code, tag)
116
+ if local_object && local_object.respond_to?(code)
117
+ local_object.send(code)
118
+ else
119
+ scope.send(code, tag)
120
+ end
99
121
  else
100
122
  _render_
101
123
  end
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.3.0"
8
+ s.version = "0.4.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-09-17}
12
+ s.date = %q{2009-09-24}
13
13
  s.description = %q{Make your HTML dynamic with the addition of a ruby attribute.}
14
14
  s.email = %q{andy@stonean.com}
15
15
  s.extra_rdoc_files = [
@@ -4,9 +4,13 @@
4
4
  </head>
5
5
  <body>
6
6
  <h1>This is the header template</h1>
7
- <ul ruby="present_results">
8
- <li>Lorem ipsum dolor sit amet</li>
9
- <li>consectetur adipiscing elit.</li>
7
+ <table ruby="_collection: user_list">
8
+ <tr>
9
+ <td ruby="first_name">Andrew</td>
10
+ <td ruby="last_name">Stone</td>
11
+ <td ruby="email">andy@stonean.com</td>
12
+ </tr>
13
+ </table>
10
14
  </ul>
11
15
  </body>
12
16
  </html>
data/spec/ruhl_spec.rb CHANGED
@@ -16,10 +16,6 @@ def generate_keywords(tag = nil)
16
16
  "I, am, custom, keywords"
17
17
  end
18
18
 
19
- def present_results(tag = nil)
20
- "<li>line item 1</li><li>line item 2</li>"
21
- end
22
-
23
19
  def my_content(tag = nil)
24
20
  "hello from my content."
25
21
  end
@@ -28,6 +24,16 @@ def sidebar_partial(tag = nil)
28
24
  html(:sidebar)
29
25
  end
30
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
+ ]
34
+
35
+ end
36
+
31
37
  describe Ruhl do
32
38
 
33
39
  describe "basic.html" do
@@ -66,8 +72,9 @@ describe Ruhl do
66
72
 
67
73
  it "ul content should have new li's" do
68
74
  doc = create_doc
69
- ul = doc.xpath('//ul').first
70
- ul.inner_html.should == "<li>line item 1</li>\n<li>line item 2</li>\n"
75
+ puts doc.to_s
76
+ #ul = doc.xpath('//ul').first
77
+ #ul.inner_html.should == "<li>line item 1</li>\n<li>line item 2</li>\n"
71
78
  end
72
79
  end
73
80
 
@@ -90,7 +97,6 @@ describe Ruhl do
90
97
 
91
98
  it "should replace sidebar with partial contents" do
92
99
  doc = create_doc
93
- puts doc.to_s
94
100
  end
95
101
  end
96
102
  end
data/spec/spec_helper.rb CHANGED
@@ -13,4 +13,14 @@ def create_doc(layout = nil)
13
13
  html = Ruhl::Engine.new(@html, :layout => layout).render(self)
14
14
  do_parse(html)
15
15
  end
16
+
17
+ class TestUser
18
+ attr_accessor :first_name, :last_name, :email
19
+
20
+ def initialize(first, last , email)
21
+ @first_name = first
22
+ @last_name = last
23
+ @email = email
24
+ end
25
+ end
16
26
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stonean-ruhl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.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-09-17 00:00:00 -07:00
12
+ date: 2009-09-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15