guard-mustachejs 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Nathan Matthews
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ = Guard for MustacheJS templates
2
+
3
+ Guard::Mustachejs concatenates all your mustache templates into a single javascript file. Your templates are stored in a global object named 'mustache_templates' by default.
4
+
5
+
6
+ == Install
7
+
8
+ First, install {guard}[http://github.com/guard/guard]. Then, install the gem:
9
+
10
+ gem install guard-mustachejs
11
+
12
+ Or you can add it to your Gemfile:
13
+
14
+ gem 'guard-mustachejs'
15
+
16
+ To add the guard definition for mustachejs to your Guardfile, execute:
17
+
18
+ guard init mustachejs
19
+
20
+
21
+ == Usage
22
+
23
+ By default, guard-mustachejs will look for mustache templates in app/mustache/**/*.html. Let's say you have the following files:
24
+
25
+ app/mustache/home/banner.html
26
+ app/mustache/table/row.html
27
+
28
+ Guard will pull these files into a single javascript resource located at:
29
+
30
+ public/javascripts/mustache-templates.js
31
+
32
+ Assuming you have included mustache.js, you can then render your templates with:
33
+
34
+ Mustache.to_html(mustache_templates.home.banner, root)
35
+ Mustache.to_html(mustache_templates.table.row, root)
36
+
37
+
38
+ == Guardfile and options
39
+
40
+ By default, guard-mustachejs will look for mustache templates in app/mustache/**/*.html. The default guard clause looks like this:
41
+
42
+ guard 'mustachejs' do
43
+ watch(%r{app/mustache/(.+)\.html})
44
+ end
45
+
46
+ You can change the output javascript file path and the name of the global variable used to store the templates. You can even have multiple template targets, like so:
47
+
48
+ guard 'mustachejs',
49
+ :output => 'public/javascripts/js-templates.js',
50
+ :variable => 'js_templates' do
51
+ watch(%r{app/mustache/(.+)\.js})
52
+ end
53
+
54
+ guard 'mustachejs',
55
+ :output => 'public/javascripts/html-templates.js',
56
+ :variable => 'html_templates' do
57
+ watch(%r{app/mustache/(.+)\.html})
58
+ end
59
+
60
+
61
+ == Authors
62
+
63
+ * {Nathan Matthews}[https://github.com/lowentropy]
64
+ * {Jamie Wilkinson}[https://github.com/jamiew]
65
+
66
+
67
+ == Acknowledgement
68
+
69
+ Thanks to the {Guard Team}[https://github.com/guard/guard/contributors] and to all the guard gem {authors}[https://github.com/guard], especially {Michael Kessler}[https://github.com/netzpirat] for his excellent {guard-coffeescript}[https://github.com/netzpirat/guard-coffeescript] gem, which was the inspiration for this one.
@@ -0,0 +1,73 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+
4
+ module Guard
5
+ class Mustachejs < Guard
6
+
7
+ def initialize(watchers = [], options = {})
8
+ super(watchers, {
9
+ :variable => 'mustache_templates',
10
+ :output => 'public/javascripts/mustache-templates.js'
11
+ }.merge(options))
12
+ end
13
+
14
+ def start
15
+ write_template([]) unless File.exists?(options[:output])
16
+ end
17
+
18
+ def run_on_changes(paths)
19
+ run_all
20
+ end
21
+
22
+ def run_all
23
+ paths = Watcher.match_files(self, Dir.glob(File.join('**', '*'))).uniq
24
+ write_template paths
25
+ end
26
+
27
+ private
28
+
29
+ def write_template(paths)
30
+ template = "var #{options[:variable]} = {};\n"
31
+ paths.each do |path|
32
+ key, raw = convert_to_js path
33
+ key.unshift options[:variable].clone
34
+ write_key_value template, key, raw
35
+ end
36
+ file = File.open(options[:output], 'w')
37
+ file.puts template
38
+ file.close
39
+ notify paths
40
+ end
41
+
42
+ def write_key_value(template, key, value)
43
+ lhs = key.shift
44
+ until key.empty?
45
+ part = key.shift
46
+ lhs << '.' << part
47
+ rhs = key.any? ? '{}' : value
48
+ line = "#{lhs} = #{rhs};\n"
49
+ template << line unless template.include? line
50
+ end
51
+ end
52
+
53
+ def notify(paths)
54
+ message = "#{options[:output]}:\n\n#{paths.join("\n")}"
55
+ ::Guard::Notifier.notify message, :title => "MustacheJS"
56
+ end
57
+
58
+ def convert_to_js(path)
59
+ watchers.each do |watcher|
60
+ if match = path.match(watcher.pattern)
61
+ unless base_name = match[1]
62
+ raise "No capture groups in guard pattern for mustache"
63
+ end
64
+ js = File.read(path).inspect
65
+ key = base_name.gsub(/[-.]/,'_').split('/')
66
+ return key, js
67
+ end
68
+ end
69
+ raise "No matcher matched '#{path}'"
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ guard 'mustachejs' do
2
+ watch(%r{app/mustache/(.+)\.html})
3
+ end
@@ -0,0 +1,5 @@
1
+ module Guard
2
+ module MustachejsVersion
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-mustachejs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Nathan Matthews
14
+ - Jamie Wilkinson
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-06-24 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: guard
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 1
33
+ version: "1.1"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 13
45
+ segments:
46
+ - 1
47
+ - 1
48
+ version: "1.1"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: guard-rspec
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 13
60
+ segments:
61
+ - 1
62
+ - 1
63
+ version: "1.1"
64
+ type: :development
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: rspec
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 23
75
+ segments:
76
+ - 2
77
+ - 10
78
+ version: "2.10"
79
+ type: :development
80
+ version_requirements: *id004
81
+ description: Compiles mustache templates into a single .js file.
82
+ email:
83
+ - lowentropy@gmail.com
84
+ - jamie@jamiedubs.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files: []
90
+
91
+ files:
92
+ - lib/guard/mustachejs/templates/Guardfile
93
+ - lib/guard/mustachejs/version.rb
94
+ - lib/guard/mustachejs.rb
95
+ - LICENSE
96
+ - README.rdoc
97
+ homepage: https://github.com/jamiew/guard-mustachejs
98
+ licenses: []
99
+
100
+ post_install_message:
101
+ rdoc_options: []
102
+
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 23
120
+ segments:
121
+ - 1
122
+ - 3
123
+ - 6
124
+ version: 1.3.6
125
+ requirements: []
126
+
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.17
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Guard for mustache.js templates
132
+ test_files: []
133
+
134
+ has_rdoc: