tadpole 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008 Loren Segal
1
+ Copyright (c) 2008, 2009 Loren Segal
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person
4
4
  obtaining a copy of this software and associated documentation
@@ -1,7 +1,7 @@
1
1
  Tadpole: A Small but Extensible Templating Engine for Ruby
2
2
  ==========================================================
3
3
 
4
- _Created by [Loren Segal](http://www.gnuu.org) in 2008_
4
+ _Created by [Loren Segal](http://www.gnuu.org) in 2008-2009_
5
5
 
6
6
  Quick How-To's
7
7
  --------------
@@ -1,5 +1,5 @@
1
1
  module Tadpole
2
- Version = '0.1.6'
2
+ Version = '0.1.7'
3
3
  Root = File.dirname(__FILE__)
4
4
 
5
5
  module SectionProviders
@@ -103,10 +103,7 @@ module Tadpole
103
103
  def initialize(opts = {}, &block)
104
104
  self.options = opts
105
105
  @providers = {}
106
- #self.sections(*sections)
107
106
 
108
- init(&block)
109
-
110
107
  if Tadpole.caching
111
108
  @compiled_sections = compile_sections(sections)
112
109
  end
@@ -116,14 +113,18 @@ module Tadpole
116
113
 
117
114
  def run(opts = {}, &block)
118
115
  return '' if run_before_run.is_a?(FalseClass)
119
-
120
- old_opts = options
121
- self.options = options.to_hash.update(opts)
122
- out = run_sections(@compiled_sections || sections, &block)
123
- self.options = old_opts
124
- out
116
+
117
+ with_options(opts) do
118
+ init
119
+ run_sections(@compiled_sections || sections, &block)
120
+ end
125
121
  rescue => e
126
- provider = find_section_provider(current_section)
122
+ begin
123
+ provider = find_section_provider(current_section)
124
+ rescue ArgumentError
125
+ raise e
126
+ end
127
+
127
128
  line = provider.full_path
128
129
  line += " (template)" if provider.is_a?(SectionProviders::TemplateProvider)
129
130
  line += ":1:in#{@compiled_sections ? ' compiled' : ''} section `#{current_section}'"
@@ -134,27 +135,29 @@ module Tadpole
134
135
  alias to_s run
135
136
 
136
137
  def run_sections(sects, break_first = false, locals = {}, &block)
137
- out = ''
138
- raise ArgumentError, "Template(#{path}) is missing sections" unless sects
139
- sects = sects.first if sects.first.is_a?(Array)
140
- sects.each_with_index do |section, i|
141
- (break_first ? break : next) if section.is_a?(Array)
142
-
143
- self.current_section = section_name(section)
144
-
145
- next if run_before_sections.is_a?(FalseClass)
138
+ with_options(locals) do
139
+ out = ''
140
+ raise ArgumentError, "Template(#{path}) is missing sections" unless sects
141
+ sects = sects.first if sects.first.is_a?(Array)
142
+ sects.each_with_index do |section, i|
143
+ (break_first ? break : next) if section.is_a?(Array)
144
+
145
+ self.current_section = section_name(section)
146
+
147
+ next if run_before_sections.is_a?(FalseClass)
146
148
 
147
- if sects[i+1].is_a?(Array)
148
- old, self.subsections = subsections, sects[i+1]
149
- out += run_subsections(section, sects[i+1], locals, &block)
150
- self.subsections = old
151
- else
152
- out += render(section, locals, true, &block)
149
+ if sects[i+1].is_a?(Array)
150
+ old, self.subsections = subsections, sects[i+1]
151
+ out += run_subsections(section, sects[i+1], &block)
152
+ self.subsections = old
153
+ else
154
+ out += render(section, {}, true, &block)
155
+ end
156
+
157
+ break if break_first
153
158
  end
154
-
155
- break if break_first
159
+ out
156
160
  end
157
- out
158
161
  end
159
162
 
160
163
  def run_subsections(section, subsections, locals = {}, &block)
@@ -189,21 +192,23 @@ module Tadpole
189
192
  end
190
193
 
191
194
  def render(section, locals = {}, call_method = false, &block)
192
- case section
193
- when String, Symbol
194
- if call_method && respond_to?(section)
195
- send(section, &block)
195
+ with_options(locals) do
196
+ case section
197
+ when String, Symbol
198
+ if call_method && respond_to?(section)
199
+ send(section, &block)
200
+ else
201
+ find_section_provider(section).render(options, &block)
202
+ end
203
+ when Template
204
+ section.run(options, &block)
205
+ when Module
206
+ section.new(options).run(&block)
207
+ when SectionProviders::SectionProvider
208
+ section.render(options, &block)
196
209
  else
197
- find_section_provider(section).render(locals, &block)
210
+ raise MissingSectionError, "missing section #{section.inspect}"
198
211
  end
199
- when Template
200
- section.run(locals, &block)
201
- when Module
202
- section.new(options).run(locals, &block)
203
- when SectionProviders::SectionProvider
204
- section.render(locals, &block)
205
- else
206
- raise MissingSectionError, "missing section #{section.inspect}"
207
212
  end
208
213
  end
209
214
 
@@ -233,6 +238,14 @@ module Tadpole
233
238
  [nil, {}]
234
239
  end
235
240
  end
241
+
242
+ def with_options(locals = {}, &block)
243
+ old_options = options
244
+ self.options = options.to_hash.merge(locals)
245
+ result = yield
246
+ self.options = old_options
247
+ result
248
+ end
236
249
 
237
250
  def find_section_provider(section)
238
251
  section = section.to_s
@@ -0,0 +1,21 @@
1
+ def init
2
+ sections :a, [:b, [:c, :d]]
3
+ end
4
+
5
+ def a
6
+ '[' + yield(:foo => 'FOO') + ']'
7
+ end
8
+
9
+ def b
10
+ foo + yieldall(:foo => 'BAR', :bar => 'BAR')
11
+ end
12
+
13
+ def c
14
+ foo
15
+ end
16
+
17
+ def d
18
+ bar
19
+ end
20
+
21
+ # expected [FOOBARBAR]
@@ -0,0 +1 @@
1
+ <%= foo %><%= yieldall %>
@@ -0,0 +1,3 @@
1
+ def init
2
+ sections :b, ['2_2']
3
+ end
@@ -0,0 +1 @@
1
+ <%= foo || "FOO" %>
@@ -0,0 +1,3 @@
1
+ def init
2
+ sections :c
3
+ end
@@ -0,0 +1 @@
1
+ <%= yieldall :foo => 'BAR' %>
@@ -0,0 +1,3 @@
1
+ def init
2
+ sections :a, ['2_1']
3
+ end
@@ -0,0 +1,16 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'tadpole')
2
+
3
+ describe Tadpole, '::Template' do
4
+ before do
5
+ Tadpole.template_paths.clear
6
+ Tadpole.register_template_path File.dirname(__FILE__) + '/examples'
7
+ end
8
+
9
+ it "should yield to a method with extra values" do
10
+ Template('yield/1').new.run.should == '[FOOBARBAR]'
11
+ end
12
+
13
+ it "should yield to external template with extra values" do
14
+ Template('yield/2').new(:foo => 'FOO').run.should == 'BARBAR'
15
+ end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tadpole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loren Segal
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-20 00:00:00 -04:00
12
+ date: 2009-08-14 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -63,9 +63,17 @@ files:
63
63
  - spec/examples/T/derived2/test/setup.rb
64
64
  - spec/examples/T/setup.rb
65
65
  - spec/examples/T/test/setup.rb
66
+ - spec/examples/yield/1/setup.rb
67
+ - spec/examples/yield/2/2_1/b.erb
68
+ - spec/examples/yield/2/2_1/setup.rb
69
+ - spec/examples/yield/2/2_2/c.erb
70
+ - spec/examples/yield/2/2_2/setup.rb
71
+ - spec/examples/yield/2/a.erb
72
+ - spec/examples/yield/2/setup.rb
66
73
  - spec/filters_spec.rb
67
74
  - spec/local_template_spec.rb
68
75
  - spec/render_spec.rb
76
+ - spec/yield_spec.rb
69
77
  - benchmarks/eval-vs-non-eval.rb
70
78
  - benchmarks/require-vs-none.rb
71
79
  - benchmarks/run-caching.rb