mote 0.0.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,115 @@
1
+ Mote
2
+ ====
3
+
4
+ Minimum Operational Template.
5
+
6
+ Description
7
+ -----------
8
+
9
+ Mote is the little brother of ERB. It only provides a subset of ERB's
10
+ features, but praises itself of being simple--both internally and externally--
11
+ and super fast. It was born out of experimentations while discussing
12
+ [NOLATE](https://github.com/antirez/nolate), another small library with the
13
+ same goals and more features.
14
+
15
+ Usage
16
+ -----
17
+
18
+ Usage is very similar to that of ERB:
19
+
20
+ template = Mote.parse("This is a template")
21
+ template.call #=> "This is a template"
22
+
23
+ Silly example, you may say, and I would agree. What follows is a short list of
24
+ the different use cases you may face:
25
+
26
+ ## Assignment
27
+
28
+ example = Mote.parse("<%= \"***\" %>")
29
+ assert_equal "***", example.call
30
+
31
+ ## Comment
32
+
33
+ example = Mote.parse("*<%# \"*\" %>*")
34
+ assert_equal "**", example.call
35
+
36
+ ## Control flow
37
+
38
+ example = Mote.parse("<% if false %>*<% else %>***<% end %>")
39
+ assert_equal "***", example.call
40
+
41
+ ## Block evaluation
42
+
43
+ example = Mote.parse("<% 3.times { %>*<% } %>")
44
+ assert_equal "***", example.call
45
+
46
+ ## Parameters
47
+
48
+ example = Mote.parse("<% params[:n].times { %>*<% } %>")
49
+ assert_equal "***", example[n: 3]
50
+ assert_equal "****", example[n: 4]
51
+
52
+ Two things are worth noting in the last example: the first is that as the
53
+ returned value is a `Proc`, you can call it with square brackets and anything
54
+ you put inside becomes a parameter. Second, that within the template you have
55
+ access to those parameters through the `params` hash, instead of the usual
56
+ approach of converting each key to a local variable.
57
+
58
+ # Helpers
59
+
60
+ There are a couple helpers available in the `Mote::Helpers` module, and you are
61
+ free to include it in your code. To do it, just type:
62
+
63
+ include Mote::Helpers
64
+
65
+ Here are the available helpers:
66
+
67
+ ## `mote`
68
+
69
+ The `mote` helper receives a template string and returns the rendered version
70
+ of it:
71
+
72
+ assert_equal "1 2 3", mote("1 <%= 2 %> 3")
73
+
74
+ It works with parameters too:
75
+
76
+ assert_equal "1 2 3", mote("1 <%= params[:n] %> 3", :n => 2)
77
+
78
+ ## `mote_file`
79
+
80
+ The `mote_file` helper receives a file name without the `.erb` extension and
81
+ returns the rendered version of its content. The compiled template is cached
82
+ for subsequent calls.
83
+
84
+ assert_equal "***\n", mote_file("test/basic", :n => 3)
85
+
86
+ Installation
87
+ ------------
88
+
89
+ $ gem install mote
90
+
91
+ License
92
+ -------
93
+
94
+ Copyright (c) 2011 Michel Martens
95
+
96
+ Permission is hereby granted, free of charge, to any person
97
+ obtaining a copy of this software and associated documentation
98
+ files (the "Software"), to deal in the Software without
99
+ restriction, including without limitation the rights to use,
100
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
101
+ copies of the Software, and to permit persons to whom the
102
+ Software is furnished to do so, subject to the following
103
+ conditions:
104
+
105
+ The above copyright notice and this permission notice shall be
106
+ included in all copies or substantial portions of the Software.
107
+
108
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
109
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
110
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
111
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
112
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
113
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
114
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
115
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ task :test do
2
+ require "cutest"
3
+ Cutest.run(Dir["test/*.rb"])
4
+ end
5
+
6
+ task :default => :test
data/lib/mote.rb ADDED
@@ -0,0 +1,53 @@
1
+ # Copyright (c) 2010 Michel Martens
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+ class Mote
21
+ VERSION = "0.0.1.rc1"
22
+
23
+ def self.parse(template, context = self)
24
+ terms = template.split(/(<%[=#]?)\s*(.*?)\s*%>/)
25
+
26
+ parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"
27
+
28
+ while term = terms.shift
29
+ case term
30
+ when "<%#" then terms.shift # skip
31
+ when "<%" then parts << "#{terms.shift}\n"
32
+ when "<%=" then parts << "__o << (#{terms.shift}).to_s\n"
33
+ else parts << "__o << #{term.inspect}\n"
34
+ end
35
+ end
36
+
37
+ parts << "__o; end"
38
+
39
+ context.instance_eval(parts)
40
+ end
41
+
42
+ module Helpers
43
+ def mote(template, params = {})
44
+ Mote.parse(template).call(params)
45
+ end
46
+
47
+ def mote_file(file_name, params = {})
48
+ @_mote ||= {}
49
+ @_mote[file_name] ||= Mote.parse(File.read("#{file_name}.erb"))
50
+ @_mote[file_name][params]
51
+ end
52
+ end
53
+ end
data/mote.gemspec ADDED
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "mote"
3
+ s.version = "0.0.1.rc1"
4
+ s.summary = "Minimum Operational Template."
5
+ s.description = "Mote is the little brother of ERB. It only provides a subset of ERB's features, but praises itself of being simple--both internally and externally--and super fast."
6
+ s.authors = ["Michel Martens"]
7
+ s.email = ["michel@soveran.com"]
8
+ s.homepage = "http://github.com/soveran/mote"
9
+ s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/mote.rb", "mote.gemspec", "test/mote_test.rb"]
10
+ s.add_development_dependency "cutest", "~> 0.1"
11
+ end
data/test/mote_test.rb ADDED
@@ -0,0 +1,45 @@
1
+ require File.expand_path("../lib/mote", File.dirname(__FILE__))
2
+
3
+ scope do
4
+ test "assignment" do
5
+ example = Mote.parse("<%= \"***\" %>")
6
+ assert_equal "***", example.call
7
+ end
8
+
9
+ test "comment" do
10
+ example = Mote.parse("*<%# \"*\" %>*")
11
+ assert_equal "**", example.call
12
+ end
13
+
14
+ test "control flow" do
15
+ example = Mote.parse("<% if false %>*<% else %>***<% end %>")
16
+ assert_equal "***", example.call
17
+ end
18
+
19
+ test "block evaluation" do
20
+ example = Mote.parse("<% 3.times { %>*<% } %>")
21
+ assert_equal "***", example.call
22
+ end
23
+
24
+ test "parameters" do
25
+ example = Mote.parse("<% params[:n].times { %>*<% } %>")
26
+ assert_equal "***", example[:n => 3]
27
+ assert_equal "****", example[:n => 4]
28
+ end
29
+ end
30
+
31
+ include Mote::Helpers
32
+
33
+ scope do
34
+ test do
35
+ assert_equal "1 2 3", mote("1 <%= 2 %> 3")
36
+ end
37
+
38
+ test do
39
+ assert_equal "1 2 3", mote("1 <%= params[:n] %> 3", :n => 2)
40
+ end
41
+
42
+ test do
43
+ assert_equal "***\n", mote_file("test/basic", :n => 3)
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mote
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: 6
5
+ version: 0.0.1.rc1
6
+ platform: ruby
7
+ authors:
8
+ - Michel Martens
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-05 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: cutest
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "0.1"
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: Mote is the little brother of ERB. It only provides a subset of ERB's features, but praises itself of being simple--both internally and externally--and super fast.
28
+ email:
29
+ - michel@soveran.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - LICENSE
38
+ - README.markdown
39
+ - Rakefile
40
+ - lib/mote.rb
41
+ - mote.gemspec
42
+ - test/mote_test.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/soveran/mote
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">"
62
+ - !ruby/object:Gem::Version
63
+ version: 1.3.1
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.6.0
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Minimum Operational Template.
71
+ test_files: []
72
+