sprockets_spacely 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.
@@ -0,0 +1,5 @@
1
+ .idea/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in spacely.gemspec
4
+ gemspec
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Outright
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.
@@ -0,0 +1,62 @@
1
+ # Sprockets-Spacely
2
+
3
+ A way to hook Mustache.js into your Sprockets 2 and Rails Asset Pipeline.
4
+
5
+ Big thanks to Ryan Dy & [his take on the problem for jQuery templates](https://github.com/rdy/sprockets-jquery-tmpl).
6
+
7
+ ## In your Gemfile:
8
+
9
+ `gem 'sprockets_spacely'`
10
+
11
+ We assume that you are requiring jQuery and Mustache.js higher up in your Sprockets bundle.
12
+
13
+ ## In a Rails Initializer (or your Ruby framework's equivalent)
14
+
15
+ ```ruby
16
+ Sprockets::Mustache::Template::Namespace.value = 'namespace.for.my.templates'
17
+ ```
18
+
19
+ This will take any *.mustache file under Sprockets' paths and generate a closure that sets a function that evaluates your template.
20
+
21
+ File: "app/assets/javascripts/my_template.mustache"
22
+
23
+ ```html
24
+ {{name}}:
25
+ <ul>
26
+ {{#items}}
27
+ <li>{{.}}</li>
28
+ {{/items}}
29
+ </ul>
30
+ ```
31
+
32
+ This function will be created:
33
+
34
+ ```javascript
35
+ (function($) {
36
+
37
+ #{@namespace}.mustache['#{@template_name}'] = {
38
+ template: "#{@template_string}",
39
+ render: render
40
+ };
41
+
42
+ function render(obj, partials) {
43
+ return Mustache.to_html(#{@namespace}.mustache['#{@template_name}'].template, obj, partials);
44
+ }
45
+ }(jQuery));
46
+ ```
47
+
48
+ ...which Sprockets will add to the appropriate JavaScript asset bundle.
49
+
50
+ Which means in your code you can call:
51
+
52
+ ```javascript
53
+ namespace.for.my.templates.mustache['javascripts/my_template'].render({
54
+ name: "George",
55
+ items: ["Judy", "Jane", "Elroy", "Astro"]
56
+ });
57
+ ```
58
+
59
+ ## Versions
60
+
61
+ ### 0.0.2
62
+ - Added ability to use mustache partials
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ require 'sprockets/engines'
2
+ require 'tilt'
3
+ require 'action_view'
4
+ require 'action_view/helpers'
5
+ require 'action_view/helpers/javascript_helper'
6
+
7
+ require "sprockets_spacely/version"
8
+ require "sprockets_spacely/mustache/template/namespace"
9
+ require "sprockets_spacely/mustache/template/generator"
10
+ require "sprockets_spacely/mustache/template/processor"
@@ -0,0 +1,35 @@
1
+ module Sprockets
2
+ module Mustache
3
+ module Template
4
+ class Generator
5
+
6
+ attr_reader :template_name
7
+
8
+ def initialize(namespace, logical_path, template_string)
9
+ @template_name = logical_path
10
+ @namespace = namespace
11
+ @template_string = template_string
12
+ end
13
+
14
+ def generate
15
+
16
+ js_function = <<-JS
17
+ (function($) {
18
+
19
+ #{@namespace}.mustache['#{@template_name}'] = {
20
+ template: "#{@template_string}",
21
+ render: render
22
+ };
23
+
24
+ function render(obj, partials) {
25
+ return Mustache.to_html(#{@namespace}.mustache['#{@template_name}'].template, obj, partials);
26
+ }
27
+ }(jQuery));
28
+ JS
29
+
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,17 @@
1
+ module Sprockets
2
+ module Mustache
3
+ module Template
4
+ class Namespace
5
+
6
+ def self.value=(val)
7
+ @@value = val
8
+ end
9
+
10
+ def self.value
11
+ @@value
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ module Sprockets
2
+ module Mustache
3
+ module Template
4
+ class Processor < Tilt::Template
5
+ include ActionView::Helpers::JavaScriptHelper
6
+
7
+ def self.default_mime_type
8
+ 'application/javascript'
9
+ end
10
+
11
+ def prepare
12
+ end
13
+
14
+ def evaluate(scope, locals, &block)
15
+
16
+ namespace = Namespace.value || "window"
17
+
18
+ Generator.new(namespace, scope.logical_path, escape_javascript(data)).generate
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ register_engine '.mustache', ::Sprockets::Mustache::Template::Processor
25
+ end
@@ -0,0 +1,3 @@
1
+ module SprocketsSpacely
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sprockets::Mustache::Template::Generator do
4
+
5
+ let :generator do
6
+
7
+ namespace = "my.Namespace"
8
+ logical_path = "javascripts/backbone/templates/mustache/entryList"
9
+ template_string = <<-HTML
10
+ <table class="ledger">
11
+ <thead>
12
+ <tr>
13
+ <th class="date">Date</td>
14
+ <th class="name">Name</td>
15
+ <th class="category">Category</td>
16
+ <th class="amount">Amount ($)</td>
17
+ </tr>
18
+ </thead>
19
+ {{{rows}}}
20
+ </table>
21
+ HTML
22
+
23
+ Sprockets::Mustache::Template::Generator.new(namespace, logical_path, template_string)
24
+ end
25
+
26
+ it "calculates the template name" do
27
+ generator.template_name.should == "javascripts/backbone/templates/mustache/entryList"
28
+ end
29
+
30
+ describe "#generate" do
31
+ before do
32
+ @js = generator.generate
33
+ end
34
+
35
+ it "makes the named JS function as a string" do
36
+ @js.should be_present
37
+ end
38
+
39
+ it "assigns the correct namespace" do
40
+ @js.should match(/my\.Namespace\.mustache\['javascripts\/backbone\/templates\/mustache\/entryList'\] = \{/)
41
+ end
42
+
43
+ it "puts the template in the mustache object" do
44
+ @js.should match(/<table class="ledger">/)
45
+ end
46
+
47
+ it "has a render function that calls Mustache.to_html" do
48
+ @js.should match(/Mustache\.to_html/)
49
+ end
50
+
51
+ it "passes partials to to_html" do
52
+ @js.should match(/to_html.+, partials/)
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1 @@
1
+ require 'sprockets_spacely'
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sprockets_spacely/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sprockets_spacely"
7
+ s.version = SprocketsSpacely::VERSION
8
+ s.authors = ["Outright"]
9
+ s.email = [""]
10
+ s.homepage = ""
11
+ s.summary = %q{Even lighter-weight way to get Mustache.js into your Sprockets. Entirely influenced by Ryan Dy's '}
12
+ s.description = %q{ Mr.Spacely, of Spacely Sprockets, Inc. had a mustache: http://www.google.com/search?q=mr.+spacely&hl=en&prmd=imvns&tbm=isch&tbo=u&source=univ&sa=X&ei=YI2wTpafH9PPiALHnIAF&ved=0CD0QsAQ&biw=1080&bih=1282&sei=%20jY6wTsr-CsSmiQKg0Tw}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency "rails"
20
+ s.add_dependency "tilt"
21
+
22
+ s.add_development_dependency "rspec"
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets_spacely
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
+ - Outright
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-06 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ name: rails
31
+ type: :runtime
32
+ prerelease: false
33
+ requirement: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ version_requirements: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ name: tilt
45
+ type: :runtime
46
+ prerelease: false
47
+ requirement: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ version_requirements: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ name: rspec
59
+ type: :development
60
+ prerelease: false
61
+ requirement: *id003
62
+ description: " Mr.Spacely, of Spacely Sprockets, Inc. had a mustache: http://www.google.com/search?q=mr.+spacely&hl=en&prmd=imvns&tbm=isch&tbo=u&source=univ&sa=X&ei=YI2wTpafH9PPiALHnIAF&ved=0CD0QsAQ&biw=1080&bih=1282&sei=%20jY6wTsr-CsSmiQKg0Tw"
63
+ email:
64
+ - ""
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - .gitignore
73
+ - .rspec
74
+ - Gemfile
75
+ - LICENSE.md
76
+ - README.md
77
+ - Rakefile
78
+ - lib/sprockets_spacely.rb
79
+ - lib/sprockets_spacely/mustache/template/generator.rb
80
+ - lib/sprockets_spacely/mustache/template/namespace.rb
81
+ - lib/sprockets_spacely/mustache/template/processor.rb
82
+ - lib/sprockets_spacely/version.rb
83
+ - spec/lib/template_generator_spec.rb
84
+ - spec/spec_helper.rb
85
+ - sprockets_spacely.gemspec
86
+ homepage: ""
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.6
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Even lighter-weight way to get Mustache.js into your Sprockets. Entirely influenced by Ryan Dy's '
119
+ test_files:
120
+ - spec/lib/template_generator_spec.rb
121
+ - spec/spec_helper.rb