jsonify 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/jsonify/builder.rb +54 -18
- data/lib/jsonify/version.rb +1 -1
- data/spec/builder_spec.rb +25 -2
- metadata +18 -18
data/lib/jsonify/builder.rb
CHANGED
@@ -55,8 +55,18 @@ module Jsonify
|
|
55
55
|
def tag!(sym, args=nil, &block)
|
56
56
|
method_missing(sym, *args, &block)
|
57
57
|
end
|
58
|
-
|
59
|
-
#
|
58
|
+
|
59
|
+
# Adds a new JsonPair for each attribute in attrs by taking attr as key and value of that attribute in object.
|
60
|
+
#
|
61
|
+
# @param object [Object] Object to take values from
|
62
|
+
# @param *attrs [Array<Symbol>] Array of attributes for JsonPair keys
|
63
|
+
def attributes!(object, *attrs)
|
64
|
+
attrs.each do |attr|
|
65
|
+
method_missing attr, object.send(attr)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Compiles the JSON objects into a string representation.
|
60
70
|
# If initialized with +:verify => true+, the compiled result will be verified by attempting to re-parse it using +MultiJson.load+.
|
61
71
|
# If initialized with +:format => :pretty+, the compiled result will be parsed and encoded via +MultiJson.dump(<json>, :pretty => true)+
|
62
72
|
# This method can be called without any side effects. You can call +compile!+ at any time, and multiple times if desired.
|
@@ -96,6 +106,47 @@ module Jsonify
|
|
96
106
|
end
|
97
107
|
self
|
98
108
|
end
|
109
|
+
|
110
|
+
# Creates array of json objects in current element from array passed to this method.
|
111
|
+
# Accepts block which yields each array element.
|
112
|
+
#
|
113
|
+
# @example Create array in root JSON element
|
114
|
+
# json.array!(@links) do |link|
|
115
|
+
# json.rel link.first
|
116
|
+
# json.href link.last
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# @example compiles to something like ...
|
120
|
+
# [
|
121
|
+
# {
|
122
|
+
# "rel": "self",
|
123
|
+
# "href": "http://example.com/people/123"
|
124
|
+
# },
|
125
|
+
# {
|
126
|
+
# "rel": "school",
|
127
|
+
# "href": "http://gatech.edu"
|
128
|
+
# }
|
129
|
+
# ]
|
130
|
+
#
|
131
|
+
def array!(args)
|
132
|
+
__array
|
133
|
+
args.each do |arg|
|
134
|
+
@level += 1
|
135
|
+
yield arg
|
136
|
+
@level -= 1
|
137
|
+
|
138
|
+
value = @stack.pop
|
139
|
+
|
140
|
+
# If the object created was an array with a single value
|
141
|
+
# assume that just the value should be added
|
142
|
+
if (JsonArray === value && value.values.length <= 1)
|
143
|
+
value = value.values.first
|
144
|
+
end
|
145
|
+
|
146
|
+
@stack[@level].add value
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
99
150
|
|
100
151
|
# Adds a new JsonPair to the builder where the key of the pair is set to the method name
|
101
152
|
# (`sym`).
|
@@ -153,22 +204,7 @@ module Jsonify
|
|
153
204
|
if args.nil?
|
154
205
|
block.call
|
155
206
|
else
|
156
|
-
|
157
|
-
args.each do |arg|
|
158
|
-
@level += 1
|
159
|
-
block.call(arg)
|
160
|
-
@level -= 1
|
161
|
-
|
162
|
-
value = @stack.pop
|
163
|
-
|
164
|
-
# If the object created was an array with a single value
|
165
|
-
# assume that just the value should be added
|
166
|
-
if (JsonArray === value && value.values.length <= 1)
|
167
|
-
value = value.values.first
|
168
|
-
end
|
169
|
-
|
170
|
-
@stack[@level].add value
|
171
|
-
end
|
207
|
+
array!(args, &block)
|
172
208
|
end
|
173
209
|
|
174
210
|
# Set the value on the pair to the object at the top of the stack
|
data/lib/jsonify/version.rb
CHANGED
data/spec/builder_spec.rb
CHANGED
@@ -145,7 +145,15 @@ PRETTY_JSON
|
|
145
145
|
MultiJson.load(json.compile!).should == MultiJson.load('{"1":[2,3],"4":5}')
|
146
146
|
end
|
147
147
|
end
|
148
|
-
|
148
|
+
|
149
|
+
describe "attributes!" do
|
150
|
+
it "should allow create object with attributes of another object" do
|
151
|
+
object = stub(:id => 1, :name => 'foo')
|
152
|
+
json.attributes!(object, :id, :name)
|
153
|
+
MultiJson.load(json.compile!).should == {'id' => 1, 'name' => 'foo'}
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
149
157
|
describe 'using blocks' do
|
150
158
|
|
151
159
|
it 'should allow names with spaces using tag!' do
|
@@ -332,6 +340,21 @@ PRETTY_JSON
|
|
332
340
|
expected = '{"letters":["A","B","C"]}'
|
333
341
|
MultiJson.load(json.compile!).should == MultiJson.load(expected)
|
334
342
|
end
|
335
|
-
|
343
|
+
|
344
|
+
end
|
345
|
+
|
346
|
+
describe 'array!' do
|
347
|
+
it 'allow creating array from root' do
|
348
|
+
json.array!([1, 2, 3]) do |number|
|
349
|
+
json.id number
|
350
|
+
json.times2 number * 2
|
351
|
+
end
|
352
|
+
|
353
|
+
MultiJson.load(json.compile!).should == [
|
354
|
+
{'id' => 1, 'times2' => 2},
|
355
|
+
{'id' => 2, 'times2' => 4},
|
356
|
+
{'id' => 3, 'times2' => 6},
|
357
|
+
]
|
358
|
+
end
|
336
359
|
end
|
337
360
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
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-
|
12
|
+
date: 2012-09-12 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|
16
|
-
requirement: &
|
16
|
+
requirement: &70302252507960 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '1.3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70302252507960
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &70302252506420 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70302252506420
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: tilt
|
38
|
-
requirement: &
|
38
|
+
requirement: &70302252482580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.3.2
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70302252482580
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
|
-
requirement: &
|
49
|
+
requirement: &70302252480320 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70302252480320
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rspec
|
60
|
-
requirement: &
|
60
|
+
requirement: &70302252475700 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70302252475700
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: yard
|
71
|
-
requirement: &
|
71
|
+
requirement: &70302252423420 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70302252423420
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rdiscount
|
82
|
-
requirement: &
|
82
|
+
requirement: &70302252420620 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70302252420620
|
91
91
|
description: Turn Ruby objects into JSON -- correctly!
|
92
92
|
email:
|
93
93
|
- bsiggelkow@me.com
|
@@ -133,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
segments:
|
135
135
|
- 0
|
136
|
-
hash: -
|
136
|
+
hash: -344377387242156613
|
137
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
138
|
none: false
|
139
139
|
requirements:
|
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
version: '0'
|
143
143
|
segments:
|
144
144
|
- 0
|
145
|
-
hash: -
|
145
|
+
hash: -344377387242156613
|
146
146
|
requirements: []
|
147
147
|
rubyforge_project: jsonify
|
148
148
|
rubygems_version: 1.8.6
|