slippers 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. data/LICENSE +20 -0
  2. data/README +56 -0
  3. data/Rakefile +27 -0
  4. data/VERSION.yml +4 -0
  5. data/examples/blog/README +3 -0
  6. data/examples/blog/blog.db +0 -0
  7. data/examples/blog/controller/main.rb +30 -0
  8. data/examples/blog/model/entry.rb +31 -0
  9. data/examples/blog/public/styles/blog.css +132 -0
  10. data/examples/blog/spec/blog.rb +87 -0
  11. data/examples/blog/start.rb +7 -0
  12. data/examples/blog/view/edit.st +19 -0
  13. data/examples/blog/view/edit.xhtml +17 -0
  14. data/examples/blog/view/entry.st +12 -0
  15. data/examples/blog/view/index.st +4 -0
  16. data/examples/blog/view/index.xhtml +17 -0
  17. data/examples/blog/view/layout.st +11 -0
  18. data/examples/blog/view/layout.xhtml +11 -0
  19. data/examples/blog/view/new.st +16 -0
  20. data/examples/blog/view/new.xhtml +16 -0
  21. data/examples/forms/Rakefile +22 -0
  22. data/examples/forms/controller/init.rb +12 -0
  23. data/examples/forms/controller/main.rb +20 -0
  24. data/examples/forms/controller/registration.rb +24 -0
  25. data/examples/forms/forms.db +0 -0
  26. data/examples/forms/model/core_extensions/string_extensions.rb +5 -0
  27. data/examples/forms/model/field.rb +33 -0
  28. data/examples/forms/model/form.rb +33 -0
  29. data/examples/forms/model/init.rb +18 -0
  30. data/examples/forms/model/orm/registration.rb +12 -0
  31. data/examples/forms/model/registration_form_builder.rb +7 -0
  32. data/examples/forms/model/registration_repository.rb +39 -0
  33. data/examples/forms/model/registration_rules/incorrect_email_format.rb +11 -0
  34. data/examples/forms/model/registration_rules/number_field.rb +10 -0
  35. data/examples/forms/model/registration_rules/required_field.rb +9 -0
  36. data/examples/forms/model/registration_validator.rb +17 -0
  37. data/examples/forms/public/css/ramaze_error.css +90 -0
  38. data/examples/forms/public/dispatch.fcgi +11 -0
  39. data/examples/forms/public/favicon.ico +0 -0
  40. data/examples/forms/public/js/jquery.js +3549 -0
  41. data/examples/forms/public/ramaze.png +0 -0
  42. data/examples/forms/public/web-application.js +0 -0
  43. data/examples/forms/spec/form_spec.rb +16 -0
  44. data/examples/forms/spec/main.rb +25 -0
  45. data/examples/forms/spec/registration_controller_spec.rb +0 -0
  46. data/examples/forms/spec/registration_repository_spec.rb +38 -0
  47. data/examples/forms/spec/registration_validator_spec.rb +53 -0
  48. data/examples/forms/spec/spec_helper.rb +3 -0
  49. data/examples/forms/start.rb +12 -0
  50. data/examples/forms/start.ru +16 -0
  51. data/examples/forms/view/error.xhtml +64 -0
  52. data/examples/forms/view/index.xhtml +34 -0
  53. data/examples/forms/view/page.xhtml +27 -0
  54. data/examples/forms/view/registration/index.st +26 -0
  55. data/examples/main_controller.rb +40 -0
  56. data/examples/start.rb +7 -0
  57. data/examples/todolist/README +1 -0
  58. data/examples/todolist/controller/main.rb +71 -0
  59. data/examples/todolist/layout/page.rb +31 -0
  60. data/examples/todolist/model/tasks.rb +14 -0
  61. data/examples/todolist/public/favicon.ico +0 -0
  62. data/examples/todolist/public/js/jquery.js +1923 -0
  63. data/examples/todolist/public/ramaze.png +0 -0
  64. data/examples/todolist/start.rb +11 -0
  65. data/examples/todolist/todolist.db +11 -0
  66. data/examples/todolist/view/index.st +39 -0
  67. data/examples/todolist/view/index.xhtml +17 -0
  68. data/examples/todolist/view/new.st +41 -0
  69. data/examples/todolist/view/new.xhtml +7 -0
  70. data/examples/todolist/view/tasks.st +6 -0
  71. data/examples/todolist.db +5 -0
  72. data/examples/view/index.st +11 -0
  73. data/examples/view/person/age.st +1 -0
  74. data/examples/view/person/age_renderer.rb +6 -0
  75. data/examples/view/person/index.st +2 -0
  76. data/lib/engine/binding_wrapper.rb +10 -0
  77. data/lib/engine/engine.rb +28 -0
  78. data/lib/engine/file_template.rb +12 -0
  79. data/lib/engine/slippers.treetop +169 -0
  80. data/lib/engine/slippers_nodes.rb +119 -0
  81. data/lib/engine/template.rb +17 -0
  82. data/lib/engine/template_group.rb +40 -0
  83. data/lib/engine/template_group_directory.rb +57 -0
  84. data/lib/ramazeTemplates/0001-Adding-slippers-as-a-new-template.patch +138 -0
  85. data/lib/ramazeTemplates/0002-Fixing-problem-with-using-other-renderers.patch +27 -0
  86. data/lib/ramazeTemplates/slippers.rb +22 -0
  87. data/lib/slippers.rb +14 -0
  88. data/spec/binding_wrapper.rb +19 -0
  89. data/spec/engine.rb +89 -0
  90. data/spec/file_template.rb +9 -0
  91. data/spec/parse_attributes.rb +98 -0
  92. data/spec/parse_renderers.rb +35 -0
  93. data/spec/parse_templates.rb +73 -0
  94. data/spec/person_template.st +1 -0
  95. data/spec/spec_helper.rb +14 -0
  96. data/spec/template_group.rb +52 -0
  97. data/spec/template_group_directory.rb +43 -0
  98. data/spec/views/index.st +1 -0
  99. data/spec/views/money.rb +2 -0
  100. data/spec/views/person/age.st +1 -0
  101. data/spec/views/person/date_renderer.rb +5 -0
  102. data/spec/views/person/name.st +1 -0
  103. metadata +218 -0
@@ -0,0 +1,138 @@
1
+ From 11437e4517358214979eef34351208f4d2fefcbb Mon Sep 17 00:00:00 2001
2
+ From: starapor <me@sarahtaraporewalla.com>
3
+ Date: Sun, 13 Sep 2009 14:47:14 +0100
4
+ Subject: [PATCH] Adding slippers as a new template
5
+
6
+ ---
7
+ lib/ramaze/view.rb | 1 +
8
+ lib/ramaze/view/slippers.rb | 22 ++++++++++++++
9
+ spec/ramaze/view/slippers.rb | 52 +++++++++++++++++++++++++++++++++
10
+ spec/ramaze/view/slippers/external.st | 8 +++++
11
+ spec/ramaze/view/slippers/sum.st | 1 +
12
+ 5 files changed, 84 insertions(+), 0 deletions(-)
13
+ create mode 100644 lib/ramaze/view/slippers.rb
14
+ create mode 100644 spec/ramaze/view/slippers.rb
15
+ create mode 100644 spec/ramaze/view/slippers/external.st
16
+ create mode 100644 spec/ramaze/view/slippers/sum.st
17
+
18
+ diff --git a/lib/ramaze/view.rb b/lib/ramaze/view.rb
19
+ index e69ae84..ec44754 100644
20
+ --- a/lib/ramaze/view.rb
21
+ +++ b/lib/ramaze/view.rb
22
+ @@ -35,5 +35,6 @@ module Ramaze
23
+ auto_register :Sass, :sass
24
+ auto_register :Tagz, :rb, :tagz
25
+ auto_register :Tenjin, :rbhtml, :tenjin
26
+ + auto_register :Slippers, :st
27
+ end
28
+ end
29
+ diff --git a/lib/ramaze/view/slippers.rb b/lib/ramaze/view/slippers.rb
30
+ new file mode 100644
31
+ index 0000000..f604185
32
+ --- /dev/null
33
+ +++ b/lib/ramaze/view/slippers.rb
34
+ @@ -0,0 +1,22 @@
35
+ +require 'slippers'
36
+ +
37
+ +module Ramaze
38
+ + module View
39
+ + module Slippers
40
+ + def self.call(action, string)
41
+ + slippers = View.compile(string){|s| ::Slippers::Engine.new(s, :template_group => template_group(action)) }
42
+ + object_to_render = ::Slippers::BindingWrapper.new(action.instance.binding)
43
+ + html = slippers.render(object_to_render)
44
+ + return html, 'text/html'
45
+ + end
46
+ +
47
+ + private
48
+ + def self.template_group(action)
49
+ + subtemplates = action.instance.ancestral_trait[:slippers_options] || {}
50
+ + view_root = "#{action.instance.options[:roots]}/#{action.instance.options[:views]}"
51
+ + template_group_directory = ::Slippers::TemplateGroupDirectory.new(view_root)
52
+ + template_group = ::Slippers::TemplateGroup.new(:super_group => template_group_directory, :templates => subtemplates)
53
+ + end
54
+ + end
55
+ + end
56
+ +end
57
+ diff --git a/spec/ramaze/view/slippers.rb b/spec/ramaze/view/slippers.rb
58
+ new file mode 100644
59
+ index 0000000..769b547
60
+ --- /dev/null
61
+ +++ b/spec/ramaze/view/slippers.rb
62
+ @@ -0,0 +1,52 @@
63
+ +require File.expand_path('../../../../spec/helper', __FILE__)
64
+ +spec_require 'slippers'
65
+ +
66
+ +Ramaze::App.options.views = 'slippers'
67
+ +
68
+ +class SpecSlippers < Ramaze::Controller
69
+ + map '/'
70
+ + engine :Slippers
71
+ +
72
+ + def index
73
+ + @value = "foo"
74
+ + '<h1>Slippers Index with $value$</h1>'
75
+ + end
76
+ +
77
+ + def sum(num1, num2)
78
+ + @num1, @num2 = num1.to_i, num2.to_i
79
+ + end
80
+ +
81
+ + def external
82
+ + end
83
+ +end
84
+ +
85
+ +describe 'Ramaze::View::Slippers' do
86
+ + behaves_like :rack_test
87
+ +
88
+ + should 'render' do
89
+ + get('/').body.should == '<h1>Slippers Index with foo</h1>'
90
+ + end
91
+ +
92
+ + should 'render external template' do
93
+ + get('/external').body.strip.
94
+ + should == '<html>
95
+ + <head>
96
+ + <title>Slippers Test</title>
97
+ + </head>
98
+ + <body>
99
+ + <h1>Slippers Template</h1>
100
+ + </body>
101
+ +</html>'
102
+ + end
103
+ +
104
+ + should 'render external template with instance variables' do
105
+ + got = get('/sum/1/2')
106
+ + got.status.should == 200
107
+ + got['Content-Type'].should == 'text/html'
108
+ + got.body.strip.should ==
109
+ +"<div>
110
+ + 1 and 2
111
+ +</div>"
112
+ + end
113
+ +
114
+ +end
115
+ diff --git a/spec/ramaze/view/slippers/external.st b/spec/ramaze/view/slippers/external.st
116
+ new file mode 100644
117
+ index 0000000..431467b
118
+ --- /dev/null
119
+ +++ b/spec/ramaze/view/slippers/external.st
120
+ @@ -0,0 +1,8 @@
121
+ +<html>
122
+ + <head>
123
+ + <title>Slippers Test</title>
124
+ + </head>
125
+ + <body>
126
+ + <h1>Slippers Template</h1>
127
+ + </body>
128
+ +</html>
129
+ diff --git a/spec/ramaze/view/slippers/sum.st b/spec/ramaze/view/slippers/sum.st
130
+ new file mode 100644
131
+ index 0000000..728e130
132
+ --- /dev/null
133
+ +++ b/spec/ramaze/view/slippers/sum.st
134
+ @@ -0,0 +1 @@
135
+ +<div>$num1$ and $num2$</div>
136
+ --
137
+ 1.6.1
138
+
@@ -0,0 +1,27 @@
1
+ From a72bd79dfa70bc41cf9bc97ef0f5e96273583a17 Mon Sep 17 00:00:00 2001
2
+ From: starapor <me@sarahtaraporewalla.com>
3
+ Date: Tue, 15 Sep 2009 19:16:55 +0100
4
+ Subject: [PATCH 2/2] Fixing problem with using other renderers
5
+
6
+ ---
7
+ lib/ramaze/view/slippers.rb | 4 ++--
8
+ 1 files changed, 2 insertions(+), 2 deletions(-)
9
+
10
+ diff --git a/lib/ramaze/view/slippers.rb b/lib/ramaze/view/slippers.rb
11
+ index f604185..7a4569a 100644
12
+ --- a/lib/ramaze/view/slippers.rb
13
+ +++ b/lib/ramaze/view/slippers.rb
14
+ @@ -14,8 +14,8 @@ module Ramaze
15
+ def self.template_group(action)
16
+ subtemplates = action.instance.ancestral_trait[:slippers_options] || {}
17
+ view_root = "#{action.instance.options[:roots]}/#{action.instance.options[:views]}"
18
+ - template_group_directory = ::Slippers::TemplateGroupDirectory.new(view_root)
19
+ - template_group = ::Slippers::TemplateGroup.new(:super_group => template_group_directory, :templates => subtemplates)
20
+ + super_group = ::Slippers::TemplateGroup.new(:templates => subtemplates)
21
+ + ::Slippers::TemplateGroupDirectory.new(view_root, :super_group => super_group)
22
+ end
23
+ end
24
+ end
25
+ --
26
+ 1.6.1
27
+
@@ -0,0 +1,22 @@
1
+ require 'slippers'
2
+
3
+ module Ramaze
4
+ module View
5
+ module Slippers
6
+ def self.call(action, string)
7
+ slippers = View.compile(string){|s| ::Slippers::Engine.new(s, :template_group => template_group(action)) }
8
+ object_to_render = ::Slippers::BindingWrapper.new(action.instance.binding)
9
+ html = slippers.render(object_to_render)
10
+ return html, 'text/html'
11
+ end
12
+
13
+ private
14
+ def self.template_group(action)
15
+ subtemplates = action.instance.ancestral_trait[:slippers_options] || {}
16
+ views = action.instance.options[:views].map{|view| "#{action.instance.options[:roots]}/#{view}" }
17
+ super_group = ::Slippers::TemplateGroup.new(:templates => subtemplates)
18
+ ::Slippers::TemplateGroupDirectory.new(views, :super_group => super_group)
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/slippers.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'treetop'
3
+ require 'active_support'
4
+
5
+ require File.dirname(__FILE__) + '/engine/template'
6
+ require File.dirname(__FILE__) + '/engine/file_template'
7
+ require File.dirname(__FILE__) + '/engine/template_group'
8
+ require File.dirname(__FILE__) + '/engine/template_group_directory'
9
+ require File.dirname(__FILE__) + '/engine/engine'
10
+ require File.dirname(__FILE__) + '/engine/binding_wrapper'
11
+ require File.dirname(__FILE__) + '/engine/slippers_nodes'
12
+
13
+
14
+
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Slippers::BindingWrapper do
4
+ def f
5
+ @a = 22
6
+ @b = 33
7
+ binding
8
+ end
9
+
10
+ it "should evaluate the bindings" do
11
+ bindings_wrapper = Slippers::BindingWrapper.new(f())
12
+ bindings_wrapper['a'].should eql(22)
13
+ end
14
+
15
+ it "should evaluate the bindings" do
16
+ bindings_wrapper = Slippers::BindingWrapper.new(f())
17
+ bindings_wrapper['z'].should eql(nil)
18
+ end
19
+ end
data/spec/engine.rb ADDED
@@ -0,0 +1,89 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Slippers::Engine do
4
+ before do
5
+ template = 'Hello $first$ $last$'
6
+ @engine = Slippers::Engine.new(template)
7
+ end
8
+
9
+ it "should render a template without any holes" do
10
+ template = "This is a string without any holes in it"
11
+ engine = Slippers::Engine.new(template)
12
+ engine.render.should eql("This is a string without any holes in it")
13
+ end
14
+
15
+ it "should fill in a hole within a template" do
16
+ template = "This is a string with a message of $message$"
17
+ engine = Slippers::Engine.new(template)
18
+ engine.render(:message => "hello world").should eql("This is a string with a message of hello world")
19
+ end
20
+
21
+ it "should render a subtemplate within a template" do
22
+ subtemplate = Slippers::Engine.new("this is a subtemplate")
23
+ template_group = Slippers::TemplateGroup.new(:templates => {:message => subtemplate})
24
+ template = "This is a template and then $message()$"
25
+ engine = Slippers::Engine.new(template, :template_group => template_group)
26
+ engine.render.should eql("This is a template and then this is a subtemplate")
27
+ end
28
+
29
+ it "should apply a template to an attribute" do
30
+ subtemplate = Slippers::Engine.new("this is a subtemplate with a message of $saying$")
31
+ template_group = Slippers::TemplateGroup.new(:templates => {:message_subtemplate => subtemplate})
32
+ template = "This is a template and then $message:message_subtemplate()$!"
33
+ engine = Slippers::Engine.new(template, :template_group => template_group)
34
+ engine.render(:message => {:saying => 'hello world'}).should eql("This is a template and then this is a subtemplate with a message of hello world!")
35
+ end
36
+
37
+ it "should apply an anonymous subtemplate to an attribute" do
38
+ template = "This is a template and then $message:{this is a subtemplate with a message of $saying$}$!"
39
+ engine = Slippers::Engine.new(template)
40
+ engine.render(:message => {:saying => 'hello world'}).should eql("This is a template and then this is a subtemplate with a message of hello world!")
41
+ end
42
+
43
+ it "should render a subtemplate using different rendering technologies" do
44
+ age_renderer = AgeRenderer.new
45
+ subtemplate = Slippers::Engine.new('$first$ $last$')
46
+ person = OpenStruct.new({:name => {:last => 'Flinstone', :first => 'Fred'}, :dob => Date.new(DateTime.now.year - 34, 2, 4)})
47
+ template_group = Slippers::TemplateGroup.new(:templates => {:name => subtemplate, :age => age_renderer})
48
+ engine = Slippers::Engine.new("Introducing $name:name()$ who is $dob:age()$.", :template_group => template_group)
49
+ engine.render(person).should eql("Introducing Fred Flinstone who is 34 years old.")
50
+ end
51
+
52
+ it "should select a renderer based on the type of the object to render" do
53
+ person = OpenStruct.new({:name => {:first => 'Fred', :last => 'Flinstone'}, :dob => Date.new(DateTime.now.year - 34, 2, 4)})
54
+ template_group = Slippers::TemplateGroup.new(:templates => {:name => Slippers::Engine.new('$first$ $last$'), Date => AgeRenderer.new})
55
+
56
+ engine = Slippers::Engine.new("Introducing $name:name()$ who is $dob$.", :template_group => template_group)
57
+ engine.render(person).should eql("Introducing Fred Flinstone who is 34 years old.")
58
+ end
59
+
60
+ it 'should render a bindings wrapper' do
61
+ @first = 'fred'
62
+ @last = 'flinstone'
63
+
64
+ person_binding = Slippers::BindingWrapper.new(binding)
65
+ @engine.render(person_binding).should eql('Hello fred flinstone')
66
+ end
67
+
68
+ it 'should render a list of objects' do
69
+ subtemplate = Slippers::Engine.new('Hello $first$ $last$ ')
70
+ template_group = Slippers::TemplateGroup.new(:templates => {:person => subtemplate})
71
+ template = 'Say: $people:person()$'
72
+ @people = [OpenStruct.new({:first => 'fred', :last => 'flinstone'}), OpenStruct.new({:first => 'barney', :last => 'rubble'})]
73
+
74
+ engine = Slippers::Engine.new(template, :template_group => template_group)
75
+ engine.render(Slippers::BindingWrapper.new(binding)).should eql('Say: Hello fred flinstone Hello barney rubble ')
76
+ end
77
+
78
+ it 'should render empty string if the template can not be evaluated' do
79
+ engine = Slippers::Engine.new('$this_is_bad')
80
+ engine.render(stub('object')).should eql('')
81
+ end
82
+ end
83
+
84
+ class AgeRenderer
85
+ def render(date)
86
+ age = DateTime.now.year - date.year
87
+ age.to_s + " years old"
88
+ end
89
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Slippers::FileTemplate, " when rendering" do
4
+ it "should read the template from a file" do
5
+ template_file = Slippers::FileTemplate.new('spec/person_template.st')
6
+ template_file.template.should eql('This is a $template$')
7
+ end
8
+ end
9
+
@@ -0,0 +1,98 @@
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 render the default string when the attribute cannot be found on the object to render" do
42
+ Slippers::Engine::DEFAULT_STRING.should eql('')
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')
47
+ Slippers::Engine::DEFAULT_STRING = ""
48
+ end
49
+
50
+ it "should convert attribute to string" do
51
+ fred = OpenStruct.new({:name => 'fred', :dob => DateTime.new(1983, 1, 2)})
52
+ template_group = Slippers::TemplateGroup.new(:templates => {:date => Slippers::Engine.new('$year$')} )
53
+ @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')
54
+ end
55
+
56
+ it "should render a hash" do
57
+ hash_object = {:title => 'Domain driven design', :author => 'Eric Evans', :find => 'method on a hash'}
58
+ @parser.parse("should parse $title$ by $author$").eval(hash_object).should eql("should parse Domain driven design by Eric Evans")
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
+ end
61
+
62
+ it "should render a symbol on a hash before its methods" do
63
+ hash_object = {:find => 'method on a hash'}
64
+ @parser.parse("should parse a symbol before a $find$").eval(hash_object).should eql('should parse a symbol before a method on a hash')
65
+ @parser.parse("should still render the method $size$").eval(hash_object).should eql('should still render the method 1')
66
+ end
67
+
68
+ it 'should return an empty string if the template is not correctly formed' do
69
+ @parser.parse("$not_properly_formed").should eql(nil)
70
+ end
71
+
72
+ it 'should use the specified expression options to render list items' do
73
+ @parser.parse('$list; null="-1", seperator=", "$').eval(:list => [1,2,nil,3]).should eql("1, 2, -1, 3")
74
+ @parser.parse('$list; seperator=", "$').eval(:list => [1,2,3]).should eql("1, 2, 3")
75
+ @parser.parse('$list; seperator="!!"$').eval(:list => [1,2,3,nil]).should eql("1!!2!!3")
76
+ @parser.parse('$list; null="-1"$').eval(:list => [1,nil,3]).should eql("1-13")
77
+ end
78
+
79
+ it 'should conditionally parse some text' do
80
+ @parser.parse("$if(greeting)$ Hello $end$").eval(:greeting => true).should eql(" Hello ")
81
+ @parser.parse("$if(greeting)$ Hello $end$").eval(:greeting => true).should eql(" Hello ")
82
+ @parser.parse("$if(greeting)$ Hello $end$").eval(:greeting => false).should eql("")
83
+ @parser.parse("$if(greeting)$ Hello $end$").eval(:greeting => nil).should eql("")
84
+ @parser.parse("$if(greeting)$Hello$else$Goodbye$end$").eval(:greeting => true).should eql("Hello")
85
+ @parser.parse("$if(greeting)$ Hello $else$ Goodbye $end$").eval(:greeting => false).should eql(" Goodbye ")
86
+ @parser.parse("$if(greeting)$ Hello $end$").eval(:greetingzzzz => true).should eql("")
87
+ @parser.parse("$if(greeting)$ $greeting$ $end$").eval(:greeting => 'Hi').should eql(" Hi ")
88
+ end
89
+
90
+ it 'should conditionally parse a template' do
91
+ @parser.parse("$if(greeting)$ $greeting$ $end$").eval(:greeting => 'Hi').should eql(" Hi ")
92
+ @parser.parse("$if(greeting)$$greeting$ $else$ Nothing to see here $end$").eval(:greeting => 'Hi').should eql("Hi ")
93
+ @parser.parse("$if(greeting)$$greeting$ $else$ Nothing to see here $end$").eval(:greeting => nil).should eql(" Nothing to see here ")
94
+ end
95
+
96
+ end
97
+
98
+
@@ -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_this_one => stub('renderer')})
18
+ @parser.parse("$not_this_one()$").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,73 @@
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, :template_2 => 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('$template_2()$').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
+ 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
71
+ end
72
+
73
+
@@ -0,0 +1 @@
1
+ This is a $template$
@@ -0,0 +1,14 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ require 'lib/slippers'
6
+ require 'ostruct'
7
+ require 'date'
8
+ require 'mocha'
9
+ require 'tempfile'
10
+
11
+ Spec::Runner.configure do |config|
12
+
13
+ end
14
+
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Slippers::TemplateGroup do
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
+
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)
16
+ end
17
+
18
+ it 'should return nil if it cannot find the right template' do
19
+ template_group = Slippers::TemplateGroup.new()
20
+ template_group.find(:not_this).should eql(nil)
21
+ template_group.find('not_this').should eql(nil)
22
+ end
23
+
24
+ it 'should look in the super template group if it cannot find the template' do
25
+ template = Slippers::Engine.new('Hello $first$ $last$')
26
+ super_template_group = Slippers::TemplateGroup.new(:templates => {:person => template})
27
+ template_group = Slippers::TemplateGroup.new(:templates => {}, :super_group => super_template_group)
28
+ template_group.find(:person).should eql(template)
29
+ template_group.find(:not_this).should eql(nil)
30
+ end
31
+
32
+ it 'should render an item if its class is registered' do
33
+ date = Date.new(DateTime.now.year, 2, 4)
34
+ rendered_text = "rendered text"
35
+ template_group = Slippers::TemplateGroup.new(:templates => {Date => OpenStruct.new({:render => rendered_text})})
36
+
37
+ template_group.has_registered?(date.class).should be_true
38
+ template_group.render(date).should eql(rendered_text)
39
+
40
+ template_group.has_registered?(template_group.class).should be_false
41
+ template_group.render(template_group).should eql('')
42
+ end
43
+
44
+ it 'should not render an item if there are no templates' do
45
+ date = Date.new(DateTime.now.year, 2, 4)
46
+ template_group = Slippers::TemplateGroup.new()
47
+
48
+ template_group.has_registered?(date.class).should be_false
49
+ template_group.render(date).should eql('')
50
+ end
51
+
52
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Slippers::TemplateGroupDirectory do
4
+
5
+ it 'should find the file in the directory folder' do
6
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
7
+ template_group.find('index').should eql(Slippers::Engine.new('Hey foo', :template_group => template_group))
8
+ template_group.find('person/age').should eql(Slippers::Engine.new('The age for him is $age$', :template_group => template_group))
9
+ end
10
+
11
+ it 'should return nil if it cannot find the file in the directory folder' do
12
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
13
+ template_group.find('person/not_found').should eql(nil)
14
+ end
15
+
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'])
18
+ template_group.find('index').should eql(Slippers::Engine.new('Hey foo', :template_group => template_group))
19
+ template_group.find('person/age').should eql(Slippers::Engine.new('The age for him is $age$', :template_group => template_group))
20
+ end
21
+
22
+ it 'should load the ruby file found in the directory folder' do
23
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'])
24
+ template_group.find('person/date_renderer').class.should eql(DateRenderer)
25
+ template_group.find('money').class.should eql(Money)
26
+ end
27
+
28
+ it 'should look in the super template group if it cannot find the template' do
29
+ template = stub 'template'
30
+ super_template_group = Slippers::TemplateGroup.new(:templates => {:person => template})
31
+ template_group = Slippers::TemplateGroupDirectory.new(['spec/views'], :super_group => super_template_group)
32
+ template_group.find('person').should eql(template)
33
+ template_group.find('not_this').should eql(nil)
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
+
42
+
43
+ end
@@ -0,0 +1 @@
1
+ Hey foo
@@ -0,0 +1,2 @@
1
+ class Money
2
+ end
@@ -0,0 +1 @@
1
+ The age for him is $age$