jsonity 1.0.1 → 1.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5e0cbbf0655194ed77a8e96424d290e700cc6a6
4
- data.tar.gz: c26576875dc473fd201e7f8ccd21940a642da05f
3
+ metadata.gz: c2659fcf6db5641ea0b6dbf288e14562dcd57cc6
4
+ data.tar.gz: 87eea35053efa6c0f1b5dc7ec4ae5a6185ff22b2
5
5
  SHA512:
6
- metadata.gz: 305a32a835c2695991915d9e35899c5080ec3c624dffd63eb54c4760d9107b0390c8c6fd43b10cf86e5135cf0ceca19b1996ac763dfc11d250c34c77af3a6c0b
7
- data.tar.gz: 4ef7f0f1a7c647255cff9f469ef35f0a7027510f5bdb1e9764fe8e6964ab6b20d142d4d7bd0db4fb792ea4c8c7b7dabe56ed87058c1e7d57c0b16d088765b712
6
+ metadata.gz: 97c515d3530a9922573f0cc1902739aa5e5876f248c0b2ad3d345fb67cf9860c22a6a4100d30ebdb966626af45470ea2bf8725291c015c76ad62aa0ef4469f97
7
+ data.tar.gz: 8db7d2049b3996e47c7c7c57075edbb6321596f9ddef05b6a801a94eab698b3e7420bdb4895885eabeab7c01235069c4bb4e66b556a68e9844359788d43c120d
@@ -1,3 +1,5 @@
1
+ require 'set'
2
+
1
3
  module Jsonity
2
4
  module Attribute
3
5
  module ClassMethods
@@ -1,5 +1,3 @@
1
- require 'set'
2
-
3
1
  module Jsonity
4
2
  class Builder < BasicObject
5
3
 
@@ -32,6 +30,7 @@ module Jsonity
32
30
  ###
33
31
  def initialize(object, content)
34
32
  @object, @content = object, content
33
+ @deferred_array_blocks = {}
35
34
  end
36
35
 
37
36
  ###
@@ -42,7 +41,7 @@ module Jsonity
42
41
  end
43
42
 
44
43
  ###
45
- # Make array context
44
+ # Create array context
46
45
  #
47
46
  # @return {Jsonity::Builder} - `self`
48
47
  ###
@@ -74,7 +73,7 @@ module Jsonity
74
73
  def method_missing(name, *args, &block)
75
74
  name = name.to_s
76
75
  is_object = name.match OBJECT_SUFFIX
77
- name = name[0..-2] if is_object
76
+ name, is_object = name[0..-2], is_object[0] if is_object
78
77
 
79
78
  options = args.last.is_a?(::Hash) ? args.pop : {}
80
79
  options[:_object] = args[0]
@@ -105,6 +104,7 @@ module Jsonity
105
104
  # @return {Hash | nil}
106
105
  ###
107
106
  def content
107
+ evaluate_array_blocks!
108
108
  @content
109
109
  end
110
110
 
@@ -148,24 +148,49 @@ module Jsonity
148
148
  # @block
149
149
  ###
150
150
  def array(name, options, &block)
151
+ ::Kernel.raise RequiredBlockError.new('No block') unless block
152
+
153
+ if (deferred = @deferred_array_blocks[name])
154
+ deferred[:blocks] << block
155
+ return
156
+ end
157
+
151
158
  obj = get_object_for name, options
152
159
 
153
160
  is_array = obj && obj.class < ::Enumerable
154
161
 
155
162
  if !is_array && options[:_nullable]
156
163
  @content[name] ||= nil
157
- return
164
+ else
165
+ @content[name] = [] unless @content[name].is_a?(::Array)
158
166
  end
159
167
 
160
- @content[name] = [] unless @content[name].is_a?(::Array)
161
- ary = @content[name]
162
-
163
168
  if is_array
164
- obj.each.with_index do |a, i|
165
- # TODO: deferred build so that can be merged correctly with conditional nodes
166
- ary[i] = Builder.build a, ary[i], &block
169
+ @deferred_array_blocks[name] = {
170
+ obj: obj,
171
+ blocks: [block],
172
+ }
173
+ end
174
+ end
175
+
176
+ ###
177
+ # Evaluate all deferred blocks of array nodes,
178
+ # and reset block stack
179
+ ###
180
+ def evaluate_array_blocks!
181
+ @deferred_array_blocks.each do |name, d|
182
+ next unless d[:obj]
183
+
184
+ ary = @content[name]
185
+
186
+ d[:obj].each.with_index do |a, i|
187
+ d[:blocks].each do |block|
188
+ ary[i] = Builder.build a, ary[i], &block
189
+ end
167
190
  end
168
191
  end
192
+
193
+ @deferred_array_blocks = {}
169
194
  end
170
195
 
171
196
  ###
data/lib/jsonity/rails.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  module ActionController
2
2
  class Base
3
3
 
4
- def render_json(options = {}, &block)
5
- json = Jsonity::Builder.build &block
4
+ protected
5
+
6
+ def render_json(*args, &block)
7
+ options = args.extract_options!
8
+ json = Jsonity::Builder.build args[0], &block
6
9
  options[:json] = json
7
10
  render options
8
11
  end
@@ -1,3 +1,3 @@
1
1
  module Jsonity
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonity
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Iwanaga