jcompiler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9fafa319062e473193f72cb981c76accdcf02e19
4
+ data.tar.gz: 79060be58ef6ca8430c42cc379844ded4f25a193
5
+ SHA512:
6
+ metadata.gz: d2632c6ac79c2593d668f9fa1580bed3222ea9f90f9232c9ec794f62b60e4c3a30f911fb126566ccdefa34735ae09a781be5bc97ee4743489e163b1d6f7d1634
7
+ data.tar.gz: 98b827c4886357a7499c7466fde2c9133749f7b3ea4e6e0f6852edb0ceaf7a8c72f59c3fe087a20bdbd4ff8b38e3f032eea6ccc68169e40fbc1302d488cf2d2a
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jcompiler (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ jcompiler!
@@ -0,0 +1,242 @@
1
+ # Jbuilder
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
+ ```
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'jcompiler'
3
+ s.version = '0.1.0'
4
+ s.authors = ['Tetsuri Moriya']
5
+ s.email = ['tetsuri.moriya@gmail.com']
6
+ s.summary = 'High performance JSON view'
7
+ s.description = 'High performance JSON view via compiled DSL'
8
+ s.homepage = 'https://bitbucket.org/pandora2000/jcompiler'
9
+ s.license = 'MIT'
10
+ s.files = `git ls-files`.split("\n")
11
+ end
@@ -0,0 +1,10 @@
1
+ class Jcompiler
2
+ cattr_accessor :default_format
3
+ self.default_format = Mime::JSON
4
+
5
+ def self.call(template)
6
+ "{\"a\":\"b\"}"
7
+ end
8
+ end
9
+
10
+ ActionView::Template.register_template_handler :jcompiler, Jcompiler
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcompiler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tetsuri Moriya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: High performance JSON view via compiled DSL
14
+ email:
15
+ - tetsuri.moriya@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - README.md
24
+ - jcompiler.gemspec
25
+ - lib/jcompiler.rb
26
+ homepage: https://bitbucket.org/pandora2000/jcompiler
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.2.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: High performance JSON view
50
+ test_files: []
51
+ has_rdoc: