jsonbuilder 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,15 @@
1
1
  module Builder
2
2
 
3
3
  class XmlMarkup
4
- def array_mode(&block)
4
+ def array_mode(key = nil, &block)
5
5
  yield(self)
6
6
  end
7
7
  end
8
8
 
9
9
  class JsonMarkup
10
10
 
11
+ attr_accessor :path, :target
12
+
11
13
  def initialize(options = {})
12
14
  # @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
13
15
  # in this case, we need some key for value.
@@ -22,10 +24,18 @@ module Builder
22
24
  end
23
25
 
24
26
  # NOTICE: you have to call this method to use array in json
25
- def array_mode(&block)
27
+ def array_mode(key = nil, &block)
26
28
  @array_mode = true
27
- eval("#{current} = []")
28
- yield(self)
29
+ if eval("#{current}").is_a?(Hash)
30
+ key ||= :entries
31
+ eval("#{current}.merge!(key => [])")
32
+ @path.push(key.to_sym)
33
+ yield(self)
34
+ @path.pop
35
+ else
36
+ eval("#{current} = []")
37
+ yield(self)
38
+ end
29
39
  @array_mode = false
30
40
  end
31
41
 
@@ -73,7 +83,6 @@ module Builder
73
83
  else
74
84
  children(key, args, &block)
75
85
  end
76
- self
77
86
  target!
78
87
  end
79
88
 
@@ -88,6 +97,7 @@ module Builder
88
97
  end
89
98
 
90
99
  def children(key, args, &block)
100
+ eval("#{current} ||= {}")
91
101
  @path.push(key)
92
102
  set_args(args, &block)
93
103
  @path.pop
data/lib/jsonbuilder.rb CHANGED
@@ -2,7 +2,7 @@ module JsonBuilder
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- REVISION = 3
5
+ REVISION = 4
6
6
  class << self
7
7
  def to_version
8
8
  "#{MAJOR}.#{MINOR}.#{REVISION}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - nov