cuba-haml 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ cuba-haml
2
+ =========
3
+
4
+ A Cuba plugin to use Haml
5
+
6
+ Configure
7
+ ---------
8
+
9
+ __Cuba::Haml__ plugin introduces some __keys__ into _Cuba.settings_ to make easy the way to reander layouts
10
+
11
+ ``` ruby
12
+ # Default layouts directory
13
+ app.settings[:haml][:layout_path] ||= app.settings[:haml][:views]
14
+
15
+ # Default layout file
16
+ app.settings[:haml][:layout] ||= "layout"
17
+ ```
18
+
19
+ Feel free to overwrite this variables after install the plugin. Following this example
20
+ ``` ruby
21
+ require "cuba/haml"
22
+
23
+ Cuba.plugin Cuba::Haml
24
+ Cuba.settings[:haml][:layout_path] = "some/other/path"
25
+ Cuba.settings[:haml][:layout] = "not_default_layout"
26
+ ```
27
+
28
+
29
+ Rendering
30
+ ---------
31
+
32
+ Cuba ships with a plugin that provides helpers for rendering templates.
33
+ But if only want to use haml, you can specify this plugin.
34
+
35
+ ``` ruby
36
+ require "cuba/haml"
37
+
38
+ Cuba.plugin Cuba::Haml
39
+
40
+ Cuba.define do
41
+ on default do
42
+
43
+ # Within the partial, you will have access to the local variable `content`,
44
+ # that will hold the value "hello, world".
45
+ res.write render("home.haml", content: "hello, world")
46
+ end
47
+ end
48
+ ```
49
+
50
+ Using the method __render__, you need to provide _full path template_ so there are two methods to do it easy
51
+ `template_path(template_name)` and `layout_path(layout).`
52
+
53
+
54
+ But you can use the __haml__ helper to get a cleaner code
55
+ ``` ruby
56
+ ...
57
+ on default do
58
+ haml("home", content: "hello, world")
59
+ end
60
+ ...
61
+ ```
62
+
63
+ Also if you want to override the _default layout_ you can use the __view__ helper
64
+ ``` ruby
65
+ ...
66
+ on default do
67
+ view("home", 'other-layout', content: "hello, world")
68
+ end
69
+ ...
70
+ ```
71
+
72
+ Don't be afraid and read the tests, there are really simple I promisse!
73
+
74
+ > Note that in order to use this plugin you need to have [Haml](https://github.com/haml/haml) installed.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ task :test do
2
+ require "cutest"
3
+
4
+ Cutest.run(Dir["test/*.rb"])
5
+ end
6
+
7
+ task :default => :test
data/cuba-haml.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cuba-haml"
3
+ s.version = "1.0"
4
+ s.summary = "A Cuba plugin to use Haml."
5
+ s.description = "Cuba is a microframework for web applications."
6
+ s.authors = ["Emiliano Mancuso"]
7
+ s.email = ["emiliano.mancuso@gmail.com"]
8
+ s.homepage = "http://github.com/eMancu/cuba-haml"
9
+ s.license = "UNLICENSE"
10
+
11
+ s.files = Dir[
12
+ "README.md",
13
+ "Rakefile",
14
+ "lib/**/*.rb",
15
+ "*.gemspec",
16
+ "test/*.*"
17
+ ]
18
+
19
+ s.add_dependency "cuba"
20
+ s.add_dependency "haml"
21
+ s.add_development_dependency "cutest"
22
+ s.add_development_dependency "rack-test"
23
+ end
data/lib/cuba/haml.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'haml'
2
+
3
+ class Cuba
4
+ module Haml
5
+ def self.setup(app)
6
+ app.settings[:haml] ||= {}
7
+ app.settings[:haml][:views] ||= File.expand_path("views", Dir.pwd)
8
+ app.settings[:haml][:layout_path] ||= app.settings[:haml][:views]
9
+ app.settings[:haml][:layout] ||= "layout"
10
+ app.settings[:haml][:options] ||= {
11
+ default_encoding: Encoding.default_external
12
+ }
13
+ end
14
+
15
+ # Use default layout
16
+ def haml(template, locals = {})
17
+ res.write render(layout_path,
18
+ { content: partial(template, locals) }.merge(locals),
19
+ settings[:haml][:options])
20
+ end
21
+
22
+ # Use specific layout file nested into :layout_path
23
+ def view(template, layout_file, locals = {})
24
+ res.write render(layout_path(layout_file),
25
+ { content: partial(template, locals) }.merge(locals))
26
+ end
27
+
28
+ # Skip layout, only renders the template
29
+ def partial(template, locals = {})
30
+ render(template_path(template), locals, settings[:haml][:options])
31
+ end
32
+
33
+ def template_path(template)
34
+ "%s/%s.haml" % [
35
+ settings[:haml][:views],
36
+ template
37
+ ]
38
+ end
39
+
40
+ def layout_path(layout = settings[:haml][:layout])
41
+ "%s/%s.haml" % [
42
+ settings[:haml][:layout_path],
43
+ layout
44
+ ]
45
+ end
46
+
47
+ # Render any type of template file supported by Tilt.
48
+ #
49
+ # @example
50
+ #
51
+ # # Renders home, and is assumed to be HAML.
52
+ # render("home")
53
+ #
54
+ # # Renders with some local variables
55
+ # render("home", site_name: "My Site")
56
+ #
57
+ # # Renders with HAML options
58
+ # render("home", {}, ugly: true, format: :html5)
59
+ #
60
+ # # Renders in layout
61
+ # render("layout.haml") { render("home.haml") }
62
+ #
63
+ def render(template, locals = {}, options = {}, &block)
64
+ template = File.read(template)
65
+ ::Haml::Engine.new(template, options.merge(outvar: '@_output'))
66
+ .render(self, locals, &block)
67
+ end
68
+ end
69
+ end
data/test/haml.rb ADDED
@@ -0,0 +1,94 @@
1
+ require_relative "helper"
2
+
3
+ require "cuba/haml"
4
+
5
+ test "doesn't override the settings if they already exist" do
6
+ Cuba.settings[:haml] = {
7
+ :views => "./test/views",
8
+ :layout => "guest"
9
+ }
10
+
11
+ Cuba.plugin Cuba::Haml
12
+
13
+ assert_equal "./test/views", Cuba.settings[:haml][:views]
14
+ assert_equal "guest", Cuba.settings[:haml][:layout]
15
+ end
16
+
17
+ scope do
18
+ setup do
19
+ Cuba.plugin Cuba::Haml
20
+ Cuba.settings[:haml][:views] = "./test/views"
21
+ Cuba.settings[:haml][:template_engine] = "haml"
22
+
23
+ Cuba.define do
24
+ on "home" do
25
+ haml("home", name: "Agent Smith", title: "Home")
26
+ end
27
+
28
+ on "home2" do
29
+ view("home", 'layout2', name: "Agent Smith", title: "Home")
30
+ end
31
+
32
+ on "home3" do
33
+ res.write render(layout_path('layout-yield')) {
34
+ render(template_path("home"), name: "Agent Smith", title: "Home")
35
+ }
36
+ end
37
+
38
+ on "about" do
39
+ res.write partial("about", title: "About Cuba")
40
+ end
41
+ end
42
+ end
43
+
44
+ test "partial" do
45
+ _, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
46
+
47
+ assert_response body, ["<h1>About Cuba</h1>\n"]
48
+ end
49
+
50
+ test "haml" do
51
+ _, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
52
+
53
+ assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>\n"]
54
+ end
55
+
56
+ test "view" do
57
+ _, _, body = Cuba.call({ "PATH_INFO" => "/home2", "SCRIPT_NAME" => "/" })
58
+
59
+ assert_response body, ["Header\n<h1>Home</h1>\n<p>Hello Agent Smith</p>\nFooter\n"]
60
+ end
61
+
62
+ test "render with blocks" do
63
+ _, _, body = Cuba.call({ "PATH_INFO" => "/home3", "SCRIPT_NAME" => "/" })
64
+
65
+ assert_response body, ["Header\n<h1>Home</h1>\n<p>Hello Agent Smith</p>\nFooter\n"]
66
+ end
67
+ end
68
+
69
+ scope do
70
+ setup do
71
+ Cuba.plugin Cuba::Haml
72
+ Cuba.settings[:haml][:views] = "./test/views"
73
+
74
+ Cuba.define do
75
+ on 'default_layout' do
76
+ haml('content-yield', title: "Default layout")
77
+ end
78
+
79
+ on 'custom_layout' do
80
+ view("layout-yield.haml", "content-yield")
81
+ end
82
+ end
83
+
84
+ test "simple layout support" do
85
+ _, _, resp = Cuba.call({ "PATH_INFO" => "/default_layout", "SCRIPT_NAME" => "" })
86
+ assert_response resp, ["Cuba: Default layout\n"]
87
+ end
88
+
89
+ test "custom layout support" do
90
+ _, _, resp = Cuba.call({ "PATH_INFO" => "/custom_layout", "SCRIPT_NAME" => "" })
91
+ assert_response resp, ["Header\nThis is the actual content.\nFooter\n"]
92
+ end
93
+ end
94
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,13 @@
1
+ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
2
+ require "cuba"
3
+
4
+ prepare { Cuba.reset! }
5
+
6
+ def assert_response(body, expected)
7
+ arr = []
8
+ body.each { |line| arr << line }
9
+
10
+ flunk "#{arr.inspect} != #{expected.inspect}" unless arr == expected
11
+ print "."
12
+ end
13
+
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuba-haml
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Emiliano Mancuso
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cuba
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: haml
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: cutest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rack-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Cuba is a microframework for web applications.
79
+ email:
80
+ - emiliano.mancuso@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - README.md
86
+ - Rakefile
87
+ - lib/cuba/haml.rb
88
+ - cuba-haml.gemspec
89
+ - test/haml.rb
90
+ - test/helper.rb
91
+ homepage: http://github.com/eMancu/cuba-haml
92
+ licenses:
93
+ - UNLICENSE
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.24
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: A Cuba plugin to use Haml.
116
+ test_files: []