snidely_whiplash 1.0.2 → 1.1.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/README.md +7 -3
- data/lib/snidely_whiplash.rb +32 -3
- data/lib/snidely_whiplash/version.rb +1 -1
- data/snidely_whiplash.gemspec +1 -0
- data/spec/snidely_whiplash_spec.rb +10 -0
- metadata +4 -3
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Add this line to your application's Gemfile:
|
|
20
20
|
|
21
21
|
And then execute:
|
22
22
|
|
23
|
-
$ bundle
|
23
|
+
$ bundle install
|
24
24
|
|
25
25
|
Or install it yourself as:
|
26
26
|
|
@@ -117,9 +117,13 @@ will output:
|
|
117
117
|
<span>{{{build_menu}}}</span>
|
118
118
|
```
|
119
119
|
|
120
|
-
|
120
|
+
### Options
|
121
121
|
|
122
|
-
|
122
|
+
The `SnidelyWhiplash` constructor can take three arguments:
|
123
|
+
|
124
|
+
1. *Parent Path:* A parent path to use for mustache code. 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). The default value for this argument is `nil` which means "no parent path".
|
125
|
+
2. *HTML Escape:* True to enable html escaping in mustache (this is the default), or false to disable html escaping for all outputs! You probably don't want to use this though... you're probably better off just using `html_safe` for the values that need to have html escaping disabled.
|
126
|
+
3. *Values:* A hash of values to use for certain methods. Sometimes you don't want certain methods to output mustache code, but you'd rather have them do something server-side. This hash lets you do that. The keys of the hash correspond to the paths that SnidelyWhiplash would output normally, so nesting is possible. For example, if you wanted to use the value "Dudley Do-Right" for "model.user.name", you could use the hash: `{'user.name' => 'Dudley Do-Right'}`. The keys must be strings.
|
123
127
|
|
124
128
|
## Contributing
|
125
129
|
|
data/lib/snidely_whiplash.rb
CHANGED
@@ -2,9 +2,36 @@ require "snidely_whiplash/version"
|
|
2
2
|
|
3
3
|
class SnidelyWhiplash
|
4
4
|
|
5
|
-
|
5
|
+
# Initialize SnidelyWhiplash
|
6
|
+
#
|
7
|
+
# options:
|
8
|
+
# path
|
9
|
+
# Specifies a parent path for the mustache output. For example, if you
|
10
|
+
# set path to "user", and then used things like "model.first_name" in
|
11
|
+
# your view, the resulting mustache output would be "{{user.first_name}}".
|
12
|
+
# You may have dots in this path (ex: "post.user"). The default is nil,
|
13
|
+
# which means that there is no parent path.
|
14
|
+
# html_escape
|
15
|
+
# If true (the default), the mustache output will use two curly braces,
|
16
|
+
# which signifies that the output should be html escaped. If you set this
|
17
|
+
# to false, the mustache output will use three curly braces, signifying
|
18
|
+
# that the output should not be altered. Since this affects all mustache
|
19
|
+
# outputs, you probably don't want to use this! Rather, you should use
|
20
|
+
# something like "model.item.html_safe" in cases where you want to
|
21
|
+
# disable html escaping.
|
22
|
+
# values
|
23
|
+
# Sometimes you want certain values to be interpreted server-side
|
24
|
+
# instead of converted to client-side mustache code. You can use this
|
25
|
+
# parameter to specify values that should be used for certain methods
|
26
|
+
# server-side. The keys in this hash MUST be strings and they correspond
|
27
|
+
# to the paths that SnidelyWhiplash would have output otherwise. So, for
|
28
|
+
# example, if you wanted to specify a value for "model.user.name", you
|
29
|
+
# could pass the following hash in as "values":
|
30
|
+
# {'user.name' => 'Dudley Do-Right'}
|
31
|
+
def initialize(path=nil, html_escape=true, values={})
|
6
32
|
@path = path
|
7
33
|
@html_escape = html_escape
|
34
|
+
@values = values
|
8
35
|
end
|
9
36
|
|
10
37
|
def to_s
|
@@ -13,9 +40,11 @@ class SnidelyWhiplash
|
|
13
40
|
|
14
41
|
def method_missing(method_sym, *args, &block)
|
15
42
|
if method_sym == :html_safe
|
16
|
-
SnidelyWhiplash.new(@path, false)
|
43
|
+
SnidelyWhiplash.new(@path, false, @values)
|
17
44
|
else
|
18
|
-
|
45
|
+
newpath = @path.nil? ? method_sym.to_s : "#{@path}.#{method_sym.to_s}"
|
46
|
+
return @values[newpath] if @values.has_key? newpath
|
47
|
+
SnidelyWhiplash.new(newpath, @html_escape, @values)
|
19
48
|
end
|
20
49
|
end
|
21
50
|
|
data/snidely_whiplash.gemspec
CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.description = %q{Convert simple partial views into a mustache template.}
|
12
12
|
gem.summary = %q{Convert simple partial views into a mustache template.}
|
13
13
|
gem.homepage = "https://github.com/bmatcuk/snidely_whiplash"
|
14
|
+
gem.license = "MIT"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -32,4 +32,14 @@ describe SnidelyWhiplash do
|
|
32
32
|
ary = [obj]
|
33
33
|
ary.flatten.should eql [obj]
|
34
34
|
end
|
35
|
+
|
36
|
+
it "should use a specific value if one is passed in" do
|
37
|
+
obj = SnidelyWhiplash.new nil, true, 'test' => 42
|
38
|
+
obj.test.should eql 42
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should understand nesting when determining if there is a specific value to use" do
|
42
|
+
obj = SnidelyWhiplash.new nil, true, 'nested.test' => 42
|
43
|
+
obj.nested.test.should eql 42
|
44
|
+
end
|
35
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snidely_whiplash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-10-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec-core
|
@@ -62,7 +62,8 @@ files:
|
|
62
62
|
- spec/snidely_whiplash_spec.rb
|
63
63
|
- spec/spec_helper.rb
|
64
64
|
homepage: https://github.com/bmatcuk/snidely_whiplash
|
65
|
-
licenses:
|
65
|
+
licenses:
|
66
|
+
- MIT
|
66
67
|
post_install_message:
|
67
68
|
rdoc_options: []
|
68
69
|
require_paths:
|