jbuilder 0.6.0 → 0.7.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/Gemfile.lock +1 -1
- data/jbuilder-0.6.0.gem +0 -0
- data/jbuilder.gemspec +1 -1
- data/lib/jbuilder.rb +37 -1
- data/test/jbuilder_test.rb +61 -0
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/jbuilder-0.6.0.gem
ADDED
Binary file
|
data/jbuilder.gemspec
CHANGED
data/lib/jbuilder.rb
CHANGED
@@ -2,7 +2,9 @@ require 'active_support/basic_object'
|
|
2
2
|
require 'active_support/ordered_hash'
|
3
3
|
require 'active_support/core_ext/array/access'
|
4
4
|
require 'active_support/core_ext/enumerable'
|
5
|
+
require 'active_support/core_ext/hash'
|
5
6
|
require 'active_support/json'
|
7
|
+
require 'active_support/cache'
|
6
8
|
require 'multi_json'
|
7
9
|
|
8
10
|
class Jbuilder < ActiveSupport::BasicObject
|
@@ -156,8 +158,18 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
156
158
|
# end
|
157
159
|
#
|
158
160
|
# { "people": [ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ] }
|
161
|
+
#
|
162
|
+
# If you omit the block then you can set the top level array directly:
|
163
|
+
#
|
164
|
+
# json.array! [1, 2, 3]
|
165
|
+
#
|
166
|
+
# [1,2,3]
|
159
167
|
def array!(collection)
|
160
|
-
@attributes =
|
168
|
+
@attributes = if block_given?
|
169
|
+
_map_collection(collection) { |element| yield self, element }
|
170
|
+
else
|
171
|
+
collection
|
172
|
+
end
|
161
173
|
end
|
162
174
|
|
163
175
|
# Extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON.
|
@@ -203,6 +215,30 @@ class Jbuilder < ActiveSupport::BasicObject
|
|
203
215
|
::MultiJson.encode @attributes
|
204
216
|
end
|
205
217
|
|
218
|
+
# Caches the json constructed within the block passed. Has the same signature as the `cache` helper
|
219
|
+
# method in `ActionView::Helpers::CacheHelper` and so can be used in the same way.
|
220
|
+
#
|
221
|
+
# Example:
|
222
|
+
#
|
223
|
+
# json.cache! ['v1', @person], :expires_in => 10.minutes do |json|
|
224
|
+
# json.extract! @person, :name, :age
|
225
|
+
# end
|
226
|
+
def cache!(key=nil, options={}, &block)
|
227
|
+
cache_key = ::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split("://").last : key, :jbuilder)
|
228
|
+
value = ::Rails.cache.fetch(cache_key, options) do
|
229
|
+
jb = ::Jbuilder.new
|
230
|
+
yield jb
|
231
|
+
jb.attributes!
|
232
|
+
end
|
233
|
+
|
234
|
+
if value.is_a?(::Array)
|
235
|
+
array! value
|
236
|
+
else
|
237
|
+
value.each do |k, v|
|
238
|
+
set! k, v
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
206
242
|
|
207
243
|
protected
|
208
244
|
def _set_value(key, value)
|
data/test/jbuilder_test.rb
CHANGED
@@ -1,9 +1,32 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'active_support/test_case'
|
3
|
+
require 'active_support/cache'
|
3
4
|
require 'active_support/inflector'
|
4
5
|
|
5
6
|
require 'jbuilder'
|
6
7
|
|
8
|
+
module Rails
|
9
|
+
class Cache
|
10
|
+
def initialize
|
11
|
+
@cache = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def write(k, v, opt={})
|
15
|
+
@cache[k] = v
|
16
|
+
end
|
17
|
+
|
18
|
+
def read(k, opt={})
|
19
|
+
@cache[k]
|
20
|
+
end
|
21
|
+
|
22
|
+
def fetch(k, opt={}, &block)
|
23
|
+
@cache[k] || @cache[k] = block.call
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.cache; @cache ||= Cache.new; end
|
28
|
+
end
|
29
|
+
|
7
30
|
class JbuilderTest < ActiveSupport::TestCase
|
8
31
|
test "single key" do
|
9
32
|
json = Jbuilder.encode do |json|
|
@@ -395,4 +418,42 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
395
418
|
|
396
419
|
assert_equal [], Jbuilder.send(:class_variable_get, "@@key_formatter").instance_variable_get("@cache").keys
|
397
420
|
end
|
421
|
+
|
422
|
+
test "fragment caching a JSON object" do
|
423
|
+
json = Jbuilder.encode do |json|
|
424
|
+
json.cache!("cachekey") do |json|
|
425
|
+
json.name "Cache"
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
Rails.cache.read("jbuilder/cachekey").tap do |parsed|
|
430
|
+
assert_equal "Cache", parsed['name']
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
test "fragment caching deserializes a JSON object" do
|
435
|
+
Rails.cache.write("jbuilder/cachekey", {'name' => "Something"})
|
436
|
+
json = Jbuilder.encode do |json|
|
437
|
+
json.cache!("cachekey") do |json|
|
438
|
+
json.name "Cache"
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
442
|
+
JSON.parse(json).tap do |parsed|
|
443
|
+
assert_equal "Something", parsed['name']
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
test "fragment caching deserializes an array" do
|
448
|
+
Rails.cache.write("jbuilder/cachekey", ["a", "b", "c"])
|
449
|
+
json = Jbuilder.encode do |json|
|
450
|
+
json.cache!("cachekey") do |json|
|
451
|
+
json.array! ['1', '2', '3']
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
JSON.parse(json).tap do |parsed|
|
456
|
+
assert_equal ["a", "b", "c"], parsed
|
457
|
+
end
|
458
|
+
end
|
398
459
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jbuilder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152334840 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152334840
|
25
25
|
description:
|
26
26
|
email: david@37signals.com
|
27
27
|
executables: []
|
@@ -30,6 +30,7 @@ extra_rdoc_files: []
|
|
30
30
|
files:
|
31
31
|
- ./Gemfile
|
32
32
|
- ./Gemfile.lock
|
33
|
+
- ./jbuilder-0.6.0.gem
|
33
34
|
- ./jbuilder.gemspec
|
34
35
|
- ./lib/jbuilder.rb
|
35
36
|
- ./lib/jbuilder_template.rb
|