jbuilder 2.0.8 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c6836a60ee9c7dd9709201026f7a6b472cc2019e
4
- data.tar.gz: 4a8069c61889a929e3ffa10b3d46b799399202a0
3
+ metadata.gz: a389ec3a309016718e77b531bb60b89aa18b5734
4
+ data.tar.gz: 0b749ea3555366e70c5f99d7697072e9f9badb27
5
5
  SHA512:
6
- metadata.gz: 774ae98b86547a5fd8261a3452e5ceb188924b9e84cf52d7c1f9c6e5f7ebfe6fb93816ef04b9f5f2aeb1f65d2136ed4d13b466957bd0cf65b3ead3a80515856f
7
- data.tar.gz: 4aeb49c248572ceedfac7fab0bfc3d763607ead19be5dda7362d93e1bd87ddb0a1ea8b47f846747c61afb13b358c7ddc772e8d22542863ddbb948a0781fc61dd
6
+ metadata.gz: a807a3a810d46fa3c6b16a1d171d6f94f765aee7dfb93da2a435160a26a4ac3e921a34783e80738cfc5b30eadfd3fffcbf074ce2e6cba801c2f7332c373c75a6
7
+ data.tar.gz: 3409ea860fbff36986ba86572d9db4690f1328d83d3876b19fb6c97744c15b1df7cd80f953fa4f2a231325c6dfe6656dfdee9f64619e917fb608248c096efb35
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ 2.1.0
4
+ -----
5
+ * [Blocks and their extract! shortcuts are additive by default](https://github.com/rails/jbuilder/commit/a49390736c5f6e2d7a31111df6531bc28dba9fb1)
6
+
3
7
  2.0.8
4
8
  -----
5
9
  * [Eliminate circular dependencies](https://github.com/rails/jbuilder/commit/0879484dc74e7be93b695f66e3708ba48cdb1be3)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '2.0.8'
3
+ s.version = '2.1.0'
4
4
  s.authors = ['David Heinemeier Hansson', 'Pavel Pravosud']
5
5
  s.email = ['david@37signals.com', 'pavel@pravosud.com']
6
6
  s.summary = 'Create JSON structures via a Builder-style DSL'
@@ -32,7 +32,7 @@ class Jbuilder
32
32
  else
33
33
  # json.comments { ... }
34
34
  # { "comments": ... }
35
- _scope{ yield self }
35
+ _merge_block(key){ yield self }
36
36
  end
37
37
  elsif args.empty?
38
38
  if ::Jbuilder === value
@@ -52,7 +52,7 @@ class Jbuilder
52
52
  else
53
53
  # json.author @post.creator, :name, :email_address
54
54
  # { "author": { "name": "David", "email_address": "david@loudthinking.com" } }
55
- _scope{ extract! value, *args }
55
+ _merge_block(key){ extract! value, *args }
56
56
  end
57
57
 
58
58
  _set_value key, result
@@ -174,13 +174,15 @@ class Jbuilder
174
174
  #
175
175
  # [1,2,3]
176
176
  def array!(collection = [], *attributes, &block)
177
- @attributes = if block
177
+ array = if block
178
178
  _map_collection(collection, &block)
179
179
  elsif attributes.any?
180
180
  _map_collection(collection) { |element| extract! element, *attributes }
181
181
  else
182
182
  collection
183
183
  end
184
+
185
+ merge! array
184
186
  end
185
187
 
186
188
  # Extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON.
@@ -230,12 +232,7 @@ class Jbuilder
230
232
 
231
233
  # Merges hash or array into current builder.
232
234
  def merge!(hash_or_array)
233
- if ::Array === hash_or_array
234
- @attributes = [] unless ::Array === @attributes
235
- @attributes.concat hash_or_array
236
- else
237
- @attributes.update hash_or_array
238
- end
235
+ @attributes = _merge_values(@attributes, hash_or_array)
239
236
  end
240
237
 
241
238
  # Encodes the current builder as JSON.
@@ -253,12 +250,29 @@ class Jbuilder
253
250
  attributes.each{ |key| _set_value key, object.public_send(key) }
254
251
  end
255
252
 
253
+ def _merge_block(key, &block)
254
+ current_value = _read(key, {})
255
+ raise NullError.build(key) if current_value.nil?
256
+ value = _scope{ yield self }
257
+ value.nil? ? value : _merge_values(current_value, value)
258
+ end
259
+
260
+ def _read(key, default = nil)
261
+ @attributes.fetch(_key(key)){ default }
262
+ end
263
+
264
+ def _write(key, value)
265
+ @attributes[_key(key)] = value
266
+ end
267
+
268
+ def _key(key)
269
+ @key_formatter.format(key)
270
+ end
271
+
256
272
  def _set_value(key, value)
257
273
  raise NullError.build(key) if @attributes.nil?
258
274
  return if @ignore_nil && value.nil?
259
-
260
- key = @key_formatter.format(key)
261
- @attributes[key] = value
275
+ _write key, value
262
276
  end
263
277
 
264
278
  def _map_collection(collection)
@@ -281,6 +295,19 @@ class Jbuilder
281
295
  def _mapable_arguments?(value, *args)
282
296
  value.respond_to?(:map)
283
297
  end
298
+
299
+ def _merge_values(attributes, hash_or_array)
300
+ attributes = attributes.dup
301
+
302
+ if ::Array === hash_or_array
303
+ attributes = [] unless ::Array === attributes
304
+ attributes.concat hash_or_array
305
+ else
306
+ attributes.update hash_or_array
307
+ end
308
+
309
+ attributes
310
+ end
284
311
  end
285
312
 
286
313
  require 'jbuilder/jbuilder_template' if defined?(ActionView::Template)
@@ -121,6 +121,42 @@ class JbuilderTest < ActiveSupport::TestCase
121
121
  assert_equal 32, result['author']['age']
122
122
  end
123
123
 
124
+ test 'blocks are additive' do
125
+ result = jbuild do |json|
126
+ json.author do
127
+ json.name 'David'
128
+ end
129
+
130
+ json.author do
131
+ json.age 32
132
+ end
133
+ end
134
+
135
+ assert_equal 'David', result['author']['name']
136
+ assert_equal 32, result['author']['age']
137
+ end
138
+
139
+ test 'blocks are additive via extract syntax' do
140
+ person = Person.new('Pavel', 27)
141
+
142
+ result = jbuild do |json|
143
+ json.author person, :age
144
+ json.author person, :name
145
+ end
146
+
147
+ assert_equal 'Pavel', result['author']['name']
148
+ assert_equal 27, result['author']['age']
149
+ end
150
+
151
+ test 'arrays are additive' do
152
+ result = jbuild do |json|
153
+ json.array! %w[foo]
154
+ json.array! %w[bar]
155
+ end
156
+
157
+ assert_equal %w[foo bar], result
158
+ end
159
+
124
160
  test 'nesting multiple children with block' do
125
161
  result = jbuild do |json|
126
162
  json.comments do
@@ -531,9 +567,40 @@ class JbuilderTest < ActiveSupport::TestCase
531
567
  assert_nil result
532
568
  end
533
569
 
534
- test 'throws meaningfull error when on trying to add properties to null' do
570
+ test 'null! in a block' do
571
+ result = jbuild do |json|
572
+ json.author do
573
+ json.name 'David'
574
+ end
575
+
576
+ json.author do
577
+ json.null!
578
+ end
579
+ end
580
+
581
+ assert result.key?('author')
582
+ assert_nil result['author']
583
+ end
584
+
585
+ test 'throws NullError when trying to add properties to null' do
535
586
  json = Jbuilder.new
536
587
  json.null!
537
- assert_raise(Jbuilder::NullError) { json.foo 'bar' }
588
+ assert_raise Jbuilder::NullError do
589
+ json.foo 'bar'
590
+ end
591
+ end
592
+
593
+ test 'throws NullError when trying to add properties to null using block syntax' do
594
+ assert_raise Jbuilder::NullError do
595
+ jbuild do |json|
596
+ json.author do
597
+ json.null!
598
+ end
599
+
600
+ json.author do
601
+ json.name "Pavel"
602
+ end
603
+ end
604
+ end
538
605
  end
539
606
  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: 2.0.8
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-06 00:00:00.000000000 Z
12
+ date: 2014-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport