jsonbuilder 0.0.4 → 0.0.5
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/lib/builder/jsonmarkup.rb +20 -21
- data/lib/jsonbuilder.rb +1 -1
- data/spec/builder/jsonmarkup_spec.rb +164 -0
- data/spec/jsonbuilder_spec.rb +13 -0
- metadata +4 -3
data/lib/builder/jsonmarkup.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Builder
|
2
2
|
|
3
3
|
class XmlMarkup
|
4
|
+
# Do nothing
|
4
5
|
def array_mode(key = nil, &block)
|
5
6
|
yield(self)
|
6
7
|
end
|
@@ -8,8 +9,6 @@ module Builder
|
|
8
9
|
|
9
10
|
class JsonMarkup
|
10
11
|
|
11
|
-
attr_accessor :path, :target
|
12
|
-
|
13
12
|
def initialize(options = {})
|
14
13
|
# @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
|
15
14
|
# in this case, we need some key for value.
|
@@ -26,14 +25,14 @@ module Builder
|
|
26
25
|
# NOTICE: you have to call this method to use array in json
|
27
26
|
def array_mode(key = nil, &block)
|
28
27
|
@array_mode = true
|
29
|
-
if eval("#{
|
28
|
+
if eval("#{_current}").is_a?(Hash)
|
30
29
|
key ||= :entries
|
31
|
-
eval("#{
|
30
|
+
eval("#{_current}.merge!(key => [])")
|
32
31
|
@path.push(key.to_sym)
|
33
32
|
yield(self)
|
34
33
|
@path.pop
|
35
34
|
else
|
36
|
-
eval("#{
|
35
|
+
eval("#{_current} = []")
|
37
36
|
yield(self)
|
38
37
|
end
|
39
38
|
@array_mode = false
|
@@ -55,18 +54,18 @@ module Builder
|
|
55
54
|
|
56
55
|
def <<(_target)
|
57
56
|
if @array_mode
|
58
|
-
eval("#{
|
57
|
+
eval("#{_current} << _target")
|
59
58
|
else
|
60
|
-
eval("#{
|
61
|
-
eval("#{
|
59
|
+
eval("#{_current} ||= {}")
|
60
|
+
eval("#{_current}.merge!(_target)")
|
62
61
|
end
|
63
62
|
end
|
64
63
|
|
65
64
|
def text!(text)
|
66
|
-
if eval("#{
|
67
|
-
eval("#{
|
65
|
+
if eval("#{_current}").is_a?(Hash)
|
66
|
+
eval("#{_current}.merge!({@default_content_key => text})")
|
68
67
|
else
|
69
|
-
eval("#{
|
68
|
+
eval("#{_current} = text")
|
70
69
|
end
|
71
70
|
end
|
72
71
|
alias_method :cdata!, :text!
|
@@ -79,43 +78,43 @@ module Builder
|
|
79
78
|
key = args.first.is_a?(Symbol) ? "#{key}:#{args.shift}".to_sym : key.to_sym
|
80
79
|
args[0] = {@default_content_key => args[0]} if args.size > 1 && !args[0].is_a?(Hash)
|
81
80
|
unless @root
|
82
|
-
|
81
|
+
_root(key, args, &block)
|
83
82
|
else
|
84
|
-
|
83
|
+
_child(key, args, &block)
|
85
84
|
end
|
86
85
|
target!
|
87
86
|
end
|
88
87
|
|
89
88
|
private
|
90
89
|
|
91
|
-
def
|
90
|
+
def _root(root, args, &block)
|
92
91
|
@root = root
|
93
92
|
@target[root] = {}
|
94
93
|
@path = [root]
|
95
|
-
|
94
|
+
_set_args(args, &block)
|
96
95
|
yield(self) if block_given?
|
97
96
|
end
|
98
97
|
|
99
|
-
def
|
100
|
-
eval("#{
|
98
|
+
def _child(key, args, &block)
|
99
|
+
eval("#{_current} ||= {}")
|
101
100
|
@path.push(key)
|
102
|
-
|
101
|
+
_set_args(args, &block)
|
103
102
|
@path.pop
|
104
103
|
end
|
105
104
|
|
106
|
-
def
|
105
|
+
def _set_args(args, &block)
|
107
106
|
args.each do |arg|
|
108
107
|
case arg
|
109
108
|
when Hash
|
110
109
|
self << arg
|
111
110
|
else
|
112
|
-
eval("#{
|
111
|
+
eval("#{_current} = arg")
|
113
112
|
end
|
114
113
|
end
|
115
114
|
yield(self) if block_given?
|
116
115
|
end
|
117
116
|
|
118
|
-
def
|
117
|
+
def _current
|
119
118
|
"@target[:\"#{@path.join('"][:"')}\"]"
|
120
119
|
end
|
121
120
|
|
data/lib/jsonbuilder.rb
CHANGED
@@ -0,0 +1,164 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe Builder::JsonMarkup, ".new" do
|
4
|
+
it "should be accessible" do
|
5
|
+
Builder::JsonMarkup.should respond_to(:new)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Builder::JsonMarkup do
|
10
|
+
|
11
|
+
it "should remove root tag" do
|
12
|
+
markup = Builder::JsonMarkup.new
|
13
|
+
# XML :: <root><tag>value</tag></root>
|
14
|
+
markup.root do
|
15
|
+
markup.tag "value"
|
16
|
+
end
|
17
|
+
markup.target!.should == {:tag => "value"}
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not remove root tag when include_root is true" do
|
21
|
+
markup = Builder::JsonMarkup.new(:include_root => true)
|
22
|
+
# XML :: <root><tag>value</tag></root>
|
23
|
+
markup.root do
|
24
|
+
markup.tag "value"
|
25
|
+
end
|
26
|
+
markup.target!.should == {:root => {:tag => "value"}}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should use default_content_key when both content and attributes are exist" do
|
30
|
+
markup = Builder::JsonMarkup.new
|
31
|
+
# XML :: <root><tag id="1">value</tag></root>
|
32
|
+
markup.root do
|
33
|
+
markup.tag("value", :id => 1)
|
34
|
+
end
|
35
|
+
markup.target!.should == {:tag => {:id => 1, :content => "value"}}
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should use default_content_key when both cdata! and attributes are exist" do
|
39
|
+
markup = Builder::JsonMarkup.new
|
40
|
+
# XML :: <root><tag id="1"><![CDATA[value]]></tag></root>
|
41
|
+
markup.root do
|
42
|
+
markup.tag(:id => 1) do
|
43
|
+
markup.cdata! "value"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
markup.target!.should == {:tag => {:id => 1, :content => "value"}}
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
describe Builder::JsonMarkup, "#array_mode" do
|
52
|
+
|
53
|
+
it "should support <<" do
|
54
|
+
markup = Builder::JsonMarkup.new
|
55
|
+
# XML ::
|
56
|
+
# <root>
|
57
|
+
# <items>
|
58
|
+
# <item>
|
59
|
+
# <text>hello world 0</text>
|
60
|
+
# </item>
|
61
|
+
# <item>
|
62
|
+
# <text>hello world 1</text>
|
63
|
+
# </item>
|
64
|
+
# </items>
|
65
|
+
# </root>
|
66
|
+
markup.root do
|
67
|
+
markup.items do
|
68
|
+
markup.array_mode do
|
69
|
+
2.times do |i|
|
70
|
+
_markup = Builder::JsonMarkup.new
|
71
|
+
markup << _markup.item do
|
72
|
+
_markup.text "hello world #{i}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
markup.target!.should == {
|
79
|
+
:items => [{:text => "hello world 0"}, {:text => "hello world 1"}]
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should generate new key if needed" do
|
84
|
+
markup = Builder::JsonMarkup.new
|
85
|
+
# XML ::
|
86
|
+
# <root>
|
87
|
+
# <items site="smart.fm">
|
88
|
+
# <item>
|
89
|
+
# <text>hello world 0</text>
|
90
|
+
# </item>
|
91
|
+
# <item>
|
92
|
+
# <text>hello world 1</text>
|
93
|
+
# </item>
|
94
|
+
# </items>
|
95
|
+
# </root>
|
96
|
+
markup.root do
|
97
|
+
markup.items(:site => "smart.fm") do
|
98
|
+
markup.array_mode do
|
99
|
+
2.times do |i|
|
100
|
+
_markup = Builder::JsonMarkup.new
|
101
|
+
markup << _markup.item do
|
102
|
+
_markup.text "hello world #{i}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
markup.target!.should == {
|
109
|
+
:items => {
|
110
|
+
:entries => [{:text => "hello world 0"}, {:text=>"hello world 1"}],
|
111
|
+
:site=>"smart.fm"
|
112
|
+
}
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should treat blank block as blank Array" do
|
117
|
+
# XML ::
|
118
|
+
# <root>
|
119
|
+
# <items>
|
120
|
+
# </items>
|
121
|
+
# </root>
|
122
|
+
markup = Builder::JsonMarkup.new
|
123
|
+
markup.root do
|
124
|
+
markup.items do
|
125
|
+
markup.array_mode do
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
markup.target!.should == {:items => []}
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should not support all methods except <<"do
|
133
|
+
markup = Builder::JsonMarkup.new
|
134
|
+
# XML ::
|
135
|
+
# <root>
|
136
|
+
# <items>
|
137
|
+
# <item>
|
138
|
+
# <text>hello world 0</text>
|
139
|
+
# </item>
|
140
|
+
# <item>
|
141
|
+
# <text>hello world 1</text>
|
142
|
+
# </item>
|
143
|
+
# </items>
|
144
|
+
# </root>
|
145
|
+
succeeded = true
|
146
|
+
markup.root do
|
147
|
+
markup.items do
|
148
|
+
begin
|
149
|
+
markup.array_mode do
|
150
|
+
2.times do
|
151
|
+
markup.item do
|
152
|
+
markup.text "hello world"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
rescue
|
157
|
+
succeeded = false
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
succeeded.should_not be_true
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe JsonBuilder::Version, "#to_version" do
|
4
|
+
it "should return version in 'X.Y.Z' format" do
|
5
|
+
JsonBuilder::Version.to_version.should =~ /\d+\.\d+\.\d+/
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe JsonBuilder::Version, "#to_name" do
|
10
|
+
it "should return version in 'X_Y_Z' format" do
|
11
|
+
JsonBuilder::Version.to_name.should =~ /\d+_\d+_\d+/
|
12
|
+
end
|
13
|
+
end
|
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.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nov
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- Rakefile
|
29
29
|
- spec/builder
|
30
30
|
- spec/builder/jsonmarkup_spec.rb
|
31
|
+
- spec/jsonbuilder_spec.rb
|
31
32
|
- spec/spec_helper.rb
|
32
33
|
- lib/builder
|
33
34
|
- lib/builder/jsonmarkup.rb
|
@@ -69,5 +70,5 @@ rubygems_version: 1.3.1
|
|
69
70
|
signing_key:
|
70
71
|
specification_version: 2
|
71
72
|
summary: Builder::XmlMarkup like JsonBuilder (Builder::JsonMarkup)
|
72
|
-
test_files:
|
73
|
-
|
73
|
+
test_files:
|
74
|
+
- spec/jsonbuilder_spec.rb
|