mustache_json 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/mustache/json/backends/active_support.rb +13 -0
- data/lib/mustache/json/backends/json_gem.rb +13 -0
- data/lib/mustache/json/backends/json_pure.rb +13 -0
- data/lib/mustache/json/backends/yajl.rb +13 -0
- data/lib/mustache_json.rb +61 -0
- data/spec/mustache_json_spec.rb +76 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- metadata +89 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Intridea, Inc. and Michael Bleigh
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= Mustache JSON Serialization
|
2
|
+
|
3
|
+
The <tt>mustache_json</tt> gem provides a simple way to encode Mustaches
|
4
|
+
into JSON for cross-compatibility with other Mustache rendering libraries
|
5
|
+
such as mustache.js (http://github.com/janl/mustache.js).
|
6
|
+
|
7
|
+
It extends Mustache in a simple way such that all public instance methods
|
8
|
+
declared in your Mustache will be automatically serialized into json when
|
9
|
+
the <tt>#to_json</tt> method is called. Any context variables that have
|
10
|
+
been set will be included as well. For example, take this Mustache:
|
11
|
+
|
12
|
+
require 'mustache_json'
|
13
|
+
|
14
|
+
class Person < Mustache
|
15
|
+
def initialize(first_name, last_name)
|
16
|
+
context[:first_name], context[:last_name] = first_name, last_name
|
17
|
+
end
|
18
|
+
|
19
|
+
def initials
|
20
|
+
"#{context[:first_name][0..0]}.#{context[:last_name][0..0]}."
|
21
|
+
end
|
22
|
+
|
23
|
+
def listing
|
24
|
+
"#{context[:last_name]}, #{context[:first_name]}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
With <tt>mustache_json</tt> you are able to do this:
|
29
|
+
|
30
|
+
bob = Person.new('Bob', 'Bobson')
|
31
|
+
bob.to_json
|
32
|
+
|
33
|
+
Which will output:
|
34
|
+
|
35
|
+
{"last_name":"Bobson","initials":"B.B.","listing":"Bobson, Bob","first_name":"Bob"}
|
36
|
+
|
37
|
+
It's a simple addition to Mustache but provides some powerful functionality
|
38
|
+
toward views that can be rendered either server-side (in Ruby) or client-side
|
39
|
+
(in Javascript).
|
40
|
+
|
41
|
+
== Note on Patches/Pull Requests
|
42
|
+
|
43
|
+
* Fork the project.
|
44
|
+
* Make your feature addition or bug fix.
|
45
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
46
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
47
|
+
* Send me a pull request.
|
48
|
+
|
49
|
+
== Copyright
|
50
|
+
|
51
|
+
Copyright (c) 2010 Intridea, Inc. and Michael Bleigh. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mustache_json"
|
8
|
+
gem.summary = %Q{Easily serialize Mustaches to JSON.}
|
9
|
+
gem.description = %Q{Easily serialize Mustaches to JSON.}
|
10
|
+
gem.email = "michael@intridea.com"
|
11
|
+
gem.homepage = "http://github.com/mbleigh/mustache-json"
|
12
|
+
gem.authors = ["Michael Bleigh"]
|
13
|
+
gem.add_dependency "mustache"
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "mustache-json #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'mustache'
|
2
|
+
|
3
|
+
class Mustache
|
4
|
+
# An array of methods that should be used for serialization.
|
5
|
+
# By default it is all public instance methods defined on the
|
6
|
+
# specific class (no superclasses). Override this for more
|
7
|
+
# specific control.
|
8
|
+
def self.serializable_methods
|
9
|
+
public_instance_methods(false)
|
10
|
+
end
|
11
|
+
|
12
|
+
# A hash of the compiled Mustache, including all
|
13
|
+
# serializable methods as well as the specified context.
|
14
|
+
def serializable_hash
|
15
|
+
hash = self.class.serializable_methods.inject({}) do |result, method|
|
16
|
+
# Symbolize the method to work better with the Mustache Context.
|
17
|
+
result[method.to_sym] = self.send(method)
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
hash.merge!(context)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Convert the current Mustache object to JSON and
|
25
|
+
# provide optional additional context for the result.
|
26
|
+
def to_json(additional_context = {})
|
27
|
+
context.merge(additional_context)
|
28
|
+
Mustache::JSON.encode(serializable_hash)
|
29
|
+
end
|
30
|
+
|
31
|
+
module JSON
|
32
|
+
def self.backend #:nodoc:
|
33
|
+
self.backend = :json_gem unless defined?(@backend)
|
34
|
+
@backend
|
35
|
+
end
|
36
|
+
|
37
|
+
# Set the back-end for the JSON encoder in a swappable fashion.
|
38
|
+
# Currently supported backends:
|
39
|
+
#
|
40
|
+
# * <tt>:json_gem</tt>
|
41
|
+
# * <tt>:json_pure</tt>
|
42
|
+
# * <tt>:active_support</tt>
|
43
|
+
# * <tt>:yajl</tt>
|
44
|
+
#
|
45
|
+
# The default backend is the JSON gem.
|
46
|
+
def self.backend=(backend)
|
47
|
+
require "mustache/json/backends/#{backend.to_s.downcase}.rb"
|
48
|
+
@backend = class_for(backend)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generic JSON encoder that will use the specified back-end.
|
52
|
+
def self.encode(object)
|
53
|
+
self.backend.encode(object)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.class_for(backend)#:nodoc:
|
57
|
+
class_name = backend.to_s.split('_').map(&:capitalize).join('')
|
58
|
+
eval "Mustache::JSON::Backends::#{class_name}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Mustache do
|
4
|
+
class MustachioedPerson < Mustache
|
5
|
+
def first_name
|
6
|
+
"Bob"
|
7
|
+
end
|
8
|
+
|
9
|
+
def last_name
|
10
|
+
"Stevens"
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def dont_show
|
16
|
+
"secrets"
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def really_secret
|
22
|
+
"bad stuff"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#serializable_hash' do
|
27
|
+
before do
|
28
|
+
@stache = MustachioedPerson.new
|
29
|
+
@hash = @stache.serializable_hash
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should include publicly declared methods' do
|
33
|
+
@hash[:first_name].should == 'Bob'
|
34
|
+
@hash[:last_name].should == 'Stevens'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should not include protected or private methods' do
|
38
|
+
@hash.keys.should_not be_include(:dont_show)
|
39
|
+
@hash.keys.should_not be_include(:really_secret)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should include any additional context' do
|
43
|
+
@stache.context.update(:foo => 'bar')
|
44
|
+
@stache.serializable_hash[:foo].should == 'bar'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.serializable_methods' do
|
49
|
+
it 'should be the public instance methods of the specific class' do
|
50
|
+
MustachioedPerson.serializable_methods.should == MustachioedPerson.public_instance_methods(false)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#to_json' do
|
55
|
+
before do
|
56
|
+
@stache = MustachioedPerson.new
|
57
|
+
end
|
58
|
+
|
59
|
+
[:json_gem, :json_pure, :yajl, :active_support].each do |backend|
|
60
|
+
describe " with the #{backend} backend" do
|
61
|
+
before do
|
62
|
+
Mustache::JSON.backend = backend
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should call the appropriate backend' do
|
66
|
+
Mustache::JSON.class_for(backend).should_receive(:encode)
|
67
|
+
@stache.to_json
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should include expected values' do
|
71
|
+
@stache.to_json.should be_include('"first_name":"Bob"')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mustache_json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Bleigh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-21 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mustache
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.9
|
34
|
+
version:
|
35
|
+
description: Easily serialize Mustaches to JSON.
|
36
|
+
email: michael@intridea.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/mustache/json/backends/active_support.rb
|
52
|
+
- lib/mustache/json/backends/json_gem.rb
|
53
|
+
- lib/mustache/json/backends/json_pure.rb
|
54
|
+
- lib/mustache/json/backends/yajl.rb
|
55
|
+
- lib/mustache_json.rb
|
56
|
+
- spec/mustache_json_spec.rb
|
57
|
+
- spec/spec.opts
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/mbleigh/mustache-json
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --charset=UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.5
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Easily serialize Mustaches to JSON.
|
87
|
+
test_files:
|
88
|
+
- spec/mustache_json_spec.rb
|
89
|
+
- spec/spec_helper.rb
|