ruby_template 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +1 -0
- data/lib/ruby_template.rb +2 -0
- data/lib/ruby_template/handler.rb +21 -0
- data/lib/ruby_template/version.rb +3 -0
- data/ruby_template.gemspec +22 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Scott Fleckenstein
|
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,81 @@
|
|
1
|
+
# RubyTemplate
|
2
|
+
|
3
|
+
RubyTemplate gives you a simple means of producing json in your views by letting you write normal ruby. The return value of your template will be passed through ActiveSupport::JSON.encode to produce the actual json.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ruby_template'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
The template users/show.json.rb:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
{
|
21
|
+
:id => user.id,
|
22
|
+
:name => user.name,
|
23
|
+
}
|
24
|
+
```
|
25
|
+
|
26
|
+
will become:
|
27
|
+
|
28
|
+
```json
|
29
|
+
{"user" : {"id" : 123, "name" : "Scott"}}
|
30
|
+
```
|
31
|
+
|
32
|
+
Simple as that.
|
33
|
+
|
34
|
+
### Partials
|
35
|
+
|
36
|
+
Partials work mostly as expected:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# in (users/show.json.rb)
|
40
|
+
render(:partial => "users/user", object: @user)
|
41
|
+
```
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# in (users/_user.json.rb)
|
45
|
+
{
|
46
|
+
:id => user.id,
|
47
|
+
:name => user.name,
|
48
|
+
}
|
49
|
+
```
|
50
|
+
|
51
|
+
Note: when rendering partials, the return value is not a string (as normal), but instead we pass up the return value of the template.
|
52
|
+
|
53
|
+
### Extending partials using deep_merge
|
54
|
+
|
55
|
+
It's nice to be DRY with your templates. You can use deep_merge to extend a common partial in contexts where extra information is needed. For example, say we want to include some private information after a user is created:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
# in (users/create.json.rb)
|
59
|
+
render(:partial => "users/user", object: @user).deep_merge({
|
60
|
+
:token => @user.login_token
|
61
|
+
})
|
62
|
+
```
|
63
|
+
|
64
|
+
### Collections
|
65
|
+
|
66
|
+
Rendering a partial with the collection option does not work. Instead, use:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# in (users/index.json.rb)
|
70
|
+
{
|
71
|
+
:users => @users.map{|u| render(:partial => "users/user", object: u)}
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
1. Fork it
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
|
4
|
+
module RubyTemplate
|
5
|
+
class Handler
|
6
|
+
class_attribute :default_format
|
7
|
+
self.default_format = Mime::JSON
|
8
|
+
|
9
|
+
def call(template)
|
10
|
+
unless File.basename(template.virtual_path).start_with?('_')
|
11
|
+
src = 'ActiveSupport::JSON.encode(code.call)'
|
12
|
+
else
|
13
|
+
src = "code.call"
|
14
|
+
end
|
15
|
+
|
16
|
+
"code = lambda{#{template.source}};" + "self.output_buffer = (#{src})"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ActionView::Template.register_template_handler :rb, RubyTemplate::Handler.new
|
@@ -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 'ruby_template/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ruby_template"
|
8
|
+
gem.version = RubyTemplate::VERSION
|
9
|
+
gem.authors = ["Scott Fleckenstein"]
|
10
|
+
gem.email = ["nullstyle@gmail.com"]
|
11
|
+
gem.description = %q{RubyTemplate gives you a simple means of producing json in your views by letting you write normal ruby. }
|
12
|
+
gem.summary = %q{Produce JSON views using pure-ruby}
|
13
|
+
gem.homepage = "https://github.com/nullstyle/ruby_template"
|
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_dependency("actionpack", '>= 3.0.0')
|
21
|
+
gem.add_dependency("activesupport", '>= 3.0.0')
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_template
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Scott Fleckenstein
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.0.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.0
|
46
|
+
description: ! 'RubyTemplate gives you a simple means of producing json in your views
|
47
|
+
by letting you write normal ruby. '
|
48
|
+
email:
|
49
|
+
- nullstyle@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/ruby_template.rb
|
60
|
+
- lib/ruby_template/handler.rb
|
61
|
+
- lib/ruby_template/version.rb
|
62
|
+
- ruby_template.gemspec
|
63
|
+
homepage: https://github.com/nullstyle/ruby_template
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.23
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Produce JSON views using pure-ruby
|
87
|
+
test_files: []
|
88
|
+
has_rdoc:
|