jcompiler 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -242
- data/jcompiler.gemspec +1 -1
- data/lib/jcompiler.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b586684703abdda10685227528d375362f8065c
|
4
|
+
data.tar.gz: d71fb9d5b6e91a967e8f6a8b0d75776c446d787f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d137b43a2fd040680ad97cbecb4d82db79fbb6eaf698fc1fbd59a7c6ec26226936de06aa4b0640099eda8381275b13b34d9768c80b628205dae4f613353dd503
|
7
|
+
data.tar.gz: 48e7d14b77471c3293a9e1098c104492ed73e110d6e81ec0f90ab1e7bfd3faf6cb73ef71ed9e5536420759bd307ef6b953e4452fbd4ec5d3444de1510d130ade
|
data/README.md
CHANGED
@@ -1,242 +1 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
[![Build Status](https://api.travis-ci.org/rails/jbuilder.svg)][travis]
|
4
|
-
[![Gem Version](http://img.shields.io/gem/v/jbuilder.svg)][gem]
|
5
|
-
[![Code Climate](http://img.shields.io/codeclimate/github/rails/jbuilder.svg)][codeclimate]
|
6
|
-
[![Dependencies Status](http://img.shields.io/gemnasium/rails/jbuilder.svg)][gemnasium]
|
7
|
-
|
8
|
-
[travis]: https://travis-ci.org/rails/jbuilder
|
9
|
-
[gem]: https://rubygems.org/gems/jbuilder
|
10
|
-
[codeclimate]: https://codeclimate.com/github/rails/jbuilder
|
11
|
-
[gemnasium]: https://gemnasium.com/rails/jbuilder
|
12
|
-
|
13
|
-
Jbuilder gives you a simple DSL for declaring JSON structures that beats
|
14
|
-
massaging giant hash structures. This is particularly helpful when the
|
15
|
-
generation process is fraught with conditionals and loops. Here's a simple
|
16
|
-
example:
|
17
|
-
|
18
|
-
``` ruby
|
19
|
-
Jbuilder.encode do |json|
|
20
|
-
json.content format_content(@message.content)
|
21
|
-
json.(@message, :created_at, :updated_at)
|
22
|
-
|
23
|
-
json.author do
|
24
|
-
json.name @message.creator.name.familiar
|
25
|
-
json.email_address @message.creator.email_address_with_name
|
26
|
-
json.url url_for(@message.creator, format: :json)
|
27
|
-
end
|
28
|
-
|
29
|
-
if current_user.admin?
|
30
|
-
json.visitors calculate_visitors(@message)
|
31
|
-
end
|
32
|
-
|
33
|
-
json.comments @message.comments, :content, :created_at
|
34
|
-
|
35
|
-
json.attachments @message.attachments do |attachment|
|
36
|
-
json.filename attachment.filename
|
37
|
-
json.url url_for(attachment)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
```
|
41
|
-
|
42
|
-
This will build the following structure:
|
43
|
-
|
44
|
-
``` javascript
|
45
|
-
{
|
46
|
-
"content": "<p>This is <i>serious</i> monkey business</p>",
|
47
|
-
"created_at": "2011-10-29T20:45:28-05:00",
|
48
|
-
"updated_at": "2011-10-29T20:45:28-05:00",
|
49
|
-
|
50
|
-
"author": {
|
51
|
-
"name": "David H.",
|
52
|
-
"email_address": "'David Heinemeier Hansson' <david@heinemeierhansson.com>",
|
53
|
-
"url": "http://example.com/users/1-david.json"
|
54
|
-
},
|
55
|
-
|
56
|
-
"visitors": 15,
|
57
|
-
|
58
|
-
"comments": [
|
59
|
-
{ "content": "Hello everyone!", "created_at": "2011-10-29T20:45:28-05:00" },
|
60
|
-
{ "content": "To you my good sir!", "created_at": "2011-10-29T20:47:28-05:00" }
|
61
|
-
],
|
62
|
-
|
63
|
-
"attachments": [
|
64
|
-
{ "filename": "forecast.xls", "url": "http://example.com/downloads/forecast.xls" },
|
65
|
-
{ "filename": "presentation.pdf", "url": "http://example.com/downloads/presentation.pdf" }
|
66
|
-
]
|
67
|
-
}
|
68
|
-
```
|
69
|
-
|
70
|
-
To define attribute and structure names dynamically, use the `set!` method:
|
71
|
-
|
72
|
-
``` ruby
|
73
|
-
json.set! :author do
|
74
|
-
json.set! :name, 'David'
|
75
|
-
end
|
76
|
-
|
77
|
-
# => "author": { "name": "David" }
|
78
|
-
```
|
79
|
-
|
80
|
-
Top level arrays can be handled directly. Useful for index and other collection actions.
|
81
|
-
|
82
|
-
``` ruby
|
83
|
-
# @people = People.all
|
84
|
-
json.array! @people do |person|
|
85
|
-
json.name person.name
|
86
|
-
json.age calculate_age(person.birthday)
|
87
|
-
end
|
88
|
-
|
89
|
-
# => [ { "name": "David", "age": 32 }, { "name": "Jamie", "age": 31 } ]
|
90
|
-
```
|
91
|
-
|
92
|
-
You can also extract attributes from array directly.
|
93
|
-
|
94
|
-
``` ruby
|
95
|
-
# @people = People.all
|
96
|
-
json.array! @people, :id, :name
|
97
|
-
|
98
|
-
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
|
99
|
-
```
|
100
|
-
|
101
|
-
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
102
|
-
|
103
|
-
``` ruby
|
104
|
-
class Person
|
105
|
-
# ... Class Definition ... #
|
106
|
-
def to_builder
|
107
|
-
Jbuilder.new do |person|
|
108
|
-
person.(self, :name, :age)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
class Company
|
114
|
-
# ... Class Definition ... #
|
115
|
-
def to_builder
|
116
|
-
Jbuilder.new do |company|
|
117
|
-
company.name name
|
118
|
-
company.president president.to_builder
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
company = Company.new('Doodle Corp', Person.new('John Stobs', 58))
|
124
|
-
company.to_builder.target!
|
125
|
-
|
126
|
-
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
|
127
|
-
```
|
128
|
-
|
129
|
-
You can either use Jbuilder stand-alone or directly as an ActionView template
|
130
|
-
language. When required in Rails, you can create views ala show.json.jbuilder
|
131
|
-
(the json is already yielded):
|
132
|
-
|
133
|
-
``` ruby
|
134
|
-
# Any helpers available to views are available to the builder
|
135
|
-
json.content format_content(@message.content)
|
136
|
-
json.(@message, :created_at, :updated_at)
|
137
|
-
|
138
|
-
json.author do
|
139
|
-
json.name @message.creator.name.familiar
|
140
|
-
json.email_address @message.creator.email_address_with_name
|
141
|
-
json.url url_for(@message.creator, format: :json)
|
142
|
-
end
|
143
|
-
|
144
|
-
if current_user.admin?
|
145
|
-
json.visitors calculate_visitors(@message)
|
146
|
-
end
|
147
|
-
```
|
148
|
-
|
149
|
-
|
150
|
-
You can use partials as well. The following will render the file
|
151
|
-
`views/comments/_comments.json.jbuilder`, and set a local variable
|
152
|
-
`comments` with all this message's comments, which you can use inside
|
153
|
-
the partial.
|
154
|
-
|
155
|
-
```ruby
|
156
|
-
json.partial! 'comments/comments', comments: @message.comments
|
157
|
-
```
|
158
|
-
|
159
|
-
It's also possible to render collections of partials:
|
160
|
-
|
161
|
-
```ruby
|
162
|
-
json.array! @posts, partial: 'posts/post', as: :post
|
163
|
-
|
164
|
-
# or
|
165
|
-
|
166
|
-
json.partial! 'posts/post', collection: @posts, as: :post
|
167
|
-
|
168
|
-
# or
|
169
|
-
|
170
|
-
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
171
|
-
|
172
|
-
# or
|
173
|
-
|
174
|
-
json.comments @post.comments, partial: 'comment/comment', as: :comment
|
175
|
-
```
|
176
|
-
|
177
|
-
You can explicitly make Jbuilder object return null if you want:
|
178
|
-
|
179
|
-
``` ruby
|
180
|
-
json.extract! @post, :id, :title, :content, :published_at
|
181
|
-
json.author do
|
182
|
-
if @post.anonymous?
|
183
|
-
json.null! # or json.nil!
|
184
|
-
else
|
185
|
-
json.first_name @post.author_first_name
|
186
|
-
json.last_name @post.author_last_name
|
187
|
-
end
|
188
|
-
end
|
189
|
-
```
|
190
|
-
|
191
|
-
Fragment caching is supported, it uses `Rails.cache` and works like caching in
|
192
|
-
HTML templates:
|
193
|
-
|
194
|
-
```ruby
|
195
|
-
json.cache! ['v1', @person], expires_in: 10.minutes do
|
196
|
-
json.extract! @person, :name, :age
|
197
|
-
end
|
198
|
-
```
|
199
|
-
|
200
|
-
You can also conditionally cache a block by using `cache_if!` like this:
|
201
|
-
|
202
|
-
```ruby
|
203
|
-
json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
|
204
|
-
json.extract! @person, :name, :age
|
205
|
-
end
|
206
|
-
```
|
207
|
-
|
208
|
-
If you are rendering fragments for a collection of objects, have a look at
|
209
|
-
`jbuilder_cache_multi` gem. It uses fetch_multi (>= Rails 4.1) to fetch
|
210
|
-
mutliple keys at once.
|
211
|
-
|
212
|
-
Keys can be auto formatted using `key_format!`, this can be used to convert
|
213
|
-
keynames from the standard ruby_format to camelCase:
|
214
|
-
|
215
|
-
``` ruby
|
216
|
-
json.key_format! camelize: :lower
|
217
|
-
json.first_name 'David'
|
218
|
-
|
219
|
-
# => { "firstName": "David" }
|
220
|
-
```
|
221
|
-
|
222
|
-
You can set this globally with the class method `key_format` (from inside your
|
223
|
-
environment.rb for example):
|
224
|
-
|
225
|
-
``` ruby
|
226
|
-
Jbuilder.key_format camelize: :lower
|
227
|
-
```
|
228
|
-
|
229
|
-
Faster JSON backends
|
230
|
-
--------------------
|
231
|
-
|
232
|
-
Jbuilder uses MultiJson, which by default will use the JSON gem. That gem is
|
233
|
-
currently tangled with ActiveSupport's all-Ruby `#to_json` implementation,
|
234
|
-
which is slow (fixed in Rails >= 4.1). For faster Jbuilder rendering, you can
|
235
|
-
specify something like the Yajl JSON generator instead. You'll need to include
|
236
|
-
the `yajl-ruby` gem in your Gemfile and then set the following configuration
|
237
|
-
for MultiJson:
|
238
|
-
|
239
|
-
``` ruby
|
240
|
-
require 'multi_json'
|
241
|
-
MultiJson.use :yajl
|
242
|
-
```
|
1
|
+
# Jcompiler
|
data/jcompiler.gemspec
CHANGED
data/lib/jcompiler.rb
CHANGED