jsonify 0.1.2 → 0.1.3

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.
@@ -1,3 +1,8 @@
1
1
  rvm:
2
+ - 1.9.3
2
3
  - 1.9.2
3
4
  - 1.8.7
5
+ - ree
6
+ - rbx-2.0
7
+ - jruby
8
+
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # Jsonify — a builder for JSON [![Build Status](http://travis-ci.org/bsiggelkow/jsonify.png)](http://travis-ci.org/bsiggelkow/jsonify)
1
+ # Jsonify — a builder for JSON [![Build Status](https://secure.travis-ci.org/bsiggelkow/jsonify.png)](http://travis-ci.org/bsiggelkow/jsonify)
2
2
 
3
3
  [Jsonify](https://github.com/bsiggelkow/jsonify) is to JSON as [Builder](https://github.com/jimweirich/builder) is to XML.
4
4
 
5
- If you want to use Jsonify for Rails templates, install [Jsonify-Rails](https://github.com/bsiggelkow/jsonify-rails).
5
+ To use Jsonify for Rails templates, install [Jsonify-Rails](https://github.com/bsiggelkow/jsonify-rails).
6
6
 
7
7
  ## Goal
8
8
 
@@ -82,9 +82,27 @@ Results in ...
82
82
  ]
83
83
  }
84
84
 
85
- ### View Templates
85
+ #### Convenience methods
86
+ Jsonify provides class-level convenience methods that
87
+ save you the trouble of instantiating the `Jsonify::Builder`. Each of these methods accepts a block, yields a new `Builder` object to the block, and then compiles the result.
86
88
 
87
- Jsonify includes a Rails 3 template handler. Any template with a `.jsonify` will be handled by Rails.
89
+ - `compile`
90
+ - Compiles the given block; any options are passed the instantiated `Builder`
91
+ - `pretty`
92
+ - Compiles the given block; results are output in `pretty` format.
93
+ - `plain`
94
+ - Compiles the given block; results are output in `plain` (default) format.
95
+
96
+ For example ...
97
+
98
+ Jsonify::Builder.plain do |j|
99
+ j.song 'Fearless'
100
+ j.album 'Meddle'
101
+ end
102
+
103
+ ### Rails View Templates
104
+
105
+ Jsonify can be used for Rails 3 view templates via the [jsonify-rails](https://github.com/bsiggelkow/jsonify-rails) which includes a Rails 3 template handler. Any template with a `.jsonify` extension will be handled by Rails.
88
106
 
89
107
  The Jsonify template handler exposes the `Jsonify::Builder` instance to your template with the `json` variable as in the following example:
90
108
 
@@ -93,7 +111,7 @@ The Jsonify template handler exposes the `Jsonify::Builder` instance to your tem
93
111
  end
94
112
 
95
113
  Just like with any other template, your Jsonify template will have access to
96
- any instance variables that are exposed through the controller.
114
+ any instance variables that are exposed through the controller. See [Jsonify-Rails](https://github.com/bsiggelkow/jsonify-rails) for additional details.
97
115
 
98
116
  #### Partials
99
117
 
@@ -151,6 +169,28 @@ You can then incorporate this partial into your Jsonify template just as you wou
151
169
 
152
170
  [1,{"date":"2011-07-30"}]
153
171
 
172
+ ### Tilt Integration
173
+
174
+ Jsonify includes support for [Tilt](http://github.com/rtomayko/tilt). This allow you to create views that use Jsonify with any framework that supports Tilt. Here's an example of a simple [Sinatra](http://sinatrarb.com) application that leverages Jsonify's Tilt integration.
175
+
176
+ require 'bundler/setup'
177
+ require 'sinatra'
178
+
179
+ require 'jsonify'
180
+ require 'jsonify/tilt'
181
+
182
+ helpers do
183
+ def jsonify(*args) render(:jsonify, *args) end
184
+ end
185
+
186
+ get '/' do
187
+ jsonify :index
188
+ end
189
+
190
+ And the corresponding template in `views\index.jsonify`
191
+
192
+ json.hello :frank
193
+
154
194
  ### Usage Patterns
155
195
 
156
196
  Jsonify is designed to support construction of an valid JSON representation and
@@ -363,10 +403,6 @@ In this case, Jsonify decides from the first line that you are creating a JSON o
363
403
  - [Representative](https://github.com/mdub/representative)
364
404
  - [Tokamak](https://github.com/abril/tokamak)
365
405
 
366
- ## TODOs
367
- 1. Benchmark performance
368
- 1. Tilt Integration (jsonify-tilt)
369
-
370
406
  ## License
371
407
 
372
408
  This project is released under the MIT license.
@@ -22,11 +22,9 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency 'json' unless RUBY_VERSION =~ /^1.9/
23
23
 
24
24
  s.add_development_dependency 'bundler'
25
- s.add_development_dependency 'tilt'
25
+ s.add_development_dependency 'tilt', '~>1.3.2'
26
26
  s.add_development_dependency 'rake'
27
27
  s.add_development_dependency 'rspec'
28
- s.add_development_dependency 'autotest'
29
28
  s.add_development_dependency 'yard'
30
29
  s.add_development_dependency 'rdiscount'
31
- s.add_development_dependency 'ruby-prof'
32
30
  end
@@ -3,16 +3,25 @@ module Jsonify
3
3
 
4
4
  class << self
5
5
 
6
+ # Compiles the given block into a JSON string without having to instantiate a Builder.
7
+ #
8
+ # @option options [boolean] :verify Builder will verify that the compiled JSON string is parseable; this option does incur a performance penalty and generally should only be used in development
9
+ # @option options [symbol] :format Format for the resultant JSON string;
10
+ # `:pretty`, the JSON string will be output in a prettier format with new lines and indentation; this option does incur a performance penalty and generally should only be used in development
11
+ # `:plain`, no formatting (compact one-line JSON -- best for production)
12
+ #
6
13
  def compile( options={} )
7
14
  builder = self.new options
8
15
  yield builder
9
16
  builder.compile!
10
17
  end
11
18
 
19
+ # Compiles the given block into a pretty JSON string without having to instantiate a Builder.
12
20
  def pretty(&block)
13
21
  compile( :format => :pretty, &block )
14
22
  end
15
23
 
24
+ # Compiles the given block into a plain (e.g. no newlines and whitespace) JSON string without having to instantiate a Builder.
16
25
  def plain(&block)
17
26
  compile( :format => :plain, &block )
18
27
  end
@@ -43,7 +52,7 @@ module Jsonify
43
52
  # @param sym [String] the key for the pair
44
53
  # @param *args [arguments] If a block is passed, the first argument will be iterated over and the subsequent result will be added to a JSON array; otherwise, the arguments set value for the `JsonPair`
45
54
  # @param &block a code block the result of which will be used to populate the value for the JSON pair
46
- def tag!(sym, *args, &block)
55
+ def tag!(sym, args=nil, &block)
47
56
  method_missing(sym, *args, &block)
48
57
  end
49
58
 
@@ -73,14 +82,16 @@ module Jsonify
73
82
 
74
83
  # Append -- pushes the given object on the end of a JsonArray.
75
84
  def <<(val)
76
- __append(val)
85
+ __array
86
+ @stack[@level].add val
77
87
  self
78
88
  end
79
89
 
80
90
  # Append -- pushes the given variable list objects on to the end of the JsonArray
81
91
  def append!(*args)
92
+ __array
82
93
  args.each do |arg|
83
- __append( arg )
94
+ @stack[@level].add arg
84
95
  end
85
96
  self
86
97
  end
@@ -125,10 +136,10 @@ module Jsonify
125
136
  # ]
126
137
  #
127
138
  # @param *args [Array] iterates over the given array yielding each array item to the block; the result of which is added to a JsonArray
128
- def method_missing(sym, *args, &block)
139
+ def method_missing(sym, args=nil, &block)
129
140
 
130
141
  # When no block given, simply add the symbol and arg as key - value for a JsonPair to current
131
- return __store( sym, args.length > 1 ? args : args.first ) unless block
142
+ return __store( sym, args ) unless block
132
143
 
133
144
  # In a block; create a JSON pair (with no value) and add it to the current object
134
145
  pair = Generate.pair_value(sym)
@@ -137,11 +148,12 @@ module Jsonify
137
148
  # Now process the block
138
149
  @level += 1
139
150
 
140
- if args.empty?
151
+ if args.nil?
141
152
  block.call
142
153
  else
143
- args.first.each do |arg|
144
- __append block.call(arg)
154
+ __array
155
+ args.each do |arg|
156
+ @stack[@level].add block.call(arg)
145
157
  end
146
158
  end
147
159
 
@@ -179,14 +191,14 @@ module Jsonify
179
191
  private
180
192
 
181
193
  # BlankSlate requires the __<method> names
182
-
194
+
183
195
  def __store(key,value=nil)
184
196
  pair = (JsonPair === key ? key : JsonPair.new(key, value))
185
197
  (@stack[@level] ||= JsonObject.new).add(pair)
186
198
  end
187
199
 
188
- def __append(value)
189
- (@stack[@level] ||= JsonArray.new).add value
200
+ def __array
201
+ @stack[@level] ||= JsonArray.new
190
202
  end
191
203
 
192
204
  end
@@ -1,3 +1,3 @@
1
1
  module Jsonify
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -142,7 +142,7 @@ PRETTY_JSON
142
142
  it 'should handle arrays' do
143
143
  json[1] = [2, 3]
144
144
  json[4] = 5
145
- json.compile!.should == '{"1":[2,3],"4":5}'
145
+ JSON.parse(json.compile!).should == JSON.parse('{"1":[2,3],"4":5}')
146
146
  end
147
147
  end
148
148
 
@@ -189,6 +189,15 @@ PRETTY_JSON
189
189
  end
190
190
  json.compile!.should == '{"foo":[2,4,6]}'
191
191
  end
192
+
193
+ it 'hash with empty array by iteration' do
194
+ ary = []
195
+ json.foo(ary) do |n|
196
+ n * 2
197
+ end
198
+ expected = '{"foo":[]}'
199
+ JSON.parse(json.compile!).should == JSON.parse(expected)
200
+ end
192
201
 
193
202
  it 'simple array with object' do
194
203
  json << 1
@@ -223,7 +232,8 @@ PRETTY_JSON
223
232
  describe 'complex array' do
224
233
  it 'should work' do
225
234
  json.bar [1,2,{:foo => 'goo'}]
226
- json.compile!.should == "{\"bar\":[1,2,{\"foo\":\"goo\"}]}"
235
+ expected = "{\"bar\":[1,2,{\"foo\":\"goo\"}]}"
236
+ JSON.parse(json.compile!).should == JSON.parse(expected)
227
237
  end
228
238
  end
229
239
 
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.1.2
4
+ version: 0.1.3
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-08-09 00:00:00.000000000Z
12
+ date: 2011-09-01 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &70122619875620 !ruby/object:Gem::Requirement
16
+ requirement: &70241180309060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,21 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70122619875620
24
+ version_requirements: *70241180309060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: tilt
27
- requirement: &70122619874940 !ruby/object:Gem::Requirement
27
+ requirement: &70241180306880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
- - - ! '>='
30
+ - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: '0'
32
+ version: 1.3.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70122619874940
35
+ version_requirements: *70241180306880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70122619874400 !ruby/object:Gem::Requirement
38
+ requirement: &70241180306020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,21 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70122619874400
46
+ version_requirements: *70241180306020
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70122619873760 !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :development
56
- prerelease: false
57
- version_requirements: *70122619873760
58
- - !ruby/object:Gem::Dependency
59
- name: autotest
60
- requirement: &70122619872980 !ruby/object:Gem::Requirement
49
+ requirement: &70241180303540 !ruby/object:Gem::Requirement
61
50
  none: false
62
51
  requirements:
63
52
  - - ! '>='
@@ -65,10 +54,10 @@ dependencies:
65
54
  version: '0'
66
55
  type: :development
67
56
  prerelease: false
68
- version_requirements: *70122619872980
57
+ version_requirements: *70241180303540
69
58
  - !ruby/object:Gem::Dependency
70
59
  name: yard
71
- requirement: &70122619872260 !ruby/object:Gem::Requirement
60
+ requirement: &70241180302600 !ruby/object:Gem::Requirement
72
61
  none: false
73
62
  requirements:
74
63
  - - ! '>='
@@ -76,21 +65,10 @@ dependencies:
76
65
  version: '0'
77
66
  type: :development
78
67
  prerelease: false
79
- version_requirements: *70122619872260
68
+ version_requirements: *70241180302600
80
69
  - !ruby/object:Gem::Dependency
81
70
  name: rdiscount
82
- requirement: &70122619871560 !ruby/object:Gem::Requirement
83
- none: false
84
- requirements:
85
- - - ! '>='
86
- - !ruby/object:Gem::Version
87
- version: '0'
88
- type: :development
89
- prerelease: false
90
- version_requirements: *70122619871560
91
- - !ruby/object:Gem::Dependency
92
- name: ruby-prof
93
- requirement: &70122619871100 !ruby/object:Gem::Requirement
71
+ requirement: &70241180301860 !ruby/object:Gem::Requirement
94
72
  none: false
95
73
  requirements:
96
74
  - - ! '>='
@@ -98,7 +76,7 @@ dependencies:
98
76
  version: '0'
99
77
  type: :development
100
78
  prerelease: false
101
- version_requirements: *70122619871100
79
+ version_requirements: *70241180301860
102
80
  description: Turn Ruby objects into JSON -- correctly!
103
81
  email:
104
82
  - bsiggelkow@me.com
@@ -144,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
122
  version: '0'
145
123
  segments:
146
124
  - 0
147
- hash: -3850211948781890722
125
+ hash: 415595821988882792
148
126
  required_rubygems_version: !ruby/object:Gem::Requirement
149
127
  none: false
150
128
  requirements:
@@ -153,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
131
  version: '0'
154
132
  segments:
155
133
  - 0
156
- hash: -3850211948781890722
134
+ hash: 415595821988882792
157
135
  requirements: []
158
136
  rubyforge_project: jsonify
159
137
  rubygems_version: 1.8.6