mold 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/.document +5 -0
- data/.gitignore +48 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.md +77 -0
- data/Rakefile +61 -0
- data/lib/mold.rb +2 -0
- data/lib/mold/handler.rb +45 -0
- data/lib/mold/version.rb +5 -0
- metadata +105 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
18
|
+
#
|
19
|
+
# * Create a file at ~/.gitignore
|
20
|
+
# * Include files you want ignored
|
21
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
22
|
+
#
|
23
|
+
# After doing this, these files will be ignored in all your git projects,
|
24
|
+
# saving you from having to 'pollute' every project you touch with them
|
25
|
+
#
|
26
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
27
|
+
#
|
28
|
+
# For MacOS:
|
29
|
+
#
|
30
|
+
#.DS_Store
|
31
|
+
|
32
|
+
# For TextMate
|
33
|
+
#*.tmproj
|
34
|
+
#tmtags
|
35
|
+
|
36
|
+
# For emacs:
|
37
|
+
#*~
|
38
|
+
#\#*
|
39
|
+
#.\#*
|
40
|
+
|
41
|
+
# For vim:
|
42
|
+
#*.swp
|
43
|
+
|
44
|
+
# For redcar:
|
45
|
+
#.redcar
|
46
|
+
|
47
|
+
# For rubinius:
|
48
|
+
#*.rbc
|
data/Gemfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Add dependencies to develop your gem here.
|
4
|
+
# Include everything needed to run rake, tests, features, etc.
|
5
|
+
group :development do
|
6
|
+
gem "shoulda", ">= 0"
|
7
|
+
gem "bundler", "~> 1.0.0"
|
8
|
+
gem "jeweler", "~> 1.6.4"
|
9
|
+
gem "rcov", ">= 0"
|
10
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.2)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.6.4)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Thiago Jackiw
|
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.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
## Overview ##
|
2
|
+
|
3
|
+
Mold is a view template handler for Rails that renders Ruby objects to JSON.
|
4
|
+
|
5
|
+
## Compatibility ##
|
6
|
+
|
7
|
+
Mold is compatible with Rails 2.x and Rails 3.x.
|
8
|
+
|
9
|
+
## Installation ##
|
10
|
+
|
11
|
+
Install Mold as a gem:
|
12
|
+
|
13
|
+
gem install mold
|
14
|
+
|
15
|
+
or add to your Gemfile:
|
16
|
+
|
17
|
+
gem 'mold'
|
18
|
+
|
19
|
+
## Usage ##
|
20
|
+
|
21
|
+
It's pretty straight-forward to create a Mold JSON template, just create the structure as you would in a Hash:
|
22
|
+
|
23
|
+
# app/controllers/users_controller.rb
|
24
|
+
class Users < ActionController:Base
|
25
|
+
def show
|
26
|
+
@user = User.first
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# app/views/users/show.json.mold
|
31
|
+
{
|
32
|
+
:user => {
|
33
|
+
:id => @user.id,
|
34
|
+
:name => @user.name
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
You can also create and use view partials in Mold:
|
39
|
+
|
40
|
+
# app/controllers/users_controller.rb
|
41
|
+
class Users < ActionController:Base
|
42
|
+
def index
|
43
|
+
@users = User.all
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# app/views/users/_user.json.mold
|
48
|
+
{
|
49
|
+
:user => {
|
50
|
+
:id => user.id,
|
51
|
+
:name => user.name
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
# app/views/users/index.json.mold
|
56
|
+
{
|
57
|
+
:users => @users.map{ |user| render(:partial => 'user', :object => user) }
|
58
|
+
}
|
59
|
+
|
60
|
+
Writing code in a Mold view also works:
|
61
|
+
|
62
|
+
# app/views/users/show.json.mold
|
63
|
+
time = Time.now
|
64
|
+
|
65
|
+
{
|
66
|
+
:user => {
|
67
|
+
:time => time
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
## Inspiration ##
|
72
|
+
|
73
|
+
This gem has been inspired mostly by [ruby\_template_handler](https://github.com/mschuerig/ruby_template_handler).
|
74
|
+
|
75
|
+
## Copyright ##
|
76
|
+
|
77
|
+
Copyright © 2011 Thiago Jackiw. See [License.txt](https://github.com/tjackiw/mold/License.txt) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
require 'jeweler'
|
14
|
+
|
15
|
+
$:.push File.expand_path("../lib", __FILE__)
|
16
|
+
require "mold/version"
|
17
|
+
|
18
|
+
Jeweler::Tasks.new do |gem|
|
19
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
20
|
+
gem.name = "mold"
|
21
|
+
gem.version = Mold::VERSION
|
22
|
+
gem.platform = Gem::Platform::RUBY
|
23
|
+
gem.authors = ["Thiago Jackiw"]
|
24
|
+
gem.email = "tjackiw@gmail.com"
|
25
|
+
gem.homepage = "http://github.com/tjackiw/mold"
|
26
|
+
gem.license = "MIT"
|
27
|
+
gem.summary = %Q{ Mold is a view template handler for Rails that renders Ruby objects to JSON. }
|
28
|
+
gem.description = %Q{ Mold is a view template handler for Rails that renders Ruby objects to JSON. }
|
29
|
+
|
30
|
+
gem.files = `git ls-files`.split("\n")
|
31
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
32
|
+
gem.require_paths = ["lib"]
|
33
|
+
end
|
34
|
+
Jeweler::RubygemsDotOrgTasks.new
|
35
|
+
|
36
|
+
require 'rake/testtask'
|
37
|
+
Rake::TestTask.new(:test) do |test|
|
38
|
+
test.libs << 'lib' << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
|
43
|
+
require 'rcov/rcovtask'
|
44
|
+
Rcov::RcovTask.new do |test|
|
45
|
+
test.libs << 'test'
|
46
|
+
test.pattern = 'test/**/test_*.rb'
|
47
|
+
test.verbose = true
|
48
|
+
test.rcov_opts << '--exclude "gems/*"'
|
49
|
+
end
|
50
|
+
|
51
|
+
task :default => :test
|
52
|
+
|
53
|
+
require 'rdoc/task'
|
54
|
+
RDoc::Task.new do |rdoc|
|
55
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
56
|
+
|
57
|
+
rdoc.rdoc_dir = 'rdoc'
|
58
|
+
rdoc.title = "mold #{version}"
|
59
|
+
rdoc.rdoc_files.include('README*')
|
60
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
61
|
+
end
|
data/lib/mold.rb
ADDED
data/lib/mold/handler.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Rails 2.X Template
|
2
|
+
if defined?(Rails) && Rails.version =~ /^2/
|
3
|
+
module Mold
|
4
|
+
class Handler < ActionView::TemplateHandler
|
5
|
+
include ActionView::TemplateHandlers::Compilable
|
6
|
+
|
7
|
+
def compile(template)
|
8
|
+
source = if File.basename(template.filename).starts_with?('_')
|
9
|
+
'code.call'
|
10
|
+
else
|
11
|
+
'ActiveSupport::JSON.encode(code.call)'
|
12
|
+
end
|
13
|
+
%{
|
14
|
+
controller.response.content_type = Mime::JSON
|
15
|
+
code = lambda{#{ template.source }}
|
16
|
+
self.output_buffer = (#{ src })
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
ActionView::Template.register_template_handler(:mold, Mold::Handler)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Rails 3.X Template
|
25
|
+
if defined?(Rails) && Rails.version =~ /^3/
|
26
|
+
module Mold
|
27
|
+
class Handler < ActionView::Template::Handler
|
28
|
+
|
29
|
+
self.default_format = Mime::JSON
|
30
|
+
|
31
|
+
def self.call(template)
|
32
|
+
source = if File.basename(template.identifier).starts_with?('_')
|
33
|
+
'code.call'
|
34
|
+
else
|
35
|
+
'ActiveSupport::JSON.encode(code.call)'
|
36
|
+
end
|
37
|
+
%{
|
38
|
+
code = lambda{#{ template.source }}
|
39
|
+
self.output_buffer = (#{ source })
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
ActionView::Template.register_template_handler(:mold, Mold::Handler)
|
45
|
+
end
|
data/lib/mold/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mold
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thiago Jackiw
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-05 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: shoulda
|
16
|
+
requirement: &70268907557060 !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: *70268907557060
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bundler
|
27
|
+
requirement: &70268907554540 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70268907554540
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: jeweler
|
38
|
+
requirement: &70268907552020 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.6.4
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70268907552020
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rcov
|
49
|
+
requirement: &70268907548560 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70268907548560
|
58
|
+
description: ! ' Mold is a view template handler for Rails that renders Ruby objects
|
59
|
+
to JSON. '
|
60
|
+
email: tjackiw@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files:
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
files:
|
67
|
+
- .document
|
68
|
+
- .gitignore
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/mold.rb
|
75
|
+
- lib/mold/handler.rb
|
76
|
+
- lib/mold/version.rb
|
77
|
+
homepage: http://github.com/tjackiw/mold
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
hash: -1770751982089159590
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.6
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Mold is a view template handler for Rails that renders Ruby objects to JSON.
|
105
|
+
test_files: []
|