to_jbuilder 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +32 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/gemfiles/activerecord_32.gemfile +22 -0
- data/gemfiles/activerecord_40.gemfile +22 -0
- data/gemfiles/activerecord_41.gemfile +22 -0
- data/gemfiles/activerecord_42.gemfile +22 -0
- data/gemfiles/activerecord_edge.gemfile +26 -0
- data/lib/to_jbuilder/emitter.rb +183 -0
- data/lib/to_jbuilder/version.rb +3 -0
- data/lib/to_jbuilder.rb +50 -0
- data/to_jbuilder.gemspec +34 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8fea4b1d3823b9fbf79d2089bcbeec4a26b282bd
|
4
|
+
data.tar.gz: 76c6f2cab9f79c056cf8252f2c5abba914f451b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b476579a67244808eb995e706398825d0f2ade2bb10c033afefc6bbadc1a361f07c7f70d609b7ba2f78a8705366e4e914edc808a86de1c9812599eaa68dcf6fd
|
7
|
+
data.tar.gz: c334307a7ceeb1c672225be66194d0447f2118a63111e02998c1c7712d7b45372d7919bf6f304d7e449e6e117f98d55b2d994f1223b2bb660dc8d0123f63b8ca
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
language: ruby
|
2
|
+
script: bundle exec rake test
|
3
|
+
cache: bundler
|
4
|
+
sudo: false
|
5
|
+
|
6
|
+
before_install:
|
7
|
+
- gem i bundler -v=1.9.4
|
8
|
+
|
9
|
+
rvm:
|
10
|
+
- 2.0.0
|
11
|
+
- 2.1
|
12
|
+
- 2.2
|
13
|
+
- ruby-head
|
14
|
+
- jruby-head
|
15
|
+
- rbx-2.4.1
|
16
|
+
- rbx-2.5.2
|
17
|
+
|
18
|
+
gemfile:
|
19
|
+
- gemfiles/activerecord_32.gemfile
|
20
|
+
- gemfiles/activerecord_40.gemfile
|
21
|
+
- gemfiles/activerecord_41.gemfile
|
22
|
+
- gemfiles/activerecord_42.gemfile
|
23
|
+
- gemfiles/activerecord_edge.gemfile
|
24
|
+
|
25
|
+
matrix:
|
26
|
+
fast_finish: true
|
27
|
+
allow_failures:
|
28
|
+
- rvm: ruby-head
|
29
|
+
- rvm: jruby-head
|
30
|
+
- rvm: rbx-2.4.1
|
31
|
+
- gemfile: gemfiles/activerecord_32.gemfile
|
32
|
+
- gemfile: gemfiles/activerecord_edge.gemfile
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Yuki Nishijima
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# ToJbuilder
|
2
|
+
|
3
|
+
Convet any JSON data to jbuilder templates!
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'to_jbuilder'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install to_jbuilder
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'json'
|
25
|
+
require 'to_jbuilder'
|
26
|
+
|
27
|
+
{
|
28
|
+
content: "<p>This is <i>serious</i> monkey business</p>",
|
29
|
+
created_at: "2011-10-29T20:45:28-05:00",
|
30
|
+
updated_at: "2011-10-29T20:45:28-05:00",
|
31
|
+
|
32
|
+
author: {
|
33
|
+
name: "Yuki Nishijima",
|
34
|
+
email_address: "'Yuki Nishijima' <mail@yukinishijima.net>",
|
35
|
+
url: "http://www.yukinishijima.net"
|
36
|
+
},
|
37
|
+
|
38
|
+
visitors: 15,
|
39
|
+
|
40
|
+
comments: [
|
41
|
+
{ content: "Hello everyone!", created_at: "2011-10-29T20:45:28-05:00" },
|
42
|
+
{ content: "To you my good sir!", created_at: "2011-10-29T20:47:28-05:00" }
|
43
|
+
],
|
44
|
+
|
45
|
+
attachments: [
|
46
|
+
{ filename: "forecast.xls", url: "http://example.com/downloads/forecast.xls" },
|
47
|
+
{ filename: "presentation.pdf", url: "http://example.com/downloads/presentation.pdf" }
|
48
|
+
]
|
49
|
+
}.to_jbuilder(:message)
|
50
|
+
```
|
51
|
+
|
52
|
+
will generate:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
json.content @message.content
|
56
|
+
json.created_at @message.created_at
|
57
|
+
json.updated_at @message.updated_at
|
58
|
+
|
59
|
+
json.author do
|
60
|
+
json.name @author.name
|
61
|
+
json.email_address @author.email_address
|
62
|
+
json.url @author.url
|
63
|
+
end
|
64
|
+
|
65
|
+
json.visitors @message.visitors
|
66
|
+
|
67
|
+
json.comments @comments do |comment|
|
68
|
+
json.content comment.content
|
69
|
+
json.created_at comment.created_at
|
70
|
+
end
|
71
|
+
|
72
|
+
json.attachments @attachments do |attachment|
|
73
|
+
json.filename attachment.filename
|
74
|
+
json.url attachment.url
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
1. Fork it ( https://github.com/[my-github-username]/to_jbuilder/fork )
|
81
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
82
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
83
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
84
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "to_jbuilder"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "activerecord", "~> 3.2.0"
|
6
|
+
|
7
|
+
platforms :ruby do
|
8
|
+
gem "sqlite3"
|
9
|
+
end
|
10
|
+
|
11
|
+
platforms :jruby do
|
12
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
13
|
+
end
|
14
|
+
|
15
|
+
platforms :rbx do
|
16
|
+
gem "rubysl", "~> 2.0"
|
17
|
+
gem "racc"
|
18
|
+
gem "rubinius-developer_tools"
|
19
|
+
gem "rubysl-yaml", "2.0.4"
|
20
|
+
end
|
21
|
+
|
22
|
+
gemspec :path => "../"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "activerecord", "~> 4.0.0"
|
6
|
+
|
7
|
+
platforms :ruby do
|
8
|
+
gem "sqlite3"
|
9
|
+
end
|
10
|
+
|
11
|
+
platforms :jruby do
|
12
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
13
|
+
end
|
14
|
+
|
15
|
+
platforms :rbx do
|
16
|
+
gem "rubysl", "~> 2.0"
|
17
|
+
gem "racc"
|
18
|
+
gem "rubinius-developer_tools"
|
19
|
+
gem "rubysl-yaml", "2.0.4"
|
20
|
+
end
|
21
|
+
|
22
|
+
gemspec :path => "../"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "activerecord", "~> 4.1.0"
|
6
|
+
|
7
|
+
platforms :ruby do
|
8
|
+
gem "sqlite3"
|
9
|
+
end
|
10
|
+
|
11
|
+
platforms :jruby do
|
12
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
13
|
+
end
|
14
|
+
|
15
|
+
platforms :rbx do
|
16
|
+
gem "rubysl", "~> 2.0"
|
17
|
+
gem "racc"
|
18
|
+
gem "rubinius-developer_tools"
|
19
|
+
gem "rubysl-yaml", "2.0.4"
|
20
|
+
end
|
21
|
+
|
22
|
+
gemspec :path => "../"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "activerecord", "~> 4.2.0"
|
6
|
+
|
7
|
+
platforms :ruby do
|
8
|
+
gem "sqlite3"
|
9
|
+
end
|
10
|
+
|
11
|
+
platforms :jruby do
|
12
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
13
|
+
end
|
14
|
+
|
15
|
+
platforms :rbx do
|
16
|
+
gem "rubysl", "~> 2.0"
|
17
|
+
gem "racc"
|
18
|
+
gem "rubinius-developer_tools"
|
19
|
+
gem "rubysl-yaml", "2.0.4"
|
20
|
+
end
|
21
|
+
|
22
|
+
gemspec :path => "../"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
git "git://github.com/rails/rails.git" do
|
6
|
+
gem "activerecord", :require => "activerecord"
|
7
|
+
end
|
8
|
+
|
9
|
+
gem "arel", :github => "rails/arel"
|
10
|
+
|
11
|
+
platforms :ruby do
|
12
|
+
gem "sqlite3"
|
13
|
+
end
|
14
|
+
|
15
|
+
platforms :jruby do
|
16
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
17
|
+
end
|
18
|
+
|
19
|
+
platforms :rbx do
|
20
|
+
gem "rubysl", "~> 2.0"
|
21
|
+
gem "racc"
|
22
|
+
gem "rubinius-developer_tools"
|
23
|
+
gem "rubysl-yaml", "2.0.4"
|
24
|
+
end
|
25
|
+
|
26
|
+
gemspec :path => "../"
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'psych'
|
2
|
+
|
3
|
+
module ToJbuilder
|
4
|
+
module Visitors
|
5
|
+
class Emitter < Psych::Visitors::Emitter
|
6
|
+
def initialize(io, resource_name)
|
7
|
+
@handler = ToJbuilder::Emitter.new(io, resource_name)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Emitter < ::Psych::Emitter
|
13
|
+
DOCUMENT_START = :document_start
|
14
|
+
DOCUMENT_END = :document_end
|
15
|
+
PAIR_KEY = :pair_key
|
16
|
+
PAIR_VALUE = :pair_value
|
17
|
+
HASH_START = :hash_start
|
18
|
+
HASH_END = :hash_end
|
19
|
+
ARRAY_START = :array_start
|
20
|
+
ARRAY_END = :array_end
|
21
|
+
|
22
|
+
EMPTY = ' '.freeze
|
23
|
+
|
24
|
+
class JbuilderLine < Struct.new(:object, :key, :value, :prefix, :indent, :line_break)
|
25
|
+
alias line_break? line_break
|
26
|
+
|
27
|
+
def to_jbuilder(key_offset = 0)
|
28
|
+
"#{("\n" if line_break?)}#{indent}json.#{key.ljust(key_offset)} #{prefix}#{object}.#{key}\n"
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_hash_key
|
32
|
+
"#{("\n" if line_break?)}#{indent}json.#{key} do\n"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class JbuilderArray < Struct.new(:line, :key)
|
37
|
+
def to_jbuilder
|
38
|
+
if key
|
39
|
+
"json.array! @#{key} do |#{key.singularize}|\n"
|
40
|
+
else
|
41
|
+
"#{("\n" if line.line_break?)}#{line.indent}json.#{line.key} @#{line.key} do |#{line.key.singularize}|\n"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Nesting < Struct.new(:name, :type)
|
47
|
+
end
|
48
|
+
|
49
|
+
def initialize(io, resource_name)
|
50
|
+
@io, @resource_name = io, resource_name
|
51
|
+
|
52
|
+
@sequence_processed = false
|
53
|
+
@jbuilder_array = nil
|
54
|
+
@jbuilder_lines = []
|
55
|
+
@nesting = [Nesting.new(@resource_name, :root)]
|
56
|
+
@trace = []
|
57
|
+
end
|
58
|
+
|
59
|
+
def end_stream
|
60
|
+
# nothing to do...
|
61
|
+
end
|
62
|
+
|
63
|
+
def start_document(version, tag_directives, implicit)
|
64
|
+
raise "#{__method__} was called more than once." if @trace.include?(DOCUMENT_START)
|
65
|
+
@trace << DOCUMENT_START
|
66
|
+
end
|
67
|
+
|
68
|
+
def end_document(implicit_end)
|
69
|
+
if @trace.include?(DOCUMENT_END)
|
70
|
+
raise "#{__method__} was called more than once."
|
71
|
+
else
|
72
|
+
@trace << DOCUMENT_END
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def start_mapping(anchor, tag, implicit, style)
|
77
|
+
case @trace.last
|
78
|
+
when ARRAY_START
|
79
|
+
flush!
|
80
|
+
|
81
|
+
@io.write "\n" if @trace[-3] != ARRAY_END
|
82
|
+
@io.write @jbuilder_array.to_jbuilder
|
83
|
+
when PAIR_KEY
|
84
|
+
return if @sequence_processed
|
85
|
+
|
86
|
+
line = @jbuilder_lines.pop
|
87
|
+
flush!
|
88
|
+
line.line_break = true if @trace[-2] != HASH_START
|
89
|
+
@io.write line.to_hash_key
|
90
|
+
@nesting.push Nesting.new(line.key.singularize, HASH_START)
|
91
|
+
end
|
92
|
+
|
93
|
+
@trace << HASH_START
|
94
|
+
end
|
95
|
+
|
96
|
+
def end_mapping
|
97
|
+
flush!
|
98
|
+
@sequence_processed = true if within_array? && !@sequence_processed
|
99
|
+
|
100
|
+
if @nesting.size > 1 && !within_array?
|
101
|
+
@nesting.pop
|
102
|
+
@io.write "#{indent}end\n"
|
103
|
+
end
|
104
|
+
|
105
|
+
@trace << HASH_END
|
106
|
+
end
|
107
|
+
|
108
|
+
def start_sequence(anchor, tag, implicit, style)
|
109
|
+
case @trace.last
|
110
|
+
when PAIR_KEY
|
111
|
+
line = @jbuilder_lines.pop
|
112
|
+
line.line_break = false if @trace[-2] == HASH_END
|
113
|
+
|
114
|
+
@jbuilder_array = JbuilderArray.new(line)
|
115
|
+
when DOCUMENT_START
|
116
|
+
@jbuilder_array = JbuilderArray.new(nil, @nesting.last.name)
|
117
|
+
end
|
118
|
+
|
119
|
+
@nesting.push Nesting.new((@jbuilder_array.line || @jbuilder_array).key.singularize, ARRAY_START)
|
120
|
+
@trace << ARRAY_START
|
121
|
+
end
|
122
|
+
|
123
|
+
def end_sequence
|
124
|
+
case @trace.last
|
125
|
+
when PAIR_VALUE
|
126
|
+
@nesting.pop
|
127
|
+
@jbuilder_lines << @jbuilder_array.line
|
128
|
+
when HASH_END
|
129
|
+
@nesting.pop
|
130
|
+
@io.write "#{indent}end\n"
|
131
|
+
end
|
132
|
+
|
133
|
+
@sequence_processed = false
|
134
|
+
@trace << ARRAY_END
|
135
|
+
end
|
136
|
+
|
137
|
+
def scalar(value, anchor, tag, plain, quoted, style)
|
138
|
+
return if @sequence_processed
|
139
|
+
|
140
|
+
case @trace.last
|
141
|
+
when DOCUMENT_START, HASH_START, HASH_END, ARRAY_END, PAIR_VALUE
|
142
|
+
line_break = @trace.last == ARRAY_END || @trace.last == HASH_END
|
143
|
+
prefix = within_array? ? "" : "@"
|
144
|
+
|
145
|
+
@jbuilder_lines << JbuilderLine.new(@nesting.last.name, value, nil, prefix, indent, line_break)
|
146
|
+
@trace << PAIR_KEY
|
147
|
+
when ARRAY_START
|
148
|
+
@sequence_processed = true
|
149
|
+
|
150
|
+
@trace << PAIR_VALUE
|
151
|
+
when PAIR_KEY
|
152
|
+
@jbuilder_lines.last.value = value
|
153
|
+
|
154
|
+
@trace << PAIR_VALUE
|
155
|
+
else
|
156
|
+
raise "Syntax error."
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def alias(anchor)
|
161
|
+
# nothing to do...
|
162
|
+
end
|
163
|
+
|
164
|
+
private
|
165
|
+
|
166
|
+
def within_array?
|
167
|
+
@nesting.last.type == ARRAY_START
|
168
|
+
end
|
169
|
+
|
170
|
+
def indent
|
171
|
+
EMPTY * (@nesting.size - 1) * 2
|
172
|
+
end
|
173
|
+
|
174
|
+
def flush!
|
175
|
+
key_length = @jbuilder_lines.map(&:key).map(&:length).sort.last
|
176
|
+
|
177
|
+
@jbuilder_lines.each do |line|
|
178
|
+
@io.write line.to_jbuilder(key_length)
|
179
|
+
end
|
180
|
+
@jbuilder_lines.clear
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
data/lib/to_jbuilder.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "to_jbuilder/version"
|
2
|
+
require "to_jbuilder/emitter"
|
3
|
+
|
4
|
+
require "active_support/concern"
|
5
|
+
require 'active_support/core_ext/string/inflections'
|
6
|
+
|
7
|
+
module ToJbuilder
|
8
|
+
module CoreExtension
|
9
|
+
prepend_features Array
|
10
|
+
prepend_features Hash
|
11
|
+
|
12
|
+
def to_jbuilder(key)
|
13
|
+
parser = Psych::Parser.new(Psych::JSON::TreeBuilder.new).parse(to_json)
|
14
|
+
io = StringIO.new(''.encode('utf-8'))
|
15
|
+
|
16
|
+
ToJbuilder::Visitors::Emitter.new(io, key.to_s).accept(parser.handler.root)
|
17
|
+
io.string.tap(&:strip!)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.model_names
|
22
|
+
@model_names ||= if defined?(ActiveRecord::Base)
|
23
|
+
ActiveRecord::Base.descendants.map(&:to_s).reject{|d| d.include?("HABTM") }.map(&:underscore)
|
24
|
+
else
|
25
|
+
[]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module TemplateGenerator
|
30
|
+
extend ActiveSupport::Concern
|
31
|
+
|
32
|
+
included do
|
33
|
+
after_action do
|
34
|
+
views_path = Rails.root.join('app/views', params[:controller]).to_s
|
35
|
+
template_path = Rails.root.join(views_path, params[:action]).to_s + ".json.jbuilder"
|
36
|
+
|
37
|
+
if response.successful? && response.body.present?
|
38
|
+
json = JSON.parse(response.body)
|
39
|
+
context = params[:controller].split("/").last
|
40
|
+
context = context.singularize if params[:action] != "index"
|
41
|
+
|
42
|
+
FileUtils.mkdir_p(views_path)
|
43
|
+
File.open(template_path, "w") do |file|
|
44
|
+
file.write json.to_jbuilder(context)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/to_jbuilder.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'to_jbuilder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "to_jbuilder"
|
8
|
+
spec.version = ToJbuilder::VERSION
|
9
|
+
spec.authors = ["Yuki Nishijima"]
|
10
|
+
spec.email = ["mail@yukinishijima.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Convert JSON to jbuilder templates}
|
13
|
+
spec.description = %q{Easily convert Ruby array/hash by just calling the #to_jbuilder method.}
|
14
|
+
spec.homepage = "https://github.com/yuki24/to_jbuilder"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "activesupport", ">= 3.0.0"
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_jbuilder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuki Nishijima
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: 'Easily convert Ruby array/hash by just calling the #to_jbuilder method.'
|
56
|
+
email:
|
57
|
+
- mail@yukinishijima.net
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- gemfiles/activerecord_32.gemfile
|
72
|
+
- gemfiles/activerecord_40.gemfile
|
73
|
+
- gemfiles/activerecord_41.gemfile
|
74
|
+
- gemfiles/activerecord_42.gemfile
|
75
|
+
- gemfiles/activerecord_edge.gemfile
|
76
|
+
- lib/to_jbuilder.rb
|
77
|
+
- lib/to_jbuilder/emitter.rb
|
78
|
+
- lib/to_jbuilder/version.rb
|
79
|
+
- to_jbuilder.gemspec
|
80
|
+
homepage: https://github.com/yuki24/to_jbuilder
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata:
|
84
|
+
allowed_push_host: https://rubygems.org
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.4.6
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Convert JSON to jbuilder templates
|
105
|
+
test_files: []
|