starapor-slippers 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -48,3 +48,5 @@ Examples
48
48
  engine = Slippers::Engine.new("Introducing $name:name()$ who is $dob$.", :template_group => template_group)
49
49
  engine.render(person) #=> "Introducing Fred Flinstone who is 34 years old."
50
50
 
51
+
52
+
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 8
2
+ :patch: 9
3
3
  :major: 0
4
4
  :minor: 0
Binary file
@@ -2,7 +2,7 @@ Treetop.load File.dirname(__FILE__) + '/slippers'
2
2
 
3
3
  module Slippers
4
4
  class Engine
5
-
5
+ DEFAULT_STRING = ''
6
6
  def initialize(template, params={})
7
7
  @main_template = Slippers::Template.new(template)
8
8
  @template_group = params[:template_group]
@@ -7,9 +7,10 @@ module Slippers
7
7
 
8
8
  def value_of(item)
9
9
  return '' if to_s == ''
10
+ return item.to_s if text_value == 'it'
10
11
  return item[to_sym] if item.respond_to?('[]'.to_sym) && item[to_sym]
11
12
  return item.send(to_s) if item.respond_to?(to_s)
12
- ''
13
+ Slippers::Engine::DEFAULT_STRING
13
14
  end
14
15
 
15
16
  def render(object_to_render, template_group)
@@ -7,7 +7,7 @@ module Slippers
7
7
 
8
8
  def find(subtemplate)
9
9
  return nil unless @templates
10
- return @templates[subtemplate.to_sym] if @templates.include?(subtemplate.to_sym)
10
+ return create_template(subtemplate.to_sym) if @templates.include?(subtemplate.to_sym)
11
11
  find_in_super_group(subtemplate)
12
12
  end
13
13
 
@@ -30,5 +30,11 @@ module Slippers
30
30
  return nil unless @super_group
31
31
  @super_group.find(subtemplate)
32
32
  end
33
+
34
+ def create_template(subtemplate)
35
+ template = @templates[subtemplate]
36
+ return template unless template.is_a?(String)
37
+ Slippers::Engine.new(template)
38
+ end
33
39
  end
34
40
  end
@@ -1,17 +1,17 @@
1
1
  module Slippers
2
2
  class TemplateGroupDirectory < TemplateGroup
3
- def initialize(directory_path, params={})
4
- @directory_path = directory_path
3
+ def initialize(directory_paths, params={})
4
+ @directory_paths = directory_paths
5
5
  @super_group = params[:super_group]
6
6
  end
7
- attr_reader :directory_path
7
+ attr_reader :directory_paths
8
8
 
9
9
  def find(subtemplate)
10
- file_name = @directory_path + '/' + subtemplate + '.st'
11
- return find_renderer(subtemplate) unless File.exist?(file_name)
12
- Engine.new(FileTemplate.new(file_name).template, :template_group => self)
13
-
10
+ file_name = @directory_paths.map { |directory_path| directory_path + '/' + subtemplate + '.st' }.find { |f| File.exist? f}
11
+ return Engine.new(FileTemplate.new(file_name).template, :template_group => self) if file_name
12
+ find_renderer_or_supergroup(subtemplate)
14
13
  end
14
+
15
15
  def has_registered?(class_name)
16
16
  return false unless @super_group
17
17
  @super_group.has_registered?(class_name)
@@ -25,16 +25,17 @@ module Slippers
25
25
 
26
26
  def eql?(other)
27
27
  return false unless other
28
- directory_path.eql?(other.directory_path)
28
+ directory_paths.eql?(other.directory_paths)
29
29
  end
30
30
  def hash
31
- @directory_path.hash
31
+ @directory_paths.hash
32
32
  end
33
33
 
34
34
  private
