starapor-slippers 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README +10 -3
- data/Rakefile +4 -39
- data/VERSION.yml +1 -1
- data/lib/engine/binding_wrapper.rb +0 -4
- data/lib/engine/slippers_nodes.rb +1 -15
- data/spec/engine.rb +1 -1
- data/spec/parse_attributes.rb +74 -0
- data/spec/parse_renderers.rb +35 -0
- data/spec/parse_templates.rb +68 -0
- data/spec/template_group_directory.rb +0 -2
- metadata +5 -4
- data/spec/parser.rb +0 -134
- data/spec/slippers_spec.rb +0 -5
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
There are many template engines that you can choose for the generation of views in your mvc application. The problem with most of the them, however, is that they are too permissive. These turing-complete engines allow for many complex constructs within the template, which begin at simple if
|
1
|
+
There are many template engines that you can choose for the generation of views in your mvc application. The problem with most of the them, however, is that they are too permissive. These turing-complete engines allow for many complex constructs within the template, which begin at simple if statements and for loops, and expand to complex object traversal. While these permissive languages are intended to offer great flexibility, in reality they promote bad practices. Allowing logic to permeate your view is bad for many reasons: firstly, the code in views is rarely tested; secondly, the separation between the models and the view blurs and business logic creeps into the view.
|
2
2
|
|
3
3
|
All we want our template engine to do is read a string which has holes in it, and replace those holes with the desired string, much like mail merge. String Template is a template engine originally for Java but now ported to C# and python, which enforces strict separation of model and view by only supporting these strings with holes. Unfortunately, it has not been ported to ruby...until now.
|
4
4
|
|
@@ -22,14 +22,14 @@ Examples
|
|
22
22
|
engine = Slippers::Engine.new(template, :template_group => template_group)
|
23
23
|
engine.render #=> "This is a template and then this is a subtemplate"
|
24
24
|
|
25
|
-
4. Applying
|
25
|
+
4. Applying an object to a subtemplate
|
26
26
|
subtemplate = Slippers::Template.new("this is a subtemplate with a message of $saying$")
|
27
27
|
template_group = Slippers::TemplateGroup.new(:templates => {:message_subtemplate => subtemplate})
|
28
28
|
template = "This is a template and then $message:message_subtemplate()$!"
|
29
29
|
engine = Slippers::Engine.new(template, :template_group => template_group)
|
30
30
|
engine.render(:message => {:saying => 'hello world'}) #=> "This is a template and then this is a subtemplate with a message of hello world!"
|
31
31
|
|
32
|
-
5. Applying
|
32
|
+
5. Applying an object to an anonymous subtemplate
|
33
33
|
template = "This is a template and then $message:{this is a subtemplate with a message of $saying$}$!"
|
34
34
|
engine = Slippers::Engine.new(template)
|
35
35
|
engine.render(:message => {:saying => 'hello world'}) #=> "This is a template and then this is a subtemplate with a message of hello world!"
|
@@ -41,3 +41,10 @@ Examples
|
|
41
41
|
template_group = Slippers::TemplateGroup.new(:templates => {:name => subtemplate, :age => age_renderer})
|
42
42
|
engine = Slippers::Engine.new("Introducing $name:name()$ who is $dob:age()$.", :template_group => template_group)
|
43
43
|
engine.render(person) #=> "Introducing Fred Flinstone who is 34 years old."
|
44
|
+
|
45
|
+
7. Select a renderer based on the type of the object to render
|
46
|
+
person = OpenStruct.new({:name => {:first => 'Fred', :last => 'Flinstone'}, :dob => Date.new(DateTime.now.year-34, 2, 4)})
|
47
|
+
template_group = Slippers::TemplateGroup.new(:templates => {:name => Slippers::Engine.new('$first$ $last$'), Date => AgeRenderer.new})
|
48
|
+
engine = Slippers::Engine.new("Introducing $name:name()$ who is $dob$.", :template_group => template_group)
|
49
|
+
engine.render(person) #=> "Introducing Fred Flinstone who is 34 years old."
|
50
|
+
|
data/Rakefile
CHANGED
@@ -1,17 +1,9 @@
|
|
1
|
+
load File.expand_path(File.dirname(__FILE__) + "/tasks/spec.rake")
|
2
|
+
#load File.expand_path(File.dirname(__FILE__) + "/tasks/git.rake")
|
3
|
+
|
1
4
|
require 'rake'
|
2
5
|
require 'spec/rake/spectask'
|
3
6
|
|
4
|
-
desc "Run all examples"
|
5
|
-
Spec::Rake::SpecTask.new('examples') do |t|
|
6
|
-
t.spec_files = FileList['spec/**/*.rb']
|
7
|
-
end
|
8
|
-
|
9
|
-
desc "Generate HTML report for failing examples"
|
10
|
-
Spec::Rake::SpecTask.new('failing_examples_with_html') do |t|
|
11
|
-
t.spec_files = FileList['failing_examples/**/*.rb']
|
12
|
-
t.spec_opts = ["--format", "html:doc/reports/tools/failing_examples.html", "--diff"]
|
13
|
-
t.fail_on_error = false
|
14
|
-
end
|
15
7
|
|
16
8
|
desc "Generate the gem using technicalpickles jeweler"
|
17
9
|
begin
|
@@ -31,32 +23,5 @@ rescue LoadError
|
|
31
23
|
puts "Slippers, or one of its dependencies, is not available. Install it with: sudo gem install starapor-slippers -s http://gems.github.com"
|
32
24
|
end
|
33
25
|
|
34
|
-
|
35
|
-
Rake::RDocTask.new do |rdoc|
|
36
|
-
rdoc.rdoc_dir = 'rdoc'
|
37
|
-
rdoc.title = 'slippers'
|
38
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
39
|
-
rdoc.rdoc_files.include('README*')
|
40
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
41
|
-
end
|
42
|
-
|
43
|
-
require 'spec/rake/spectask'
|
44
|
-
Spec::Rake::SpecTask.new(:spec) do |t|
|
45
|
-
t.libs << 'lib' << 'spec'
|
46
|
-
t.spec_files = FileList['spec/**/*.rb']
|
47
|
-
end
|
48
|
-
|
49
|
-
Spec::Rake::SpecTask.new(:rcov) do |t|
|
50
|
-
t.libs << 'lib' << 'spec'
|
51
|
-
t.spec_files = FileList['spec/**/*_spec.rb']
|
52
|
-
t.rcov = true
|
53
|
-
end
|
54
|
-
|
55
|
-
begin
|
56
|
-
require 'cucumber/rake/task'
|
57
|
-
Cucumber::Rake::Task.new(:features)
|
58
|
-
rescue LoadError
|
59
|
-
puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
60
|
-
end
|
26
|
+
task :default => "spec:run"
|
61
27
|
|
62
|
-
task :default => :spec
|
data/VERSION.yml
CHANGED
@@ -7,8 +7,8 @@ module Slippers
|
|
7
7
|
|
8
8
|
def value_of(item)
|
9
9
|
return '' if to_s == ''
|
10
|
-
return item.send(to_s) if item.respond_to?(to_s)
|
11
10
|
return item[to_sym] if item.respond_to?('[]'.to_sym) && item[to_sym]
|
11
|
+
return item.send(to_s) if item.respond_to?(to_s)
|
12
12
|
''
|
13
13
|
end
|
14
14
|
|
@@ -39,10 +39,6 @@ module Slippers
|
|
39
39
|
return '' unless (subtemplate && subtemplate.respond_to?('render'))
|
40
40
|
subtemplate.render(item)
|
41
41
|
end
|
42
|
-
|
43
|
-
def to_s
|
44
|
-
text_value
|
45
|
-
end
|
46
42
|
end
|
47
43
|
|
48
44
|
class AnonymousTemplateNode < Treetop::Runtime::SyntaxNode
|
@@ -54,10 +50,6 @@ module Slippers
|
|
54
50
|
def apply_attribute_to_subtemplate(item, template_group)
|
55
51
|
SlippersParser.new.parse(anonymous_template_words.to_s).eval(item, template_group)
|
56
52
|
end
|
57
|
-
|
58
|
-
def to_s
|
59
|
-
text_value
|
60
|
-
end
|
61
53
|
end
|
62
54
|
|
63
55
|
class ApplyAttributeToTemplateNode < Treetop::Runtime::SyntaxNode
|
@@ -70,12 +62,6 @@ module Slippers
|
|
70
62
|
object_to_render = attribute.value_of(item)
|
71
63
|
[object_to_render].flatten.inject('') { |rendered, i| rendered + template.apply_attribute_to_subtemplate(i, template_group).to_s }
|
72
64
|
end
|
73
|
-
|
74
|
-
|
75
|
-
def to_s
|
76
|
-
text_value
|
77
|
-
end
|
78
|
-
|
79
65
|
end
|
80
66
|
|
81
67
|
class TemplatedExpressionNode < Treetop::Runtime::SyntaxNode
|
data/spec/engine.rb
CHANGED
@@ -43,7 +43,7 @@ describe Slippers::Engine do
|
|
43
43
|
it "should render a subtemplate using different rendering technologies" do
|
44
44
|
age_renderer = AgeRenderer.new
|
45
45
|
subtemplate = Slippers::Engine.new('$first$ $last$')
|
46
|
-
person = OpenStruct.new({:name => {:
|
46
|
+
person = OpenStruct.new({:name => {:last => 'Flinstone', :first => 'Fred'}, :dob => Date.new(DateTime.now.year - 34, 2, 4)})
|
47
47
|
template_group = Slippers::TemplateGroup.new(:templates => {:name => subtemplate, :age => age_renderer})
|
48
48
|
engine = Slippers::Engine.new("Introducing $name:name()$ who is $dob:age()$.", :template_group => template_group)
|
49
49
|
engine.render(person).should eql("Introducing Fred Flinstone who is 34 years old.")
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe SlippersParser do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@parser = SlippersParser.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return the string unparsed when there are no keywords in it" do
|
10
|
+
@parser.parse('').eval(nil).should eql('')
|
11
|
+
@parser.parse(' ').eval(nil).should eql(' ')
|
12
|
+
@parser.parse('this should be returned unchanged').eval.should eql('this should be returned unchanged')
|
13
|
+
@parser.parse(' this should be returned unchanged ').eval.should eql(' this should be returned unchanged ')
|
14
|
+
@parser.parse('this should be 1234567890 ').eval.should eql('this should be 1234567890 ')
|
15
|
+
@parser.parse('this should be abc1234567890 ').eval.should eql('this should be abc1234567890 ')
|
16
|
+
@parser.parse('this should be !@£%^&*()').eval.should eql('this should be !@£%^&*()')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should find the keyword within the delimiters' do
|
20
|
+
message = OpenStruct.new({:message => 'the message', :message2 => 'the second message', :name => 'fred', :full_name => 'fred flinstone'})
|
21
|
+
@parser.parse('$message$').eval(message).should eql('the message')
|
22
|
+
@parser.parse('$message$ for $name$').eval(message).should eql('the message for fred')
|
23
|
+
@parser.parse('we want to find $message$').eval(message).should eql('we want to find the message')
|
24
|
+
@parser.parse('$message$ has spoken').eval(message).should eql('the message has spoken')
|
25
|
+
@parser.parse('Yes! $message$ has spoken').eval(message).should eql('Yes! the message has spoken')
|
26
|
+
@parser.parse('Yes! $full_name$ has spoken').eval(message).should eql('Yes! fred flinstone has spoken')
|
27
|
+
@parser.parse('Yes! $message2$ has spoken').eval(message).should eql('Yes! the second message has spoken')
|
28
|
+
@parser.parse('Yes! "$message2$" has spoken').eval(message).should eql('Yes! "the second message" has spoken')
|
29
|
+
@parser.parse('$$').eval(message).should eql('')
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should not match on escaped delimiters' do
|
33
|
+
@parser.parse('stuff \$notmatched\$').eval(stub(:nothing)).should eql('stuff \$notmatched\$')
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should render a list of objects" do
|
37
|
+
people = [OpenStruct.new({:name => 'fred'}), OpenStruct.new({:name => 'barney'}) ]
|
38
|
+
@parser.parse('this is $name$').eval(people).should eql("this is fredbarney")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should substitute in an empty string when the attribute cannot be found" do
|
42
|
+
@parser.parse("This is the $adjective$ template with $message$.").eval(OpenStruct.new).should eql("This is the template with .")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should convert attribute to string" do
|
46
|
+
fred = OpenStruct.new({:name => 'fred', :dob => DateTime.new(1983, 1, 2)})
|
47
|
+
template_group = Slippers::TemplateGroup.new(:templates => {:date => Slippers::Engine.new('$year$')} )
|
48
|
+
@parser.parse("This is $name$ who was born in $dob:date()$").eval(fred, template_group).should eql('This is fred who was born in 1983')
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should render a hash" do
|
52
|
+
hash_object = {:title => 'Domain driven design', :author => 'Eric Evans', :find => 'method on a hash'}
|
53
|
+
@parser.parse("should parse $title$ by $author$").eval(hash_object).should eql("should parse Domain driven design by Eric Evans")
|
54
|
+
@parser.parse("should parse a symbol before a $find$").eval(hash_object).should eql('should parse a symbol before a method on a hash')
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should render a symbol on a hash before its methods" do
|
58
|
+
hash_object = {:find => 'method on a hash'}
|
59
|
+
@parser.parse("should parse a symbol before a $find$").eval(hash_object).should eql('should parse a symbol before a method on a hash')
|
60
|
+
@parser.parse("should still render the method $size$").eval(hash_object).should eql('should still render the method 1')
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should return an empty string if the template is not correctly formed' do
|
64
|
+
@parser.parse("$not_properly_formed").should eql(nil)
|
65
|
+
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
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
end
|
5
|
+
class FooRenderer
|
6
|
+
def render(object)
|
7
|
+
"foo renderer"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
describe SlippersParser do
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
@parser = SlippersParser.new
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return an empty string if the subtemplate does not respond to render' do
|
17
|
+
template_group = Slippers::TemplateGroup.new(:templates => {:not_a_renderer => stub('renderer')})
|
18
|
+
@parser.parse("$not_a_renderer()$").eval(stub('object'), template_group).should eql('')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should find a renderer based on the type of the object to render' do
|
22
|
+
foo = Foo.new
|
23
|
+
template_group = Slippers::TemplateGroup.new(:templates => {:foo => FooRenderer.new})
|
24
|
+
@parser.parse("$foo()$").eval(foo, template_group).should eql('foo renderer')
|
25
|
+
@parser.parse("$foobar:foo()$").eval({:foobar => foo}, template_group).should eql('foo renderer')
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should find a renderer based on the type of the object to render' do
|
29
|
+
foo = Foo.new
|
30
|
+
template_group = Slippers::TemplateGroup.new(:templates => {Foo => FooRenderer.new})
|
31
|
+
@parser.parse("$foobar$").eval({:foobar => foo}, template_group).should eql('foo renderer')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class Person
|
4
|
+
def initialize(first, last)
|
5
|
+
@first, @last = first, last
|
6
|
+
end
|
7
|
+
attr_reader :first, :last
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
describe SlippersParser do
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
@parser = SlippersParser.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should parse the subtemplate found within the delimiters' do
|
18
|
+
template = Slippers::Engine.new('template for this')
|
19
|
+
template_with_underscore = Slippers::Engine.new('template with underscore')
|
20
|
+
predefined_templates = {:template => template, :template_with_underscore => template_with_underscore, :template2 => template}
|
21
|
+
template_group = Slippers::TemplateGroup.new(:templates => predefined_templates)
|
22
|
+
|
23
|
+
@parser.parse('$template()$').eval(nil, template_group).should eql('template for this')
|
24
|
+
@parser.parse('$template2()$').eval(nil, template_group).should eql('template for this')
|
25
|
+
@parser.parse('Stuff before $template()$ and after').eval(nil, template_group).should eql('Stuff before template for this and after')
|
26
|
+
@parser.parse('then there is $template_with_underscore()$').eval(nil, template_group).should eql('then there is template with underscore')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should apply the attribute to a subtemplate when parsing it' do
|
30
|
+
person = OpenStruct.new({:name => Person.new('fred', 'flinstone')})
|
31
|
+
subtemplate = Slippers::Engine.new('Hello $first$ $last$')
|
32
|
+
template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
|
33
|
+
|
34
|
+
@parser.parse('$name:person()$').eval(person, template_group).should eql('Hello fred flinstone')
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should parse an anonymous subtemplate' do
|
38
|
+
@parser.parse('$people:{template for this $name$}$').eval(:people => {:name => 'fred'}).should eql('template for this fred')
|
39
|
+
@parser.parse('$people:{template for this "$name$"}$').eval(:people => {:name => 'fred'}).should eql('template for this "fred"')
|
40
|
+
@parser.parse('${template for this $name$}$').eval(:name => 'fred').should eql('template for this fred')
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should apply a list of objects to subtemplates" do
|
44
|
+
people = [ Person.new('fred', 'flinstone'), Person.new('barney', 'rubble') ]
|
45
|
+
subtemplate = Slippers::Engine.new('this is $first$ $last$ ')
|
46
|
+
template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
|
47
|
+
object_to_render = OpenStruct.new({:people => people})
|
48
|
+
|
49
|
+
@parser.parse('$people:person()$').eval(object_to_render, template_group).should eql("this is fred flinstone this is barney rubble ")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should render an empty string when the subtemplate cannot be found" do
|
53
|
+
@parser.parse("This is the unknown template $unknown()$!").eval(Person.new('fred', 'flinstone')).should eql("This is the unknown template !")
|
54
|
+
@parser.parse("This is the unknown template $first:unknown()$!").eval(Person.new('fred', 'flinstone')).should eql("This is the unknown template !")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should parse the file template from the template group" do
|
58
|
+
template_group = Slippers::TemplateGroupDirectory.new('spec/views')
|
59
|
+
name = OpenStruct.new({:first => 'fred', :last => 'flinestone'})
|
60
|
+
people = OpenStruct.new({:fred => name})
|
61
|
+
@parser.parse("should parse $person/name()$").eval(name, template_group).should eql("should parse fred flinestone")
|
62
|
+
@parser.parse("should parse $fred:person/name()$").eval(people, template_group).should eql("should parse fred flinestone")
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
|
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.
|
4
|
+
version: 0.0.8
|
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-
|
12
|
+
date: 2009-09-25 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -62,9 +62,10 @@ files:
|
|
62
62
|
- spec/binding_wrapper.rb
|
63
63
|
- spec/engine.rb
|
64
64
|
- spec/file_template.rb
|
65
|
-
- spec/
|
65
|
+
- spec/parse_attributes.rb
|
66
|
+
- spec/parse_renderers.rb
|
67
|
+
- spec/parse_templates.rb
|
66
68
|
- spec/person_template.st
|
67
|
-
- spec/slippers_spec.rb
|
68
69
|
- spec/spec_helper.rb
|
69
70
|
- spec/template_group.rb
|
70
71
|
- spec/template_group_directory.rb
|
data/spec/parser.rb
DELETED
@@ -1,134 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
class Person
|
4
|
-
def initialize(first, last)
|
5
|
-
@first, @last = first, last
|
6
|
-
end
|
7
|
-
attr_reader :first, :last
|
8
|
-
def full_name
|
9
|
-
to_s
|
10
|
-
end
|
11
|
-
def to_s
|
12
|
-
first + ' ' + last
|
13
|
-
end
|
14
|
-
end
|
15
|
-
describe SlippersParser do
|
16
|
-
|
17
|
-
before(:each) do
|
18
|
-
@parser = SlippersParser.new
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should return the string unparsed when there are no keywords in it" do
|
22
|
-
@parser.parse('').eval(nil).should eql('')
|
23
|
-
@parser.parse(' ').eval(nil).should eql(' ')
|
24
|
-
@parser.parse('this should be returned unchanged').eval.should eql('this should be returned unchanged')
|
25
|
-
@parser.parse(' this should be returned unchanged ').eval.should eql(' this should be returned unchanged ')
|
26
|
-
@parser.parse('this should be 1234567890 ').eval.should eql('this should be 1234567890 ')
|
27
|
-
@parser.parse('this should be abc1234567890 ').eval.should eql('this should be abc1234567890 ')
|
28
|
-
@parser.parse('this should be !@£%^&*()').eval.should eql('this should be !@£%^&*()')
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'should find the keyword within the delimiters' do
|
32
|
-
message = OpenStruct.new({:message => 'the message', :message2 => 'the second message', :name => 'fred', :full_name => 'fred flinstone'})
|
33
|
-
@parser.parse('$message$').eval(message).should eql('the message')
|
34
|
-
@parser.parse('$message$ for $name$').eval(message).should eql('the message for fred')
|
35
|
-
@parser.parse('we want to find $message$').eval(message).should eql('we want to find the message')
|
36
|
-
@parser.parse('$message$ has spoken').eval(message).should eql('the message has spoken')
|
37
|
-
@parser.parse('Yes! $message$ has spoken').eval(message).should eql('Yes! the message has spoken')
|
38
|
-
@parser.parse('Yes! $full_name$ has spoken').eval(message).should eql('Yes! fred flinstone has spoken')
|
39
|
-
@parser.parse('Yes! $message2$ has spoken').eval(message).should eql('Yes! the second message has spoken')
|
40
|
-
@parser.parse('Yes! "$message2$" has spoken').eval(message).should eql('Yes! "the second message" has spoken')
|
41
|
-
@parser.parse('$$').eval(message).should eql('')
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'should parse the subtemplate found within the delimiters' do
|
45
|
-
template = Slippers::Engine.new('template for this')
|
46
|
-
template_with_underscore = Slippers::Engine.new('template with underscore')
|
47
|
-
template_group = Slippers::TemplateGroup.new(:templates => {:template => template, :template_with_underscore => template_with_underscore, :template2 => template})
|
48
|
-
@parser.parse('$template()$').eval(nil, template_group).should eql('template for this')
|
49
|
-
@parser.parse('$template2()$').eval(nil, template_group).should eql('template for this')
|
50
|
-
@parser.parse('Stuff before $template()$ and after').eval(nil, template_group).should eql('Stuff before template for this and after')
|
51
|
-
@parser.parse('then there is $template_with_underscore()$').eval(nil, template_group).should eql('then there is template with underscore')
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'should parse an anonymous subtemplate' do
|
55
|
-
@parser.parse('$people:{template for this $name$}$').eval(:people => {:name => 'fred'}).should eql('template for this fred')
|
56
|
-
@parser.parse('$people:{template for this "$name$"}$').eval(:people => {:name => 'fred'}).should eql('template for this "fred"')
|
57
|
-
@parser.parse('${template for this $name$}$').eval(:name => 'fred').should eql('template for this fred')
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'should parse an anonymous subtemplate within an anonymous subtemplate' do
|
61
|
-
stuff = @parser.parse('$cars:{I really like $types:{toyota}$ }$')
|
62
|
-
stuff.eval(:cars => {:types => 'toyota'}) if stuff
|
63
|
-
#.should eql('template for this')
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'should apply the attribute to a subtemplate when parsing it' do
|
67
|
-
subtemplate = Slippers::Engine.new('Hello $first$ $last$')
|
68
|
-
template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
|
69
|
-
person = OpenStruct.new({:name => OpenStruct.new({:first => 'fred', :last => 'flinstone'})})
|
70
|
-
@parser.parse('$name:person()$').eval(person, template_group).should eql('Hello fred flinstone')
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'should not match on escaped delimiters' do
|
74
|
-
@parser.parse('stuff \$notmatched\$').eval(stub(:nothing)).should eql('stuff \$notmatched\$')
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should render a list of objects" do
|
78
|
-
people = [OpenStruct.new({:name => 'fred'}), OpenStruct.new({:name => 'barney'}) ]
|
79
|
-
@parser.parse('this is $name$').eval(people).should eql("this is fredbarney")
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should apply a list of objects to subtemplates" do
|
83
|
-
people = [ Person.new('fred', 'flinstone'), Person.new('barney', 'rubble') ]
|
84
|
-
subtemplate = Slippers::Engine.new('this is $first$ $last$ ')
|
85
|
-
template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
|
86
|
-
object_to_render = OpenStruct.new({:people => people})
|
87
|
-
|
88
|
-
@parser.parse('$people:person()$').eval(object_to_render, template_group).should eql("this is fred flinstone this is barney rubble ")
|
89
|
-
end
|
90
|
-
|
91
|
-
it "should substitue in an empty string when the subtemplate cannot be found" do
|
92
|
-
person = OpenStruct.new({:name => 'red'})
|
93
|
-
@parser.parse("This is the unknown template $name:unknown()$!").eval(person).should eql("This is the unknown template !")
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should substitute in an empty string when the attribute cannot be found" do
|
97
|
-
@parser.parse("This is the $adjective$ template with $message$.").eval(OpenStruct.new).should eql("This is the template with .")
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should parse the file template from the template group" do
|
101
|
-
template_group = Slippers::TemplateGroupDirectory.new('spec/views')
|
102
|
-
name = OpenStruct.new({:first => 'fred', :last => 'flinestone'})
|
103
|
-
people = OpenStruct.new({:fred => name})
|
104
|
-
@parser.parse("should parse $person/name()$").eval(name, template_group).should eql("should parse fred flinestone")
|
105
|
-
@parser.parse("should parse $fred:person/name()$").eval(people, template_group).should eql("should parse fred flinestone")
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should convert attribute to string" do
|
109
|
-
fred = OpenStruct.new({:name => 'fred', :dob => DateTime.new(1983, 1, 2)})
|
110
|
-
template_group = Slippers::TemplateGroup.new(:templates => {:date => Slippers::Engine.new('$year$')} )
|
111
|
-
@parser.parse("This is $name$ who was born in $dob:date()$").eval(fred, template_group).should eql('This is fred who was born in 1983')
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should render a hash" do
|
115
|
-
hash_object = {:title => 'Domain driven design', :author => 'Eric Evans'}
|
116
|
-
@parser.parse("should parse $title$ by $author$").eval(hash_object).should eql("should parse Domain driven design by Eric Evans")
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'should return an empty string if the subtemplate does not respond to render' do
|
120
|
-
template_group = Slippers::TemplateGroup.new(:templates => {:not_a_renderer => stub('renderer')})
|
121
|
-
@parser.parse("$not_a_renderer()$").eval(stub('object'), template_group).should eql('')
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'should return an empty string if the template is not correctly formed' do
|
125
|
-
@parser.parse("$not_properly_formed").should eql(nil)
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'should render an empty string if it cannot find the attribute to render' do
|
129
|
-
@parser.parse("$not_me$").eval(Person.new('fred', 'flinestone')).should eql('')
|
130
|
-
end
|
131
|
-
|
132
|
-
end
|
133
|
-
|
134
|
-
|