representative 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,12 +7,14 @@ module Representative
7
7
  class Json < Base
8
8
 
9
9
  DEFAULT_ATTRIBUTE_PREFIX = "@".freeze
10
+ DEFAULT_INDENTATION = 2 # two spaces
10
11
 
11
12
  def initialize(subject = nil, options = {})
12
13
  super(subject, options)
13
14
  @buffer = []
14
15
  @indent_level = 0
15
- @attribute_prefix = options[:attribute_prefix] || DEFAULT_ATTRIBUTE_PREFIX
16
+ @indent_string = resolve_indentation(options.fetch(:indentation, DEFAULT_INDENTATION))
17
+ @attribute_prefix = options.fetch(:attribute_prefix, DEFAULT_ATTRIBUTE_PREFIX)
16
18
  now_at :beginning_of_buffer
17
19
  yield self if block_given?
18
20
  end
@@ -82,7 +84,7 @@ module Representative
82
84
  end
83
85
 
84
86
  def to_json
85
- @buffer << "\n"
87
+ emit("\n") if indenting?
86
88
  @buffer.join
87
89
  end
88
90
 
@@ -92,6 +94,19 @@ module Representative
92
94
 
93
95
  private
94
96
 
97
+ def resolve_indentation(arg)
98
+ case arg
99
+ when Integer
100
+ " " * arg
101
+ when /\A[ \t]*\Z/
102
+ arg
103
+ when nil, false
104
+ false
105
+ else
106
+ raise ArgumentError, "invalid indentation setting"
107
+ end
108
+ end
109
+
95
110
  def emit(s)
96
111
  @buffer << s
97
112
  end
@@ -100,20 +115,27 @@ module Representative
100
115
  ActiveSupport::JSON.encode(data)
101
116
  end
102
117
 
103
- def indentation
104
- (" " * @indent_level)
118
+ def indenting?
119
+ !!@indent_string
120
+ end
121
+
122
+ def current_indentation
123
+ (@indent_string * @indent_level)
105
124
  end
106
125
 
107
126
  def label(name)
108
127
  return false if @indent_level == 0
109
128
  new_item
110
- emit(format_name(name).inspect + ": ")
129
+ emit(format_name(name).inspect + ":")
130
+ emit(" ") if indenting?
111
131
  end
112
132
 
113
133
  def new_item
114
134
  emit(",") if at? :end_of_item
115
- emit("\n") unless at? :beginning_of_buffer
116
- emit(indentation)
135
+ if indenting?
136
+ emit("\n") unless at? :beginning_of_buffer
137
+ emit(current_indentation)
138
+ end
117
139
  @pending_comma = ","
118
140
  end
119
141
 
@@ -123,7 +145,9 @@ module Representative
123
145
  now_at :beginning_of_block
124
146
  yield
125
147
  @indent_level -= 1
126
- emit("\n" + indentation) unless at? :beginning_of_block
148
+ if indenting?
149
+ emit("\n" + current_indentation) unless at? :beginning_of_block
150
+ end
127
151
  emit(closing_char)
128
152
  now_at :end_of_item
129
153
  end
@@ -1,3 +1,3 @@
1
1
  module Representative
2
- VERSION = "1.0.4".freeze
2
+ VERSION = "1.0.5".freeze
3
3
  end
@@ -33,9 +33,9 @@ describe Representative::Json do
33
33
  end
34
34
 
35
35
  end
36
-
36
+
37
37
  describe "with a nil value" do
38
-
38
+
39
39
  it "generates null" do
40
40
  r.element :flavour, nil
41
41
  resulting_json.should == "null\n"
@@ -51,13 +51,13 @@ describe Representative::Json do
51
51
  end
52
52
 
53
53
  end
54
-
54
+
55
55
  end
56
-
56
+
57
57
  describe "with attributes" do
58
-
58
+
59
59
  describe "and a block" do
