snidely_whiplash 1.0.0
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/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +110 -0
- data/Rakefile +1 -0
- data/lib/snidely_whiplash/version.rb +3 -0
- data/lib/snidely_whiplash.rb +17 -0
- data/snidely_whiplash.gemspec +22 -0
- data/spec/snidely_whiplash_spec.rb +22 -0
- data/spec/spec_helper.rb +17 -0
- metadata +90 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Bob Matcuk
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# SnidelyWhiplash
|
2
|
+
|
3
|
+
Let's say you have a partial view for displaying one of your models (written in your favorite templating language... [haml](http://haml.info/) perhaps?). But, hey, this 2013! Wouldn't it be nice if you could do some fancy, AJAXy thing to load new instances of your model and automatically format them the same way?
|
4
|
+
|
5
|
+
So you could use some fancypants javascript to make that happen, but that's ugly. So you Google around and find [mustache](http://mustache.github.io/). Mustache takes a string with some HTML and `{{variables}}` and replaces all of those variables with data from a javascript object. Ok cool.
|
6
|
+
|
7
|
+
But what are you going to do? Rewrite your partial view in some large boring javascript string so you can pass it to mustache? Yuck. If only you can haz your mustache template inline with your HTML. That would look prettier. Oh, wait! [You can haz that!](http://icanhazjs.com/)
|
8
|
+
|
9
|
+
Ok, but that doesn't solve the fact that you have to have two versions of your partial view: one for ruby, and one for ICanHaz/Mustache. That's not very "dry". Lame.
|
10
|
+
|
11
|
+
It's a good thing you have a friend that understands mustaches: good ol' Snidely Whiplash! He'll teach you proper grooming technique... right after he's done tying Nell to the railroad tracks!
|
12
|
+
|
13
|
+
SnidelyWhiplash is a brain-dead simple mock class that causes all of your `model.property` lines in your partial view to output as `{{model.property}}`. The end result is that you get a block of HTML which you can shove in a `<script type="text/html">` for use with ICanHaz/Mustache. SnidelyWhiplash understands any simple outputs in your template (like `<%= model.property %>`), but doesn't understand anything more complicated than that. This is really just a simple solution to converting simple partial views to mustache templates so they can be used both server-side, and client-side.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'snidely_whiplash'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install snidely_whiplash
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Let's say you have a partial view that looks like this:
|
32
|
+
|
33
|
+
```erb
|
34
|
+
<div class="user">
|
35
|
+
<h2><%= user.full_name %></h2>
|
36
|
+
<dl>
|
37
|
+
<dt>Address:</dt>
|
38
|
+
<dd><%= user.address %></dd>
|
39
|
+
<dt>Email:</dt>
|
40
|
+
<dd><%= user.email %></dd>
|
41
|
+
</dl>
|
42
|
+
</div>
|
43
|
+
```
|
44
|
+
|
45
|
+
... or maybe in haml:
|
46
|
+
|
47
|
+
```haml
|
48
|
+
.user
|
49
|
+
%h2= user.full_name
|
50
|
+
%dl
|
51
|
+
%dt Address:
|
52
|
+
%dd= user.address
|
53
|
+
%dt Email:
|
54
|
+
%dd= user.email
|
55
|
+
```
|
56
|
+
|
57
|
+
And perhaps you render it by running: `render partial: 'user', locals: {user: @user}`.
|
58
|
+
|
59
|
+
You could create an ICanHaz/Mustache template by adding the following, in ERB:
|
60
|
+
|
61
|
+
```erb
|
62
|
+
<script id="user_template" type="text/html">
|
63
|
+
<%= render partial: 'user', locals: {user: SnidelyWhiplash.new} %>
|
64
|
+
</script>
|
65
|
+
```
|
66
|
+
|
67
|
+
... or in haml:
|
68
|
+
|
69
|
+
```haml
|
70
|
+
%script#user_template{type: 'text/html'}
|
71
|
+
= render partial: 'user', locals: {user: SnidelyWhiplash.new}
|
72
|
+
```
|
73
|
+
|
74
|
+
Either way would cause the following output:
|
75
|
+
|
76
|
+
```html
|
77
|
+
<script id="user_template" type="text/html">
|
78
|
+
<div class="user">
|
79
|
+
<h2>{{full_name}}</h2>
|
80
|
+
<dl>
|
81
|
+
<dt>Address:</dt>
|
82
|
+
<dd>{{address}}</dd>
|
83
|
+
<dt>Email:</dt>
|
84
|
+
<dd>{{email}}</dd>
|
85
|
+
</dl>
|
86
|
+
</div>
|
87
|
+
</script>
|
88
|
+
```
|
89
|
+
|
90
|
+
... which you could then use in javascript with something like:
|
91
|
+
|
92
|
+
```javascript
|
93
|
+
data = {
|
94
|
+
full_name: 'Nell Fenwick',
|
95
|
+
address: 'Canada',
|
96
|
+
email: 'nell@example.org'
|
97
|
+
};
|
98
|
+
|
99
|
+
var user = ich.user_template(data);
|
100
|
+
```
|
101
|
+
|
102
|
+
The `SnidelyWhiplash` constructor can take one argument which can be used to specify the parent path for properties. For example, if you had used `SnidelyWhiplash.new('user')` in the examples above, the mustache outputs would have all been in the form `user.full_name`, `user.address`, and `user.email`. Your path can contain dots for those cases when your data is very deeply nested (for example: `SnidelyWhiplash.new('result.user')` would cause output like `{{result.user.full_name}}`, etc).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
1. Fork it
|
107
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
108
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
109
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
110
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "snidely_whiplash/version"
|
2
|
+
|
3
|
+
class SnidelyWhiplash
|
4
|
+
|
5
|
+
def initialize(path=nil)
|
6
|
+
@path = path
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
"{{#{@path || ''}}}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(method, *args, &block)
|
14
|
+
SnidelyWhiplash.new(@path.nil? ? method.to_s : "#{@path}.#{method.to_s}")
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'snidely_whiplash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "snidely_whiplash"
|
8
|
+
gem.version = SnidelyWhiplash::VERSION
|
9
|
+
gem.authors = ["Bob Matcuk"]
|
10
|
+
gem.email = ["snidelywhiplash@squeg.net"]
|
11
|
+
gem.description = %q{Convert simple partial views into a mustache template.}
|
12
|
+
gem.summary = %q{Convert simple partial views into a mustache template.}
|
13
|
+
gem.homepage = "https://github.com/bmatcuk/snidely_whiplash"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency('rspec-core')
|
21
|
+
gem.add_development_dependency('rspec-expectations')
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'snidely_whiplash'
|
3
|
+
|
4
|
+
describe SnidelyWhiplash do
|
5
|
+
it "should respond to a method with a new instance of itself" do
|
6
|
+
obj = SnidelyWhiplash.new
|
7
|
+
obj.example.should be_an_instance_of SnidelyWhiplash
|
8
|
+
obj.nested.example.should be_an_instance_of SnidelyWhiplash
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return a mustache-escaped string describing the method calls" do
|
12
|
+
obj = SnidelyWhiplash.new
|
13
|
+
obj.example.to_s.should eql '{{example}}'
|
14
|
+
obj.nested.example.to_s.should eql '{{nested.example}}'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should allow the caller to specify a parent path" do
|
18
|
+
obj = SnidelyWhiplash.new 'parent'
|
19
|
+
obj.example.to_s.should eql '{{parent.example}}'
|
20
|
+
obj.nested.example.to_s.should eql '{{parent.nested.example}}'
|
21
|
+
end
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snidely_whiplash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bob Matcuk
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec-core
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
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: rspec-expectations
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
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
|
+
description: Convert simple partial views into a mustache template.
|
47
|
+
email:
|
48
|
+
- snidelywhiplash@squeg.net
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/snidely_whiplash.rb
|
60
|
+
- lib/snidely_whiplash/version.rb
|
61
|
+
- snidely_whiplash.gemspec
|
62
|
+
- spec/snidely_whiplash_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
homepage: https://github.com/bmatcuk/snidely_whiplash
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Convert simple partial views into a mustache template.
|
88
|
+
test_files:
|
89
|
+
- spec/snidely_whiplash_spec.rb
|
90
|
+
- spec/spec_helper.rb
|