jsonity 1.0.4 → 1.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.
- checksums.yaml +4 -4
- data/README.md +8 -2
- data/lib/jsonity/attribute.rb +29 -5
- data/lib/jsonity/builder.rb +23 -18
- data/lib/jsonity/formatter.rb +11 -0
- data/lib/jsonity/version.rb +1 -1
- data/lib/jsonity.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b9dbe8b1de7d83969314a84ad14212824935da4
|
4
|
+
data.tar.gz: 1ad4683c458a684dbb631ca8870ce40ea019500c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aebb379ed36c70e1e37c50e4ae27ef4cc2235ae9b0c5fd48994857407ec385fa0b7fb86f9477562cf14906ec7072696d3d1907180600866a09084f50e16a6c20
|
7
|
+
data.tar.gz: 0a7d2d44a3cc95b2fbcdd424080c6b7addd7f8a0b34d41451441f2c31fd1adea71e08b206755d85ad7e66589f546110a3e37d30e888cf543e73aeea30fb6e71b
|
data/README.md
CHANGED
@@ -296,6 +296,10 @@ If you set `attr_json` in any class, **the specified attributes will automatical
|
|
296
296
|
```ruby
|
297
297
|
class Sample < Struct.new(:id, :foo, :bar)
|
298
298
|
attr_json :id, :foo
|
299
|
+
|
300
|
+
attr_json { |sample|
|
301
|
+
sample.hello_from 'attr_json!'
|
302
|
+
}
|
299
303
|
end
|
300
304
|
|
301
305
|
@sample = Sample.new 123, 'foo!', 'bar!!'
|
@@ -311,7 +315,8 @@ Jsonity.build { |t|
|
|
311
315
|
{
|
312
316
|
"sample": {
|
313
317
|
"id": 123,
|
314
|
-
"foo": "foo!"
|
318
|
+
"foo": "foo!",
|
319
|
+
"hello_from": "attr_json!"
|
315
320
|
}
|
316
321
|
}
|
317
322
|
=end
|
@@ -322,7 +327,7 @@ Still you can create any kinds of nodes with a block:
|
|
322
327
|
```ruby
|
323
328
|
Jsonity.build { |t|
|
324
329
|
t.sample!(@sample) { |sample|
|
325
|
-
sample.bar { |
|
330
|
+
sample.bar { |s| "this is #{s.bar}" }
|
326
331
|
}
|
327
332
|
}
|
328
333
|
=begin
|
@@ -330,6 +335,7 @@ Jsonity.build { |t|
|
|
330
335
|
"sample": {
|
331
336
|
"id": 123,
|
332
337
|
"foo": "foo!",
|
338
|
+
"hello_from": "attr_json!",
|
333
339
|
"bar": "this is bar!!"
|
334
340
|
}
|
335
341
|
}
|
data/lib/jsonity/attribute.rb
CHANGED
@@ -8,10 +8,15 @@ module Jsonity
|
|
8
8
|
# Automatically export attributes to json
|
9
9
|
#
|
10
10
|
# @params {[String | Symbol]} *attrs
|
11
|
+
# @block - [optional]
|
11
12
|
###
|
12
|
-
def attr_json(*attrs)
|
13
|
-
@json_attributes ||=
|
14
|
-
@json_attributes
|
13
|
+
def attr_json(*attrs, &block)
|
14
|
+
@json_attributes ||= []
|
15
|
+
@json_attributes += attrs.map(&:to_s)
|
16
|
+
@json_attributes.uniq!
|
17
|
+
|
18
|
+
@json_attribute_blocks ||= []
|
19
|
+
@json_attribute_blocks << block if block
|
15
20
|
end
|
16
21
|
|
17
22
|
###
|
@@ -20,14 +25,24 @@ module Jsonity
|
|
20
25
|
# @return {[String]}
|
21
26
|
###
|
22
27
|
def json_attributes
|
23
|
-
@json_attributes
|
28
|
+
@json_attributes || []
|
29
|
+
end
|
30
|
+
|
31
|
+
###
|
32
|
+
# Get json attributes
|
33
|
+
#
|
34
|
+
# @return {[String]}
|
35
|
+
###
|
36
|
+
def json_attribute_blocks
|
37
|
+
@json_attribute_blocks || []
|
24
38
|
end
|
39
|
+
|
25
40
|
end
|
26
41
|
|
27
42
|
module InstanceMethods
|
28
43
|
|
29
44
|
###
|
30
|
-
# Get json attributes
|
45
|
+
# Get json attributes (delegates to self class)
|
31
46
|
#
|
32
47
|
# @return {[String]}
|
33
48
|
###
|
@@ -35,6 +50,15 @@ module Jsonity
|
|
35
50
|
self.class.json_attributes
|
36
51
|
end
|
37
52
|
|
53
|
+
###
|
54
|
+
# Get json attributes (delegates to self class)
|
55
|
+
#
|
56
|
+
# @return {[String]}
|
57
|
+
###
|
58
|
+
def json_attribute_blocks
|
59
|
+
self.class.json_attribute_blocks
|
60
|
+
end
|
61
|
+
|
38
62
|
end
|
39
63
|
end
|
40
64
|
end
|
data/lib/jsonity/builder.rb
CHANGED
@@ -15,11 +15,15 @@ module Jsonity
|
|
15
15
|
builder = new object, content
|
16
16
|
|
17
17
|
if object.respond_to? :json_attributes
|
18
|
-
object.json_attributes.each { |a| builder.__send__
|
18
|
+
object.json_attributes.each { |a| builder.__send__(:attribute, a, {}) }
|
19
|
+
end
|
20
|
+
|
21
|
+
if object.respond_to? :json_attribute_blocks
|
22
|
+
object.json_attribute_blocks.each { |b| builder.(&b) }
|
19
23
|
end
|
20
24
|
|
21
25
|
builder.(&block)
|
22
|
-
builder.
|
26
|
+
builder._content
|
23
27
|
end
|
24
28
|
|
25
29
|
###
|
@@ -64,6 +68,16 @@ module Jsonity
|
|
64
68
|
end
|
65
69
|
end
|
66
70
|
|
71
|
+
###
|
72
|
+
# Getter for `@content`
|
73
|
+
#
|
74
|
+
# @return {Hash | nil}
|
75
|
+
###
|
76
|
+
def _content
|
77
|
+
evaluate_array_blocks!
|
78
|
+
@content
|
79
|
+
end
|
80
|
+
|
67
81
|
|
68
82
|
private
|
69
83
|
|
@@ -75,7 +89,7 @@ module Jsonity
|
|
75
89
|
is_object = name.match OBJECT_SUFFIX
|
76
90
|
name, is_object = name[0..-2], is_object[0] if is_object
|
77
91
|
|
78
|
-
options = args.last.is_a?(::Hash) ? args.pop : {}
|
92
|
+
options = args.last.is_a?(::Hash) ? args.pop.dup : {}
|
79
93
|
options[:_object] = args[0]
|
80
94
|
options[:_nullable] = ('?' == is_object)
|
81
95
|
|
@@ -98,16 +112,6 @@ module Jsonity
|
|
98
112
|
self
|
99
113
|
end
|
100
114
|
|
101
|
-
###
|
102
|
-
# Getter for `@content`
|
103
|
-
#
|
104
|
-
# @return {Hash | nil}
|
105
|
-
###
|
106
|
-
def content
|
107
|
-
evaluate_array_blocks!
|
108
|
-
@content
|
109
|
-
end
|
110
|
-
|
111
115
|
###
|
112
116
|
# Create attribute node
|
113
117
|
#
|
@@ -118,7 +122,8 @@ module Jsonity
|
|
118
122
|
def attribute(name, options, &block)
|
119
123
|
obj = get_object_for name, options
|
120
124
|
|
121
|
-
|
125
|
+
value = block ? block_call(block, options[:_object] || @object) : obj
|
126
|
+
@content[name] = Formatter.format value, name
|
122
127
|
end
|
123
128
|
|
124
129
|
###
|
@@ -196,14 +201,14 @@ module Jsonity
|
|
196
201
|
# Get object
|
197
202
|
#
|
198
203
|
# @params {String} name
|
199
|
-
# @params {Hash} options
|
204
|
+
# @params {Hash} options
|
200
205
|
#
|
201
206
|
# @return {any}
|
202
207
|
###
|
203
|
-
def get_object_for(name, options
|
204
|
-
if options
|
208
|
+
def get_object_for(name, options)
|
209
|
+
if options[:_object]
|
205
210
|
options[:_object]
|
206
|
-
elsif options
|
211
|
+
elsif options[:inherit]
|
207
212
|
@object
|
208
213
|
elsif @object.respond_to? name
|
209
214
|
@object.public_send name
|
data/lib/jsonity/version.rb
CHANGED
data/lib/jsonity.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuki Iwanaga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- lib/jsonity/attribute.rb
|
56
56
|
- lib/jsonity/builder.rb
|
57
57
|
- lib/jsonity/core_ext.rb
|
58
|
+
- lib/jsonity/formatter.rb
|
58
59
|
- lib/jsonity/rails.rb
|
59
60
|
- lib/jsonity/version.rb
|
60
61
|
homepage: https://github.com/creasty/jsonity
|