jbuilder 0.3.2 → 0.4.0

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.
data/README.md CHANGED
@@ -48,13 +48,24 @@ This will build the following structure:
48
48
  { "content": "To you my good sir!", "created_at": "2011-10-29T20:47:28-05:00" }
49
49
  ],
50
50
 
51
- "attachment": [
51
+ "attachments": [
52
52
  { "filename": "forecast.xls", "url": "http://example.com/downloads/forecast.xls" },
53
53
  { "filename": "presentation.pdf", "url": "http://example.com/downloads/presentation.pdf" }
54
54
  ]
55
55
  }
56
56
  ```
57
57
 
58
+ Top level arrays can be handled directly. Useful for index and other collection actions.
59
+
60
+ ``` ruby
61
+ # @people = People.all
62
+ json.array!(@people) do |json, person|
63
+ json.name person.name
64
+ json.age calculate_age(person.birthday)
65
+ end
66
+ # => [ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ]
67
+ ```
68
+
58
69
  You can either use Jbuilder stand-alone or directly as an ActionView template language. When required in Rails, you can create views ala show.json.jbuilder (the json is already yielded):
59
70
 
60
71
  ``` ruby
@@ -72,14 +83,17 @@ if current_user.admin?
72
83
  json.visitors calculate_visitors(@message)
73
84
  end
74
85
 
75
- # You can use partials as well, just remember to pass in the json instance
76
- json.partial! "api/comments/comments", @message.comments
86
+ # You can use partials as well. The following line will render the file
87
+ # RAILS_ROOT/app/views/api/comments/_comments, and set a local variable
88
+ # 'comments' with all this message's comments, which you can use inside
89
+ # the partial.
90
+ json.partial! "api/comments/comments", comments: @message.comments
77
91
  ```
78
92
 
79
- Libraries similar to this in some form or another includes:
93
+ Libraries similar to this in some form or another include:
80
94
 
81
95
  * RABL: https://github.com/nesquena/rabl
82
96
  * JsonBuilder: https://github.com/nov/jsonbuilder
83
97
  * JSON Builder: https://github.com/dewski/json_builder
84
98
  * Jsonify: https://github.com/bsiggelkow/jsonify
85
- * RepresentationView: https://github.com/mdub/representative_view
99
+ * RepresentationView: https://github.com/mdub/representative_view
Binary file
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '0.3.2'
3
+ s.version = '0.4.0'
4
4
  s.author = 'David Heinemeier Hansson'
5
5
  s.email = 'david@37signals.com'
6
6
  s.summary = 'Create JSON structures via a Builder-style DSL'
@@ -126,7 +126,7 @@ class Jbuilder < BlankSlate
126
126
 
127
127
  # json.age 32
128
128
  # { "age": 32 }
129
- when args.one?
129
+ when args.length == 1
130
130
  set! method, args.first
131
131
 
132
132
  # json.comments { |json| ... }
@@ -182,4 +182,4 @@ class Jbuilder < BlankSlate
182
182
  end
183
183
  end
184
184
 
185
- require "jbuilder_template" if defined?(ActionView::Template)
185
+ require "jbuilder_template" if defined?(ActionView::Template)
@@ -12,6 +12,23 @@ class JbuilderTest < ActiveSupport::TestCase
12
12
  assert_equal "hello", JSON.parse(json)["content"]
13
13
  end
14
14
 
15
+ test "single key with false value" do
16
+ json = Jbuilder.encode do |json|
17
+ json.content false
18
+ end
19
+
20
+ assert_equal false, JSON.parse(json)["content"]
21
+ end
22
+
23
+ test "single key with nil value" do
24
+ json = Jbuilder.encode do |json|
25
+ json.content nil
26
+ end
27
+
28
+ assert JSON.parse(json).has_key?("content")
29
+ assert_equal nil, JSON.parse(json)["content"]
30
+ end
31
+
15
32
  test "multiple keys" do
16
33
  json = Jbuilder.encode do |json|
17
34
  json.title "hello"
@@ -225,4 +242,4 @@ class JbuilderTest < ActiveSupport::TestCase
225
242
 
226
243
  assert_equal "stuff", JSON.parse(json)["each"]
227
244
  end
228
- end
245
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
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-01-23 00:00:00.000000000 Z
12
+ date: 2012-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70205624198380 !ruby/object:Gem::Requirement
16
+ requirement: &70191736347120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70205624198380
24
+ version_requirements: *70191736347120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: blankslate
27
- requirement: &70205624197900 !ruby/object:Gem::Requirement
27
+ requirement: &70191736346420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.1.2.4
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70205624197900
35
+ version_requirements: *70191736346420
36
36
  description:
37
37
  email: david@37signals.com
38
38
  executables: []
@@ -42,6 +42,7 @@ files:
42
42
  - ./Gemfile
43
43
  - ./Gemfile.lock
44
44
  - ./jbuilder-0.3.1.gem
45
+ - ./jbuilder-0.3.2.gem
45
46
  - ./jbuilder-0.3.gem
46
47
  - ./jbuilder.gemspec
47
48
  - ./lib/jbuilder.rb