60
-
60
+
61
61
  it "generates labelled fields for the attributes" do
62
62
  @book = OpenStruct.new(:lang => "fr", :title => "En Edge")
63
63
  r.element :book, @book, :lang => :lang do
@@ -70,20 +70,20 @@ describe Representative::Json do
70
70
  }
71
71
  JSON
72
72
  end
73
-
73
+
74
74
  end
75
75
 
76
76
  describe "and an explicit value" do
77
-
77
+
78
78
  it "ignores the attributes" do
79
79
  r.element :review, "Blah de blah", :lang => "fr"
80
80
  resulting_json.should == undent(<<-JSON)
81
81
  "Blah de blah"
82
82
  JSON
83
83
  end
84
-
84
+
85
85
  end
86
-
86
+
87
87
  end
88
88
 
89
89
  describe "without an explicit value" do
@@ -144,7 +144,7 @@ describe Representative::Json do
144
144
  }
145
145
  JSON
146
146
  end
147
-
147
+
148
148
  end
149
149
 
150
150
  describe "with a block" do
@@ -176,9 +176,9 @@ describe Representative::Json do
176
176
  ]
177
177
  JSON
178
178
  end
179
-
179
+
180
180
  end
181
-
181
+
182
182
  describe "with item attributes" do
183
183
  it "adds the attributes with an @ sign to child elements" do
184
184
  @authors = [
@@ -198,7 +198,7 @@ describe Representative::Json do
198
198
  "age": 3
199
199
  },
200
200
  {
201
- "@about": "Dewey is 4 years old",
201
+ "@about": "Dewey is 4 years old",
202
202
  "name": "Dewey",
203
203
  "age": 4
204
204
  },
@@ -211,19 +211,19 @@ describe Representative::Json do
211
211
  JSON
212
212
  end
213
213
  end
214
-
214
+
215
215
  describe "with list attributes" do
216
216
  it "raises an ArgumentError" do
217
217
  @authors = []
218
218
  lambda{ r.list_of(:authors, @authors, :list_attributes => {}) {} }.should raise_exception(ArgumentError)
219
219
  end
220
220
  end
221
-
221
+
222
222
  describe "with unnecessary arguments" do
223
223
  it "raises an ArgumentError" do
224
224
  @authors = []
