tadpole 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ module Tadpole
12
12
  include mod
13
13
  before_run_filters.push(*mod.before_run_filters)
14
14
  before_section_filters.push(*mod.before_section_filters)
15
- inherited_paths.push(*mod.template_paths)
15
+ inherited_paths.unshift(*mod.template_paths)
16
16
  end
17
17
  end
18
18
  end
@@ -24,7 +24,18 @@ module Tadpole
24
24
 
25
25
  def T(extra_path, extra_opts = {})
26
26
  opts = options.to_hash.update(extra_opts)
27
- Template(path, extra_path).new(opts)
27
+ dirname = File.dirname(caller.first[/^(.+?):/, 1])
28
+ begin
29
+ Template(path, extra_path).new(opts)
30
+ rescue MissingTemplateError => e
31
+ Tadpole.template_paths.each do |template_path|
32
+ if dirname.index(template_path) == 0
33
+ path = dirname[template_path.length..-1]
34
+ return Template(path, extra_path).new(opts)
35
+ end
36
+ end
37
+ raise e
38
+ end
28
39
  end
29
40
 
30
41
  included(self)
data/lib/tadpole/main.rb CHANGED
@@ -19,6 +19,8 @@ class Insertion
19
19
  end
20
20
 
21
21
  module Tadpole
22
+ class MissingTemplateError < ArgumentError; end
23
+
22
24
  class << self
23
25
  attr_accessor :caching
24
26
 
@@ -35,7 +37,7 @@ module Tadpole
35
37
  def template(*path)
36
38
  path = absolutize_path(*path)
37
39
  exists = find_matching_template_paths(path)
38
- raise ArgumentError, "no such template `#{path}'" if exists.empty?
40
+ raise MissingTemplateError, "no such template `#{path}'" if exists.empty?
39
41
 
40
42
  create_template(*path)
41
43
  end
@@ -73,15 +75,17 @@ module Tadpole
73
75
  find_matching_template_paths(total_list).each do |subpath|
74
76
  submod = create_local_template_mod(subpath, total_list)
75
77
  mod.send :include, submod
78
+
79
+ inherited = submod.inherited_paths
80
+ inherited.reject! {|path| mod.template_paths.include?(path) }
81
+ mod.template_paths.unshift(*inherited)
76
82
  mod.template_paths.unshift(*submod.template_paths)
77
83
  mod.before_run_filters.push(*submod.before_run_filters)
78
84
  mod.before_section_filters.push(*submod.before_section_filters)
79
- inherited.push(*submod.inherited_paths)
80
85
  end
81
86
  list
82
87
  end
83
-
84
- mod.template_paths.push(*inherited)
88
+
85
89
  mod.template_paths.uniq!
86
90
  end
87
91
 
@@ -122,7 +126,7 @@ module Tadpole
122
126
  def load_setup_rb(full_path, mod)
123
127
  setup_file = File.join(full_path, 'setup.rb')
124
128
  if File.file? setup_file
125
- mod.module_eval(File.read(setup_file).taint, setup_file)
129
+ mod.module_eval(File.read(setup_file).taint, setup_file, 1)
126
130
  end
127
131
  mod
128
132
  end
@@ -9,13 +9,13 @@ module Tadpole
9
9
  super
10
10
 
11
11
  erb = ERB.new(content, nil, '<>')
12
- instance_eval(<<-eof, full_path, -2)
12
+ instance_eval(<<-eof, full_path, 1)
13
13
  def render(locals = nil, &block)
14
14
  if locals
15
15
  opts = owner.options
16
16
  owner.options = owner.options.to_hash.update(locals)
17
17
  end
18
- out = owner.instance_eval #{erb.src.inspect}
18
+ out = owner.instance_eval(#{erb.src.inspect}, __FILE__, __LINE__)
19
19
  owner.options = opts if locals
20
20
  out
21
21
  end
@@ -21,7 +21,7 @@ module Tadpole
21
21
  # extensions.
22
22
  #
23
23
  # @return [String, nil] The full pathname
24
- def self.provides?(basename)
24
+ def self.provides?(object, basename)
25
25
  self.const_get("EXTENSIONS").any? do |ext|
26
26
  path = basename + ext
27
27
  return path if path_suitable?(path)
@@ -242,13 +242,13 @@ module Tadpole
242
242
  filename, provider = nil, nil
243
243
  template_paths.each do |template_path|
244
244
  SectionProviders.providers.each do |prov|
245
- filename = prov.provides?(File.join(template_path, section))
245
+ filename = prov.provides?(self, File.join(template_path, section))
246
246
  break provider = prov if filename
247
247
  end
248
248
  break if provider && filename
249
249
  end
250
250
 
251
- raise ArgumentError, "missing section `#{section}'" if !provider
251
+ raise ArgumentError, "Template(#{path}) is missing section `#{section}'" if !provider
252
252
  @providers[section] = provider.new(filename, self)
253
253
  end
254
254
 
@@ -264,7 +264,7 @@ module Tadpole
264
264
  if file = find_template(filename)
265
265
  File.read(file)
266
266
  else
267
- raise ArgumentError, "missing template file `#{filename}' in #{self.inspect}"
267
+ raise ArgumentError, "Template(#{path}) is missing template file `#{filename}'"
268
268
  end
269
269
  end
270
270
  end
data/lib/tadpole.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Tadpole
2
- Version = '0.1.3'
2
+ Version = '0.1.4'
3
3
  Root = File.dirname(__FILE__)
4
4
 
5
5
  module SectionProviders
data/spec/erb_spec.rb ADDED
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'tadpole')
2
+
3
+ describe Tadpole::SectionProviders::ERBProvider do
4
+ before do
5
+ Tadpole.template_paths.clear
6
+ Tadpole.register_template_path(File.dirname(__FILE__) + '/examples')
7
+ end
8
+
9
+ describe '#render' do
10
+ it "should setup locals through run" do
11
+ Tadpole('erb').run(:a_variable => 1).should == '1'
12
+ end
13
+
14
+ it "should setup locals through #render" do
15
+ Tadpole('erb2').run.should == '2'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ def init
2
+ super
3
+ end
@@ -0,0 +1,3 @@
1
+ def init
2
+ super
3
+ end
@@ -0,0 +1,5 @@
1
+ def init
2
+ sections :a
3
+ end
4
+
5
+ def a; 'notinfo' end
@@ -0,0 +1,3 @@
1
+ def init
2
+ sections T(:test, :extra => 'info')
3
+ end
@@ -0,0 +1,5 @@
1
+ def init
2
+ sections :a
3
+ end
4
+
5
+ def a; extra end
@@ -0,0 +1 @@
1
+ <%= a_variable %>
@@ -0,0 +1,3 @@
1
+ def init
2
+ sections :a
3
+ end
@@ -0,0 +1 @@
1
+ <%= a_variable %>
@@ -0,0 +1,7 @@
1
+ def init
2
+ sections :a
3
+ end
4
+
5
+ def a
6
+ render('_a', :a_variable => 2)
7
+ end
@@ -0,0 +1,7 @@
1
+ def init
2
+ super
3
+ sections :a, :b
4
+ end
5
+
6
+ def a; "a" end
7
+ def b; "b" end
@@ -0,0 +1,6 @@
1
+ def init
2
+ super
3
+ sections.push :d
4
+ end
5
+
6
+ def d; "d" end
@@ -0,0 +1,8 @@
1
+ inherits '../inherits_a'
2
+
3
+ def init
4
+ super
5
+ sections.push :c
6
+ end
7
+
8
+ def c; "c" end
@@ -0,0 +1 @@
1
+ inherits '/inherits/inherits_a'
@@ -0,0 +1,2 @@
1
+ inherits '../inherits_a'
2
+ inherits '../inherits_b'
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'tadpole')
2
+
3
+ describe Tadpole::LocalTemplate do
4
+ before do
5
+ Tadpole.template_paths.clear
6
+ Tadpole.register_template_path(File.dirname(__FILE__) + '/examples')
7
+ end
8
+
9
+ describe '.inherits' do
10
+ it "should inherit from a relative path" do
11
+ Tadpole('inherits/inherits_b').run.should == "abc"
12
+ end
13
+
14
+ it "should automatically inherit parent directories" do
15
+ Tadpole('inherits/inherits_b/inherits_c').run.should == "abcd"
16
+ end
17
+
18
+ it "should inherit absolute paths from top of registered template paths" do
19
+ Tadpole('inherits/inherits_d').run.should == "ab"
20
+ end
21
+
22
+ it "should order template_paths properly" do
23
+ paths = Tadpole('inherits/inherits_b/inherits_c').template_paths
24
+ paths.map! {|p| p.gsub(Tadpole.template_paths.first + '/', '') }
25
+ paths.should == ["inherits/inherits_b/inherits_c", "inherits/inherits_b", "inherits/inherits_a", "inherits"]
26
+ end
27
+
28
+ it "should accept multiple inheritances" do
29
+ paths = Tadpole('inherits/inherits_e').template_paths
30
+ paths.map! {|p| p.gsub(Tadpole.template_paths.first + '/', '') }
31
+ paths.should == ["inherits/inherits_e", "inherits/inherits_b", "inherits/inherits_a", "inherits"]
32
+ end
33
+ end
34
+
35
+ describe '.T' do
36
+ it "should refer to a template" do
37
+ Tadpole('T').run.should == 'info'
38
+ end
39
+
40
+ it "should refer to the same template path even in a subclass" do
41
+ Tadpole('T/derived').run.should == 'info'
42
+ end
43
+
44
+ it "should refer to the subclassed path if the section exists in the subclass" do
45
+ Tadpole('T/derived2').run.should == 'notinfo'
46
+ end
47
+ end
48
+ 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.3
4
+ version: 0.1.4
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-10 00:00:00 -04:00
12
+ date: 2009-07-12 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -35,9 +35,19 @@ files:
35
35
  - lib/tadpole.rb
36
36
  - spec/array_spec.rb
37
37
  - spec/basic_spec.rb
38
+ - spec/erb_spec.rb
39
+ - spec/examples/erb/a.erb
40
+ - spec/examples/erb/setup.rb
41
+ - spec/examples/erb2/_a.erb
42
+ - spec/examples/erb2/setup.rb
38
43
  - spec/examples/filters/a.txt
39
44
  - spec/examples/filters/b.txt
40
45
  - spec/examples/filters/setup.rb
46
+ - spec/examples/inherits/inherits_a/setup.rb
47
+ - spec/examples/inherits/inherits_b/inherits_c/setup.rb
48
+ - spec/examples/inherits/inherits_b/setup.rb
49
+ - spec/examples/inherits/inherits_d/setup.rb
50
+ - spec/examples/inherits/inherits_e/setup.rb
41
51
  - spec/examples/render/1/a.txt
42
52
  - spec/examples/render/1/b.txt
43
53
  - spec/examples/render/1/d.erb
@@ -48,7 +58,13 @@ files:
48
58
  - spec/examples/render/4/setup.rb
49
59
  - spec/examples/render/5/setup.rb
50
60
  - spec/examples/render/6/setup.rb
61
+ - spec/examples/T/derived/setup.rb
62
+ - spec/examples/T/derived2/setup.rb
63
+ - spec/examples/T/derived2/test/setup.rb
64
+ - spec/examples/T/setup.rb
65
+ - spec/examples/T/test/setup.rb
51
66
  - spec/filters_spec.rb
67
+ - spec/local_template_spec.rb
52
68
  - spec/render_spec.rb
53
69
  - benchmarks/eval-vs-non-eval.rb
54
70
  - benchmarks/require-vs-none.rb