jsonity 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42316fb2fab2080e7f8c124563f1971d6da6e2dc
4
- data.tar.gz: 236ba99b0a71315e733f4ec072a9935d71e72a20
3
+ metadata.gz: 8b9dbe8b1de7d83969314a84ad14212824935da4
4
+ data.tar.gz: 1ad4683c458a684dbb631ca8870ce40ea019500c
5
5
  SHA512:
6
- metadata.gz: 7a20a170051ad9b85ee8e7718b2eb29b8fc73b4ada974ae09c36321b8c97662e33baacc6fc6d541c7b862ac8d61d33467d1cc724f6dba4c696181b20bf30e252
7
- data.tar.gz: 2a5f7fcd98774c4d19914f07f4048e61c7809259456382e52d00deac7baac9561ea0e5a5e5bc32337675f35fd856a1f397315ddd07aa69a948173719a8aa8f46
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 { |bar| "this is #{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
  }
@@ -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 ||= Set.new
14
- @json_attributes |= attrs.map(&:to_s)
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.to_a
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
@@ -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__ :attribute, a, nil }
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.__send__ :content
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
- @content[name] = block ? block_call(block, obj || @object) : obj
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 - [optional]
204
+ # @params {Hash} options
200
205
  #
201
206
  # @return {any}
202
207
  ###
203
- def get_object_for(name, options = nil)
204
- if options && options[:_object]
208
+ def get_object_for(name, options)
209
+ if options[:_object]
205
210
  options[:_object]
206
- elsif options && options[:inherit]
211
+ elsif options[:inherit]
207
212
  @object
208
213
  elsif @object.respond_to? name
209
214
  @object.public_send name
@@ -0,0 +1,11 @@
1
+ module Jsonity
2
+ class Formatter
3
+ class << self
4
+
5
+ def format(obj, name)
6
+ obj
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Jsonity
2
- VERSION = '1.0.4'
2
+ VERSION = '1.0.5'
3
3
  end
data/lib/jsonity.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'forwardable'
2
2
  require 'jsonity/version'
3
+ require 'jsonity/formatter'
3
4
  require 'jsonity/builder'
4
5
  require 'jsonity/core_ext'
5
6
  require 'jsonity/rails' if defined? Rails
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
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-18 00:00:00.000000000 Z
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