225
- lambda{
226
- r.list_of(:authors, @authors, :unecessary_arg_should_cause_failure, :item_attributes => {}){}
225
+ lambda{
226
+ r.list_of(:authors, @authors, :unecessary_arg_should_cause_failure, :item_attributes => {}){}
227
227
  }.should raise_exception(ArgumentError)
228
228
  end
229
229
  end
@@ -231,16 +231,16 @@ describe Representative::Json do
231
231
  end
232
232
 
233
233
  describe "#comment" do
234
-
234
+
235
235
  it "inserts a comment" do
236
236
  r.comment "now pay attention"
237
237
  resulting_json.should == undent(<<-JSON)
238
238
  // now pay attention
239
239
  JSON
240
240
  end
241
-
241
+
242
242
  end
243
-
243
+
244
244
  end
245
245
 
246
246
  describe "within an element block" do
@@ -269,7 +269,7 @@ describe Representative::Json do
269
269
  r.element :age
270
270
  end
271
271
  resulting_json.should == undent(<<-JSON)
272
- {
272
+ {
273
273
  "name": "Fred",
274
274
  "age": 36
275
275
  }
@@ -281,7 +281,7 @@ describe Representative::Json do
281
281
  end
282
282
 
283
283
  describe "#attribute" do
284
-
284
+
285
285
  it "generates labelled values, with a label prefix" do
286
286
  r.element :author, Object.new do
287
287
  r.attribute :href, "http://example.com/authors/1"
@@ -294,11 +294,11 @@ describe Representative::Json do
294
294
  }
295
295
  JSON
296
296
  end
297
-
297
+
298
298
  end
299
-
299
+
300
300
  describe "#comment" do
301
-
301
+
302
302
  it "inserts a comment" do
303
303
  @author = OpenStruct.new(:name => "Fred", :age => 36)
304
304
  r.element :author, @author do
@@ -307,33 +307,33 @@ describe Representative::Json do
307
307
  r.element :age
308
308
  end
309
309
  resulting_json.should == undent(<<-JSON)
310
- {
310
+ {
311
311
  "name": "Fred",
312
312
  // age is irrelevant
313
313
  "age": 36
314
314
  }
315
315
  JSON
316
316
  end
317
-
317
+
318
318
  end
319
-
319
+
320
320
  end
321
321
 
322
322
  context "by default" do
323
-
323
+
324
324
  it "does not tranform element names" do
325
325
  r.element :user, Object.new do
326
326
  r.element :full_name, "Fred Bloggs"
327
327
  end
328
328
  resulting_json.should == undent(<<-JSON)
329
- {
329
+ {
330
330
  "full_name": "Fred Bloggs"
331
331
  }
332
332
  JSON
333
333
  end
334
334
 
335
335
  end
336
-
336
+
337
337
  context "with naming_strategy :camelcase" do
338
338
 
339
339
  it "generates camelCased element and attribute names" do
@@ -343,7 +343,7 @@ describe Representative::Json do
343
343
  r.element :full_name
344
344
  end
345
345
  resulting_json.should == undent(<<-JSON)
346
- {
346
+ {
347
347
  "@altUrl": "http://xyz.com",
348
348
  "fullName": "Fred Bloggs"
349
349
  }
@@ -352,4 +352,73 @@ describe Representative::Json do
352
352
 
353
353
  end
354
354
 
355
+ context "with :indentation option" do
356
+
357
+ before do
358
+ @authors = [
359
+ OpenStruct.new(:name => "Hewey", :age => 3),
360
+ OpenStruct.new(:name => "Dewey", :age => 4)
361
+ ]
362
+ end
363
+
364
+ context "set to an integer" do
365
+
366
+ it "indents the specified number of spaces" do
367
+ r(:indentation => 3).list_of :authors, @authors do
368
+ r.element :name
369
+ r.element :age
370
+ end
371
+ resulting_json.should == undent(<<-JSON)
372
+ [
373
+ {
374
+ "name": "Hewey",
375
+ "age": 3
376
+ },
377
+ {
378
+ "name": "Dewey",
379
+ "age": 4
380
+ }
381
+ ]
382
+ JSON
383
+ end
384
+
385
+ end
386
+
387
+ context "set to a whitespace String" do
388
+
389
+ it "indents the specified number of spaces" do
390
+ r(:indentation => "\t").list_of :authors, @authors do
391
+ r.element :name
392
+ r.element :age
393
+ end
394
+ resulting_json.should == undent(<<-JSON)
395
+ [
396
+ \t{
397
+ \t\t"name": "Hewey",
398
+ \t\t"age": 3
399
+ \t},
400
+ \t{
401
+ \t\t"name": "Dewey",
402
+ \t\t"age": 4
403
+ \t}
404
+ ]
405
+ JSON
406
+ end
407
+
408
+ end
409
+
410
+ context "set false" do
411
+
412
+ it "does not indent, or wrap" do
413
+ r(:indentation => false).list_of :authors, @authors do
414
+ r.element :name
415
+ r.element :age
416
+ end
417
+ resulting_json.should == %([{"name":"Hewey","age":3},{"name":"Dewey","age":4}])
418
+ end
419
+
420
+ end
421
+
422
+ end
423
+
355
424
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representative
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
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: 2011-11-05 00:00:00.000000000Z
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70110341854200 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,84 +21,60 @@ dependencies:
21
21
  version: 2.2.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70110341854200
25
- - !ruby/object:Gem::Dependency
26
- name: i18n
27
- requirement: &70110341852520 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
28
25
  none: false
29
26
  requirements:
30
27
  - - ! '>='
31
28
  - !ruby/object:Gem::Version
32
- version: 0.4.1
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: *70110341852520
29
+ version: 2.2.2
36
30
  - !ruby/object:Gem::Dependency
37
- name: builder
38
- requirement: &70110341851600 !ruby/object:Gem::Requirement
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
39
33
  none: false
40
34
  requirements:
41
35
  - - ! '>='
42
36
  - !ruby/object:Gem::Version
43
- version: 2.1.2
37
+ version: 0.4.1
44
38
  type: :runtime
45
39
  prerelease: false
46
- version_requirements: *70110341851600
47
- - !ruby/object:Gem::Dependency
48
- name: nokogiri
49
- requirement: &70110341850920 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
50
41
  none: false
51
42
  requirements:
52
43
  - - ! '>='
53
44
  - !ruby/object:Gem::Version
54
- version: 1.4.2
55
- type: :runtime
56
- prerelease: false
57
- version_requirements: *70110341850920
45
+ version: 0.4.1
58
46
  - !ruby/object:Gem::Dependency
59
- name: rake
60
- requirement: &70110341850100 !ruby/object:Gem::Requirement
47
+ name: builder
48
+ requirement: !ruby/object:Gem::Requirement
61
49
  none: false
62
50
  requirements:
63
- - - ~>
51
+ - - ! '>='
64
52
  - !ruby/object:Gem::Version
65
- version: 0.8.7
66
- type: :development
53
+ version: 2.1.2
54
+ type: :runtime
67
55
  prerelease: false
68
- version_requirements: *70110341850100
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: &70110341848720 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
72
57
  none: false
73
58
  requirements:
74
- - - ~>
59
+ - - ! '>='
75
60
  - !ruby/object:Gem::Version
76
- version: 2.5.0
77
- type: :development
78
- prerelease: false
79
- version_requirements: *70110341848720
61
+ version: 2.1.2
80
62
  - !ruby/object:Gem::Dependency
81
- name: tilt
82
- requirement: &70110341841560 !ruby/object:Gem::Requirement
63
+ name: nokogiri
64
+ requirement: !ruby/object:Gem::Requirement
83
65
  none: false
84
66
  requirements:
85
67
  - - ! '>='
86
68
  - !ruby/object:Gem::Version
87
- version: '0.9'
88
- type: :development
69
+ version: 1.4.2
70
+ type: :runtime
89
71
  prerelease: false
90
- version_requirements: *70110341841560
91
- - !ruby/object:Gem::Dependency
92
- name: diff-lcs
93
- requirement: &70110341840580 !ruby/object:Gem::Requirement
72
+ version_requirements: !ruby/object:Gem::Requirement
94
73
  none: false
95
74
  requirements:
96
75
  - - ! '>='
97
76
  - !ruby/object:Gem::Version
98
- version: '0'
99
- type: :development
100
- prerelease: false
101
- version_requirements: *70110341840580
77
+ version: 1.4.2
102
78
  description:
103
79
  email: mdub@dogbiscuit.org
104
80
  executables: []
@@ -139,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
115
  version: '0'
140
116
  segments:
141
117
  - 0
142
- hash: 156870103319875874
118
+ hash: 868732793501301304
143
119
  required_rubygems_version: !ruby/object:Gem::Requirement
144
120
  none: false
145
121
  requirements:
@@ -148,10 +124,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
124
  version: '0'
149
125
  segments:
150
126
  - 0
151
- hash: 156870103319875874
127
+ hash: 868732793501301304
152
128
  requirements: []
153
129
  rubyforge_project:
154
- rubygems_version: 1.8.10
130
+ rubygems_version: 1.8.23
155
131
  signing_key:
156
132
  specification_version: 3
157
133
  summary: Builds XML and JSON representations of your Ruby objects