slippers 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ begin
11
11
  gem.name = "slippers"
12
12
  gem.summary = "A strict templating library for Ruby"
13
13
  gem.email = "me@sarahtaraporewalla.com"
14
- gem.homepage = "http://github.com/starapor/slippers"
14
+ gem.homepage = "http://starapor.github.com/slippers"
15
15
  gem.description = "A strict templating library for ruby"
16
16
  gem.authors = ["Sarah Taraporewalla"]
17
17
  gem.files = FileList["[A-Z]*", "{bin,lib,spec,examples}/**/*"]
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 10
2
+ :patch: 11
3
3
  :major: 0
4
4
  :minor: 0
data/lib/engine/engine.rb CHANGED
@@ -3,6 +3,7 @@ Treetop.load File.dirname(__FILE__) + '/slippers'
3
3
  module Slippers
4
4
  class Engine
5
5
  DEFAULT_STRING = ''
6
+ MISSING_HANDLER = lambda { return "" }
6
7
  def initialize(template, params={})
7
8
  @main_template = Slippers::Template.new(template)
8
9
  @template_group = params[:template_group]
@@ -2,15 +2,15 @@ module Slippers
2
2
  module AttributeToRenderNode
3
3
 
4
4
  def eval(object_to_render, template_group)
5
- [object_to_render].flatten.inject('') { |rendered, item| rendered + render(value_of(item), template_group) }
5
+ [object_to_render].flatten.inject('') { |rendered, item| rendered + render(value_of(item, template_group), template_group) }
6
6
  end
7
7
 
8
- def value_of(item)
9
- return '' if attribute == ''
8
+ def value_of(item, template_group)
9
+ return default_string(template_group) if attribute == ''
10
10
  return item.to_s if attribute == 'it'
11
11
  return item[to_sym] if item.respond_to?('[]'.to_sym) && item[to_sym]
12
12
  return item.send(attribute) if item.respond_to?(attribute)
13
- Slippers::Engine::DEFAULT_STRING
13
+ default_string(template_group)
14
14
  end
15
15
 
16
16
  def render(object_to_render, template_group)
@@ -38,6 +38,12 @@ module Slippers
38
38
  def attribute
39
39
  text_value
40
40
  end
41
+
42
+ private
43
+ def default_string(template_group)
44
+ return Slippers::Engine::DEFAULT_STRING unless template_group
45
+ template_group.default_string
46
+ end
41
47
  end
42
48
 
43
49
  class AttributeWithExpressionOptionNode < Treetop::Runtime::SyntaxNode
@@ -57,11 +63,17 @@ module Slippers
57
63
  end
58
64
 
59
65
  def apply_attribute_to_subtemplate(item, template_group)
60
- return '' unless template_group
66
+ return invoke_misisng_handler unless template_group
61
67
  subtemplate = template_group.find(template_path.to_s)
62
- return '' unless (subtemplate && subtemplate.respond_to?('render'))
68
+ return invoke_misisng_handler(template_group.missing_handler) unless (subtemplate && subtemplate.respond_to?('render'))
63
69
  subtemplate.render(item)
64
70
  end
71
+
72
+ private
73
+ def invoke_misisng_handler(missing_handler=Slippers::Engine::MISSING_HANDLER)
74
+ return missing_handler.call(template_path.to_s) if missing_handler.arity == 1
75
+ missing_handler.call
76
+ end
65
77
  end
66
78
 
67
79
  class AnonymousTemplateNode < Treetop::Runtime::SyntaxNode
@@ -82,7 +94,7 @@ module Slippers
82
94
  end
83
95
 
84
96
  def find_attribute_and_render(item, template_group)
85
- object_to_render = attribute.value_of(item)
97
+ object_to_render = attribute.value_of(item, template_group)
86
98
  [object_to_render].flatten.inject('') { |rendered, i| rendered + template.apply_attribute_to_subtemplate(i, template_group).to_s }
87
99
  end
88
100
  end
@@ -90,15 +102,19 @@ module Slippers
90
102
  class ConditionalTemplateNode < Treetop::Runtime::SyntaxNode
91
103
 
92
104
  def eval(object_to_render, template_group)
93
- attribute = if_clause.value_of(object_to_render)
94
- if (attribute && attribute != Slippers::Engine::DEFAULT_STRING) then
105
+ attribute = if_clause.value_of(object_to_render, template_group)
106
+ if (attribute && attribute != default_string(template_group)) then
95
107
  if_expression.eval(object_to_render, template_group)
96
108
  else