35
- def find_renderer(subtemplate)
36
- file_name = @directory_path + '/' + subtemplate + '.rb'
37
- return find_in_super_group(subtemplate) unless File.exist?(file_name)
35
+ def find_renderer_or_supergroup(subtemplate)
36
+ file_name = @directory_paths.map { |directory_path| directory_path + '/' + subtemplate + '.rb' }.find { |f| File.exist? f}
37
+ return find_in_super_group(subtemplate) unless file_name
38
+
38
39
  renderer_name = subtemplate.split('/')[-1]
39
40
  load File.expand_path(file_name)
40
41
  create_renderer renderer_name
@@ -13,9 +13,9 @@ module Ramaze
13
13
  private
14
14
  def self.template_group(action)
15
15
  subtemplates = action.instance.ancestral_trait[:slippers_options] || {}
16
- view_root = "#{action.instance.options[:roots]}/#{action.instance.options[:views]}"
16
+ views = action.instance.options[:views].map{|view| "#{action.instance.options[:roots]}/#{view}" }
17
17
  super_group = ::Slippers::TemplateGroup.new(:templates => subtemplates)
18
- ::Slippers::TemplateGroupDirectory.new(view_root, :super_group => super_group)
18
+ ::Slippers::TemplateGroupDirectory.new(views, :super_group => super_group)
19
19
  end
20
20
  end
21
21
  end
@@ -38,8 +38,12 @@ describe SlippersParser do
38
38
  @parser.parse('this is $name$').eval(people).should eql("this is fredbarney")
39
39
  end
40
40
 
41
- it "should substitute in an empty string when the attribute cannot be found" do
41
+ it "should render the default string when the attribute cannot be found on the object to render" do
42
+ Slippers::Engine::DEFAULT_STRING.should eql('')
42
43
  @parser.parse("This is the $adjective$ template with $message$.").eval(OpenStruct.new).should eql("This is the template with .")
44
+ @parser.parse("$not_me$").eval(:object).should eql('')
45
+ Slippers::Engine::DEFAULT_STRING = "foo"
46
+ @parser.parse("$not_me$").eval(:object).should eql('foo')
43
47
  end
44
48
 
45
49
  it "should convert attribute to string" do
@@ -63,12 +67,7 @@ describe SlippersParser do
63
67
  it 'should return an empty string if the template is not correctly formed' do
64
68
  @parser.parse("$not_properly_formed").should eql(nil)
65
69
  end
66
-
67
- it 'should render an empty string if it cannot find the attribute to render' do
68
- @parser.parse("$not_me$").eval(:object).should eql('')
69
- end
70
-
71
-
70
+
72
71
  end
73
72
 
74
73
 
@@ -55,14 +55,19 @@ describe SlippersParser do
55
55
  end
56
56
 
57
57
  it "should parse the file template from the template group" do
58
- template_group = Slippers::TemplateGroupDirectory.new('spec/views')
58
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
59
59
  name = OpenStruct.new({:first => 'fred', :last => 'flinestone'})
60
60
  people = OpenStruct.new({:fred => name})
61
61
  @parser.parse("should parse $person/name()$").eval(name, template_group).should eql("should parse fred flinestone")
62
62
  @parser.parse("should parse $fred:person/name()$").eval(people, template_group).should eql("should parse fred flinestone")
63
63
  end
64
-
65
64
 
65
+ it 'should render the object if the keyword it is used' do
66
+ supergroup = Slippers::TemplateGroup.new(:templates => {:bold => Slippers::Engine.new("<b>$it$</b>")})
67
+ subgroup = Slippers::TemplateGroup.new(:templates => {}, :super_group => supergroup)
68
+ @parser.parse("<b>$it$</b>").eval("Sarah", subgroup).should eql('<b>Sarah</b>')
69
+ @parser.parse("$name:bold()$").eval({:name => "Sarah"}, subgroup).should eql('<b>Sarah</b>')
70
+ end
66
71
  end
67
72
 
68
73
 
@@ -6,6 +6,13 @@ describe Slippers::TemplateGroup do
6
6
  template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
7
7
  template_group.find(:person).should eql(subtemplate)
8
8
  template_group.find('person').should eql(subtemplate)
9
+ end
10
+
11
+ it 'should wrap a template string in the engine if it is not one' do
12
+ subtemplate = Slippers::Engine.new('Hello $first$ $last$')
13
+ template_group = Slippers::TemplateGroup.new(:templates => {:person => 'Hello $first$ $last$'})
14
+ template_group.find(:person).should eql(subtemplate)
15
+ template_group.find('person').should eql(subtemplate)
9
16
  end
10
17
 
11
18
  it 'should return nil if it cannot find the right template' do
@@ -15,7 +22,7 @@ describe Slippers::TemplateGroup do
15
22
  end
16
23
 
17
24
  it 'should look in the super template group if it cannot find the template' do
18
- template = stub 'template'
25
+ template = Slippers::Engine.new('Hello $first$ $last$')
19
26
  super_template_group = Slippers::TemplateGroup.new(:templates => {:person => template})
20
27
  template_group = Slippers::TemplateGroup.new(:templates => {}, :super_group => super_template_group)
21
28
  template_group.find(:person).should eql(template)
@@ -41,5 +48,5 @@ describe Slippers::TemplateGroup do
41
48
  template_group.has_registered?(date.class).should be_false
42
49
  template_group.render(date).should eql('')
43
50
  end
44
-
51
+
45
52
  end
@@ -3,24 +3,24 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  describe Slippers::TemplateGroupDirectory do
4
4
 
5
5
  it 'should find the file in the directory folder' do
6
- template_group = Slippers::TemplateGroupDirectory.new('spec/views')
6
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
7
7
  template_group.find('index').should eql(Slippers::Engine.new('Hey foo', :template_group => template_group))
8
8
  template_group.find('person/age').should eql(Slippers::Engine.new('The age for him is $age$', :template_group => template_group))
9
9
  end
10
10
 
11
11
  it 'should return nil if it cannot find the file in the directory folder' do
12
- template_group = Slippers::TemplateGroupDirectory.new('spec/views')
12
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
13
13
  template_group.find('person/not_found').should eql(nil)
14
14
  end
15
15
 
16
16
  it 'should read the st template file and return a new slipers engine for it' do
17
- template_group = Slippers::TemplateGroupDirectory.new('spec/views')
17
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
18
18
  template_group.find('index').should eql(Slippers::Engine.new('Hey foo', :template_group => template_group))
19
19
  template_group.find('person/age').should eql(Slippers::Engine.new('The age for him is $age$', :template_group => template_group))
20
20
  end
21
21
 
22
22
  it 'should load the ruby file found in the directory folder' do
23
- template_group = Slippers::TemplateGroupDirectory.new('spec/views')
23
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
24
24
  template_group.find('person/date_renderer').class.should eql(DateRenderer)
25
25
  template_group.find('money').class.should eql(Money)
26
26
  end
@@ -28,9 +28,16 @@ describe Slippers::TemplateGroupDirectory do
28
28
  it 'should look in the super template group if it cannot find the template' do
29
29
  template = stub 'template'
30
30
  super_template_group = Slippers::TemplateGroup.new(:templates => {:person => template})
31
- template_group = Slippers::TemplateGroupDirectory.new('spec/views', :super_group => super_template_group)
31
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'], :super_group => super_template_group)
32
32
  template_group.find('person').should eql(template)
33
33
  template_group.find('not_this').should eql(nil)
34
34
  end
35
+
36
+ it 'should look in all the directories provided for the template' do
37
+ template_group = Slippers::TemplateGroupDirectory.new(['examples/blog', 'spec/views'])
38
+ template_group.find('index').should eql(Slippers::Engine.new('Hey foo', :template_group => template_group))
39
+ template_group.find('person/age').should eql(Slippers::Engine.new('The age for him is $age$', :template_group => template_group))
40
+ end
41
+
35
42
 
36
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starapor-slippers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sarah Taraporewalla
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-25 00:00:00 -07:00
12
+ date: 2009-09-27 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency