tadpole 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tadpole.rb CHANGED
@@ -1,7 +1,5 @@
1
- require 'rubygems'
2
-
3
1
  module Tadpole
4
- Version = '0.1.2'
2
+ Version = '0.1.3'
5
3
  Root = File.dirname(__FILE__)
6
4
 
7
5
  module SectionProviders
data/lib/tadpole/main.rb CHANGED
@@ -33,21 +33,24 @@ module Tadpole
33
33
  end
34
34
 
35
35
  def template(*path)
36
+ path = absolutize_path(*path)
37
+ exists = find_matching_template_paths(path)
38
+ raise ArgumentError, "no such template `#{path}'" if exists.empty?
39
+
40
+ create_template(*path)
41
+ end
42
+
43
+ def create_template(*path)
36
44
  path = absolutize_path(*path)
37
45
  name = template_mod_name(path)
38
46
 
39
47
  remove_const(name) unless caching rescue NameError
40
48
  return const_get(name) rescue NameError
41
-
42
- exists = find_matching_template_paths(path)
43
- if exists.empty?
44
- raise ArgumentError, "no such template `#{path}'"
45
- end
46
-
47
- mod = create_template(path)
49
+
50
+ mod = create_template_mod(path)
48
51
  const_set(name, mod)
49
52
  end
50
-
53
+
51
54
  private
52
55
 
53
56
  def absolutize_path(*path)
@@ -57,39 +60,45 @@ module Tadpole
57
60
  File.expand_path(File.join(path))[(Dir.pwd.length+1)..-1]
58
61
  end
59
62
 
60
- def create_template(path)
61
- mod = Module.new
62
- mod.send(:include, Template)
63
- mod.path = path
64
- mod.template_paths = []
63
+ def add_template_filters(mod)
65
64
  mod.before_run_filters = LocalTemplate.before_run_filters.dup
66
65
  mod.before_section_filters = LocalTemplate.before_section_filters.dup
67
-
66
+ end
67
+
68
+ def add_inherited_templates(mod, path)
68
69
  inherited = []
69
70
  path.split(File::SEPARATOR).inject([]) do |list, el|
70
71
  list << el
71
72
  total_list = File.join(list)
72
73
  find_matching_template_paths(total_list).each do |subpath|
73
- submod = create_template_mod(subpath, total_list)
74
+ submod = create_local_template_mod(subpath, total_list)
74
75
  mod.send :include, submod
75
- #if total_list == path
76
- mod.template_paths.unshift(*submod.template_paths)
77
- mod.before_run_filters.push(*submod.before_run_filters)
78
- mod.before_section_filters.push(*submod.before_section_filters)
79
- inherited.push(*submod.inherited_paths)
80
- #mod.sections = submod.sections
81
- #end
76
+ mod.template_paths.unshift(*submod.template_paths)
77
+ mod.before_run_filters.push(*submod.before_run_filters)
78
+ mod.before_section_filters.push(*submod.before_section_filters)
79
+ inherited.push(*submod.inherited_paths)
82
80
  end
83
81
  list
84
82
  end
85
83
 
86
84
  mod.template_paths.push(*inherited)
87
85
  mod.template_paths.uniq!
86
+ end
87
+
88
+ def create_template_mod(path)
89
+ mod = Module.new
90
+ mod.send(:include, Template)
91
+ mod.path = path
92
+ mod.template_paths = []
93
+
94
+ add_template_filters(mod)
95
+ add_inherited_templates(mod, path)
96
+
88
97
  mod
89
98
  end
90
99
 
91
- def create_template_mod(full_path, path)
92
- name = 'Local'+template_mod_name(full_path)
100
+ def create_local_template_mod(full_path, path)
101
+ name = 'Local' + template_mod_name(full_path)
93
102
 
94
103
  remove_const(name) unless caching rescue NameError
95
104
  return const_get(name) rescue NameError
data/spec/basic_spec.rb CHANGED
@@ -9,6 +9,37 @@ describe Tadpole do
9
9
  Tadpole.should_receive(:template).with(:x, :y, :z)
10
10
  Tadpole(:x, :y, :z)
11
11
  end
12
+
13
+ describe '.create_template' do
14
+ it "should create a template that does not exist on disk" do
15
+ template = Tadpole.create_template('x/y/z')
16
+ template.ancestors.should include(Tadpole::Template)
17
+ template.path.should == 'x/y/z'
18
+ end
19
+
20
+ it "should inherit templates with same path" do
21
+ module Tadpole::LocalTemplate_x_y; end
22
+ File.should_receive(:directory?).with('./x').and_return(true)
23
+ File.should_receive(:directory?).with('./x/y').and_return(false)
24
+ Tadpole.register_template_path '.'
25
+ template = Tadpole.create_template('x/y')
26
+ template.ancestors.should_not include(Tadpole::LocalTemplate_x_y)
27
+ template.ancestors.should include(Tadpole::LocalTemplate_x)
28
+ end
29
+
30
+ it "should allow sections to be set programmatically" do
31
+ mod = Tadpole.create_template('mine')
32
+ template = mod.new
33
+ template.class.should == Tadpole::Template_mine
34
+ class << template
35
+ def a; 'a' end
36
+ def b; 'b' end
37
+ def c; 'c' end
38
+ end
39
+ template.sections :a, :b, :c
40
+ template.run.should == 'abc'
41
+ end
42
+ end
12
43
 
13
44
  describe '.template' do
14
45
  it "should raise ArgumentError if path does not exist in template_paths" do
@@ -51,8 +82,17 @@ describe Tadpole::Template do
51
82
  end
52
83
 
53
84
  it "should act as a class (have a .new, #inspect, etc.)" do
54
- Tadpole(:default, :html).should respond_to(:new)
55
- Tadpole::Template.should === Tadpole(:default, :html).new
85
+ File.should_receive(:directory?).with('./default/html').and_return(true)
86
+ mod = Tadpole(:default, :html)
87
+ mod.should respond_to(:new)
88
+ template = mod.new
89
+ Tadpole::Template.should === template
90
+ template.inspect.should =~ /#<Template:(\S+) path='default\/html' sections=nil>/
91
+ template.class.should == Tadpole::Template_default_html
92
+ end
93
+
94
+ it "should create two template modules for the same template in separate template paths" do
95
+
56
96
  end
57
97
 
58
98
  it "should #run with Symbol sections as methods" do
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loren Segal