97
- if else_clause.elements then else_clause.else_expression.eval(object_to_render, template_group) else Slippers::Engine::DEFAULT_STRING end
109
+ if else_clause.elements then else_clause.else_expression.eval(object_to_render, template_group) else default_string(template_group) end
98
110
  end
99
111
  end
100
112
 
101
-
113
+ private
114
+ def default_string(template_group)
115
+ return Slippers::Engine::DEFAULT_STRING unless template_group
116
+ template_group.default_string
117
+ end
102
118
  end
103
119
 
104
120
  class TemplatedExpressionNode < Treetop::Runtime::SyntaxNode
@@ -3,8 +3,12 @@ module Slippers
3
3
  def initialize(params={})
4
4
  @templates = params[:templates]
5
5
  @super_group = params[:super_group]
6
+ @missing_handler = params[:missing_template_handler] || Slippers::Engine::MISSING_HANDLER
7
+ @default_string = params[:default_string] || Slippers::Engine::DEFAULT_STRING
6
8
  end
7
9
 
10
+ attr_reader :missing_handler, :default_string
11
+
8
12
  def find(subtemplate)
9
13
  return nil unless @templates
10
14
  return create_template(subtemplate.to_sym) if @templates.include?(subtemplate.to_sym)
@@ -3,8 +3,11 @@ module Slippers
3
3
  def initialize(directory_paths, params={})
4
4
  @directory_paths = directory_paths
5
5
  @super_group = params[:super_group]
6
+ @missing_handler = params[:missing_template_handler] || Slippers::Engine::MISSING_HANDLER
7
+ @default_string = params[:default_string] || Slippers::Engine::DEFAULT_STRING
6
8
  end
7
- attr_reader :directory_paths
9
+
10
+ attr_reader :directory_paths, :missing_handler, :default_string
8
11
 
9
12
  def find(subtemplate)
10
13
  file_name = @directory_paths.map { |directory_path| directory_path + '/' + subtemplate + '.st' }.find { |f| File.exist? f}
data/spec/engine.rb CHANGED
@@ -79,6 +79,13 @@ describe Slippers::Engine do
79
79
  engine = Slippers::Engine.new('$this_is_bad')
80
80
  engine.render(stub('object')).should eql('')
81
81
  end
82
+
83
+ it "should handle missing handlers" do
84
+ template = "This is a string without any holes in it"
85
+ engine = Slippers::Engine.new(template, :missing_template_handler => nil, :default_string => nil)
86
+ engine.render.should eql("This is a string without any holes in it")
87
+ end
88
+
82
89
  end
83
90
 
84
91
  class AgeRenderer
@@ -38,13 +38,16 @@ 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 render the default string when the attribute cannot be found on the object to render" do
41
+ it "should render the default string when the attribute cannot be found on the object to render and there is no template group" do
42
42
  Slippers::Engine::DEFAULT_STRING.should eql('')
43
43
  @parser.parse("This is the $adjective$ template with $message$.").eval(OpenStruct.new).should eql("This is the template with .")
44
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')
47
- Slippers::Engine::DEFAULT_STRING = ""
45
+ end
46
+
47
+ it "should render the default string of the template group when the attribute cannot be found on the object to render" do
48
+ template_group = Slippers::TemplateGroup.new(:default_string => "foo" )
49
+ template_group.default_string.should eql('foo')
50
+ @parser.parse("$not_me$").eval(:object, template_group).should eql('foo')
48
51
  end
49
52
 
50
53
  it "should convert attribute to string" do
@@ -49,11 +49,19 @@ describe SlippersParser do
49
49
  @parser.parse('$people:person()$').eval(object_to_render, template_group).should eql("this is fred flinstone this is barney rubble ")
50
50
  end
51
51
 
52
- it "should render an empty string when the subtemplate cannot be found" do
52
+ it "should call the default missing handler when the subtemplate cannot be found and there is no template group" do
53
+ Slippers::Engine::MISSING_HANDLER.call.should eql('')
53
54
  @parser.parse("This is the unknown template $unknown()$!").eval(Person.new('fred', 'flinstone')).should eql("This is the unknown template !")
54
55
  @parser.parse("This is the unknown template $first:unknown()$!").eval(Person.new('fred', 'flinstone')).should eql("This is the unknown template !")
55
56
  end
56
57
 
58
+ it "should call the missing handler when the subtemplate cannot be found" do
59
+ missing_handler = lambda { |template| "Warning: the template [#{template}] is missing" }
60
+ template_group = Slippers::TemplateGroup.new(:missing_template_handler => missing_handler)
61
+ @parser.parse("This is the unknown template $unknown()$!").eval(:object, template_group).should eql("This is the unknown template Warning: the template [unknown] is missing!")
62
+ @parser.parse("This is the unknown template $first:unknown()$!").eval(Person.new('fred', 'flinstone'), template_group).should eql("This is the unknown template Warning: the template [unknown] is missing!")
63
+ end
64
+
57
65
  it "should parse the file template from the template group" do
58
66
  template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
59
67
  name = OpenStruct.new({:first => 'fred', :last => 'flinestone'})
@@ -2,12 +2,12 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe Slippers::TemplateGroup do
4
4
  it 'should find the right template' do
5
- subtemplate = Slippers::Engine.new('Hello $first$ $last$')
6
- template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
7
- template_group.find(:person).should eql(subtemplate)
8
- template_group.find('person').should eql(subtemplate)
9
- end
10
-
5
+ subtemplate = Slippers::Engine.new('Hello $first$ $last$')
6
+ template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
7
+ template_group.find(:person).should eql(subtemplate)
8
+ template_group.find('person').should eql(subtemplate)
9
+ end
10
+
11
11
  it 'should wrap a template string in the engine if it is not one' do
12
12
  subtemplate = Slippers::Engine.new('Hello $first$ $last$')
13
13
  template_group = Slippers::TemplateGroup.new(:templates => {:person => 'Hello $first$ $last$'})
@@ -48,5 +48,25 @@ describe Slippers::TemplateGroup do
48
48
  template_group.has_registered?(date.class).should be_false
49
49
  template_group.render(date).should eql('')
50
50
  end
51
-
51
+
52
+ it 'missing handler should be the provided handler' do
53
+ missing_handler = Proc.new{ |foo| foo.to_s }
54
+ template_group = Slippers::TemplateGroup.new(:missing_template_handler => missing_handler)
55
+ template_group.missing_handler.should eql(missing_handler)
56
+ end
57
+
58
+ it 'missing handler should be the default handler when none is provided' do
59
+ template_group = Slippers::TemplateGroup.new()
60
+ template_group.missing_handler.should eql(Slippers::Engine::MISSING_HANDLER)
61
+ end
62
+
63
+ it 'default string should be the provided default' do
64
+ template_group = Slippers::TemplateGroup.new(:default_string => "Hello Mum")
65
+ template_group.default_string.should eql("Hello Mum")
66
+ end
67
+
68
+ it 'missing handler should be the default handler when none is provided' do
69
+ template_group = Slippers::TemplateGroup.new()
70
+ template_group.default_string.should eql(Slippers::Engine::DEFAULT_STRING)
71
+ end
52
72
  end
@@ -39,5 +39,32 @@ describe Slippers::TemplateGroupDirectory do
39
39
  template_group.find('person/age').should eql(Slippers::Engine.new('The age for him is $age$', :template_group => template_group))
40
40
  end
41
41
 
42
+ it 'should accept missing handlers' do
43
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'], :missing_template_handler => nil, :default_string => nil)
44
+ template_group.find('index').should eql(Slippers::Engine.new('Hey foo', :template_group => template_group))
45
+ template_group.find('person/age').should eql(Slippers::Engine.new('The age for him is $age$', :template_group => template_group))
46
+ end
47
+
48
+ it 'missing handler should be the provided handler' do
49
+ missing_handler = Proc.new{ |foo| foo.to_s }
50
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'], :missing_template_handler => missing_handler)
51
+ template_group.missing_handler.should eql(missing_handler)
52
+ end
53
+
54
+ it 'missing handler should be the default handler when none is provided' do
55
+ template_group = template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
56
+ template_group.missing_handler.should eql(Slippers::Engine::MISSING_HANDLER)
57
+ end
58
+
59
+ it 'default string should be the provided default' do
60
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'], :default_string => "Hello Mum")
61
+ template_group.default_string.should eql("Hello Mum")
62
+ end
63
+
64
+ it 'missing handler should be the default handler when none is provided' do
65
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
66
+ template_group.default_string.should eql(Slippers::Engine::DEFAULT_STRING)
67
+ end
68
+
42
69
 
43
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slippers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
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-29 00:00:00 +01:00
12
+ date: 2009-09-30 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -145,7 +145,7 @@ files:
145
145
  - spec/views/person/date_renderer.rb
146
146
  - spec/views/person/name.st
147
147
  has_rdoc: true
148
- homepage: http://github.com/starapor/slippers
148
+ homepage: http://starapor.github.com/slippers
149
149
  licenses: []
150
150
 
151
151
  post_install_message: