json_builder 3.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/Gemfile +0 -10
- data/Gemfile.lock +1 -2
- data/README.md +7 -1
- data/json_builder.gemspec +23 -0
- data/lib/json_builder/compiler.rb +15 -7
- data/lib/json_builder/member.rb +1 -1
- data/lib/json_builder/template.rb +10 -4
- data/lib/json_builder/version.rb +1 -1
- data/spec/dummy/app/controllers/users_controller.rb +1 -1
- data/spec/dummy/app/models/user.rb +2 -0
- data/spec/dummy/app/views/users/index.json_builder +9 -9
- data/spec/dummy/config/routes.rb +1 -1
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/unit/json_builder_spec.rb +3 -2
- metadata +43 -9
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -1,13 +1,3 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
3
|
gemspec
|
4
|
-
|
5
|
-
gem "rails", "3.0.10"
|
6
|
-
gem "capybara", ">= 0.4.0"
|
7
|
-
gem "sqlite3"
|
8
|
-
|
9
|
-
gem "rspec-rails", ">= 2.0.0.beta"
|
10
|
-
|
11
|
-
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
|
12
|
-
# gem 'ruby-debug'
|
13
|
-
# gem 'ruby-debug19'
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
json_builder (3.0.
|
4
|
+
json_builder (3.0.3)
|
5
5
|
activesupport (>= 2.0.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -117,6 +117,5 @@ DEPENDENCIES
|
|
117
117
|
capybara (>= 0.4.0)
|
118
118
|
json_builder!
|
119
119
|
rails (= 3.0.10)
|
120
|
-
rspec
|
121
120
|
rspec-rails (>= 2.0.0.beta)
|
122
121
|
sqlite3
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
JSON Builder
|
2
|
+
============
|
2
3
|
Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. The standard `to_json` works just fine, but can get very verbose when you need full control of what is generated and performance is a factor. JSON Builder hopes to solve that problem.
|
3
4
|
|
4
5
|
## Sample Usage
|
@@ -9,6 +10,7 @@ require 'json_builder'
|
|
9
10
|
json = JSONBuilder::Compiler.generate do
|
10
11
|
name "Garrett Bjerkhoel"
|
11
12
|
email "spam@garrettbjerkhoel.com"
|
13
|
+
url root_path
|
12
14
|
address do
|
13
15
|
street "1234 1st Ave"
|
14
16
|
street2 "Apt 1"
|
@@ -34,6 +36,7 @@ Which will generate:
|
|
34
36
|
{
|
35
37
|
"name": "Garrett Bjerkhoel",
|
36
38
|
"email": "spam@garrettbjerkhoel.com",
|
39
|
+
"url": "/",
|
37
40
|
"address": {
|
38
41
|
"street": "1234 1st Ave",
|
39
42
|
"street2": "Apt 1",
|
@@ -108,6 +111,7 @@ results @users do |user|
|
|
108
111
|
id user.id
|
109
112
|
name user.name
|
110
113
|
body user.body
|
114
|
+
url user_url(url)
|
111
115
|
links user.links do |link|
|
112
116
|
url link.url
|
113
117
|
visits link.visits
|
@@ -129,6 +133,7 @@ You will get something like:
|
|
129
133
|
"id": 1,
|
130
134
|
"name": "Garrett Bjerkhoel",
|
131
135
|
"body": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod.",
|
136
|
+
"url": "http://example.com/users/garrett-bjerkhoel",
|
132
137
|
"links": [
|
133
138
|
{
|
134
139
|
"url": "http://github.com/",
|
@@ -145,6 +150,7 @@ You will get something like:
|
|
145
150
|
"id": 2,
|
146
151
|
"name": "John Doe",
|
147
152
|
"body": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod.",
|
153
|
+
"url": "http://example.com/users/john-doe",
|
148
154
|
"links": [
|
149
155
|
{
|
150
156
|
"url": "http://google.com/",
|
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "json_builder/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "json_builder"
|
6
|
+
s.version = JSONBuilder::VERSION
|
7
|
+
s.summary = "Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. The standard to_json works well, but can get very verbose when you need full control of what is generated. JSON Builder hopes to solve that problem."
|
8
|
+
s.description = "Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. The standard to_json works well, but can get very verbose when you need full control of what is generated. JSON Builder hopes to solve that problem."
|
9
|
+
s.authors = ["Garrett Bjerkhoel"]
|
10
|
+
s.email = ["me@garrettbjerkhoel.com"]
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_dependency "activesupport", ">= 2.0.0"
|
19
|
+
s.add_development_dependency "rspec-rails", ">= 2.0.0.beta"
|
20
|
+
s.add_development_dependency "rails", "3.0.10"
|
21
|
+
s.add_development_dependency "sqlite3"
|
22
|
+
s.add_development_dependency "capybara", ">= 0.4.0"
|
23
|
+
end
|
@@ -37,16 +37,19 @@ module JSONBuilder
|
|
37
37
|
end
|
38
38
|
|
39
39
|
# Need to return a Key instance to allow for arrays to be handled appropriately
|
40
|
-
def method_missing(
|
41
|
-
if @_scope.respond_to?(
|
42
|
-
@_scope.send(
|
40
|
+
def method_missing(key_name, *args, &block)
|
41
|
+
if @_scope.respond_to?(key_name) && !ignore_scope_methods.include?(key_name)
|
42
|
+
@_scope.send(key_name, *args, &block)
|
43
43
|
else
|
44
|
-
|
45
|
-
@_members << member
|
46
|
-
member
|
44
|
+
key(key_name, *args, &block)
|
47
45
|
end
|
48
46
|
end
|
49
|
-
|
47
|
+
|
48
|
+
def key(key_name, *args, &block)
|
49
|
+
member = Member.new(key_name, @_scope, *args, &block)
|
50
|
+
@_members << member
|
51
|
+
member
|
52
|
+
end
|
50
53
|
|
51
54
|
# Once all nodes are compiled, build the string
|
52
55
|
def to_s
|
@@ -71,5 +74,10 @@ module JSONBuilder
|
|
71
74
|
vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
|
72
75
|
vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
|
73
76
|
end
|
77
|
+
|
78
|
+
# There are some special methods that need to be ignored that may be matched within +@_scope+.
|
79
|
+
def ignore_scope_methods
|
80
|
+
[:id]
|
81
|
+
end
|
74
82
|
end
|
75
83
|
end
|
data/lib/json_builder/member.rb
CHANGED
@@ -10,7 +10,7 @@ module JSONBuilder
|
|
10
10
|
@key = key
|
11
11
|
|
12
12
|
argument = args.shift
|
13
|
-
if argument.is_a?(Array)
|
13
|
+
if argument.is_a?(Array) || defined?(ActiveRecord::Relation) && argument.is_a?(ActiveRecord::Relation)
|
14
14
|
@value = Elements.new(scope, argument, &block)
|
15
15
|
else
|
16
16
|
@value = Value.new(scope, argument, &block)
|
@@ -18,9 +18,11 @@ if defined?(Rails) && Rails.version =~ /^2/
|
|
18
18
|
class JSONBuilder < TemplateHandler
|
19
19
|
include Compilable
|
20
20
|
|
21
|
-
def compile(template)
|
22
|
-
|
23
|
-
|
21
|
+
def compile(template) %{
|
22
|
+
::JSONBuilder::Compiler.generate(:scope => self, :pretty => ActionView::Base.pretty_print_json, :callback => ActionView::Base.json_callback) {
|
23
|
+
#{template.source}
|
24
|
+
}
|
25
|
+
} end
|
24
26
|
end
|
25
27
|
end
|
26
28
|
end
|
@@ -43,7 +45,11 @@ if defined?(Rails) && Rails.version =~ /^3/
|
|
43
45
|
template.source
|
44
46
|
end
|
45
47
|
|
46
|
-
|
48
|
+
%{
|
49
|
+
::JSONBuilder::Compiler.generate(:scope => self, :pretty => ActionView::Base.pretty_print_json, :callback => ActionView::Base.json_callback) {
|
50
|
+
#{template.source}
|
51
|
+
}
|
52
|
+
}
|
47
53
|
end
|
48
54
|
end
|
49
55
|
end
|
data/lib/json_builder/version.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
url
|
1
|
+
array @users do |user|
|
2
|
+
id user.id
|
3
|
+
name user.name
|
4
|
+
email { user.email }
|
5
|
+
url user_path(user)
|
6
6
|
help included_helper
|
7
|
-
|
8
|
-
|
7
|
+
created_at user.created_at
|
8
|
+
updated_at user.updated_at
|
9
9
|
deep do
|
10
|
+
key :included_helper, included_helper
|
10
11
|
node included_helper
|
11
12
|
url root_path
|
12
13
|
end
|
13
14
|
end
|
14
|
-
|
15
|
-
name @user.name
|
15
|
+
# last line comment without \n
|
data/spec/dummy/config/routes.rb
CHANGED
Binary file
|
@@ -36,7 +36,7 @@ describe "JSONBuilder" do
|
|
36
36
|
date Date.new(2011, 11, 23)
|
37
37
|
date_time DateTime.new(2001, 2, 3, 4, 5, 6)
|
38
38
|
timed Time.at(1322427883)
|
39
|
-
end.should == '{"date": "2011-11-23", "date_time": "2001-02-03T04:05:06+00:00", "timed": "2011-11-
|
39
|
+
end.should == '{"date": "2011-11-23", "date_time": "2001-02-03T04:05:06+00:00", "timed": "2011-11-27T13:04:43-08:00"}'
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should support multiple nestings" do
|
@@ -60,12 +60,13 @@ describe "JSONBuilder" do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
key :custom_key, 1
|
63
|
+
key :with_method, with_method
|
63
64
|
key 'as_string', true
|
64
65
|
nested do
|
65
66
|
key "deep_down", -1
|
66
67
|
end
|
67
68
|
key with_method, "chuck"
|
68
|
-
end.should == '{"custom_key": 1, "as_string": true, "nested": {"deep_down": -1}, "nope": "chuck"}'
|
69
|
+
end.should == '{"custom_key": 1, "with_method": "nope", "as_string": true, "nested": {"deep_down": -1}, "nope": "chuck"}'
|
69
70
|
end
|
70
71
|
|
71
72
|
it "should support adding hash objects" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.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-
|
12
|
+
date: 2011-12-02 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70325733616220 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,32 @@ dependencies:
|
|
21
21
|
version: 2.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70325733616220
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name: rspec
|
27
|
-
requirement: &
|
26
|
+
name: rspec-rails
|
27
|
+
requirement: &70325733615700 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0.beta
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70325733615700
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rails
|
38
|
+
requirement: &70325733615220 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - =
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.0.10
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70325733615220
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &70325733614800 !ruby/object:Gem::Requirement
|
28
50
|
none: false
|
29
51
|
requirements:
|
30
52
|
- - ! '>='
|
@@ -32,7 +54,18 @@ dependencies:
|
|
32
54
|
version: '0'
|
33
55
|
type: :development
|
34
56
|
prerelease: false
|
35
|
-
version_requirements: *
|
57
|
+
version_requirements: *70325733614800
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: capybara
|
60
|
+
requirement: &70325733614140 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 0.4.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70325733614140
|
36
69
|
description: Rails provides an excellent XML Builder by default to build RSS and ATOM
|
37
70
|
feeds, but nothing to help you build complex and custom JSON data structures. The
|
38
71
|
standard to_json works well, but can get very verbose when you need full control
|
@@ -49,6 +82,7 @@ files:
|
|
49
82
|
- MIT-LICENSE
|
50
83
|
- README.md
|
51
84
|
- Rakefile
|
85
|
+
- json_builder.gemspec
|
52
86
|
- lib/blankslate.rb
|
53
87
|
- lib/json_builder.rb
|
54
88
|
- lib/json_builder/compiler.rb
|
@@ -119,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
153
|
version: '0'
|
120
154
|
segments:
|
121
155
|
- 0
|
122
|
-
hash:
|
156
|
+
hash: 3125274407060127813
|
123
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
158
|
none: false
|
125
159
|
requirements:
|
@@ -128,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
162
|
version: '0'
|
129
163
|
segments:
|
130
164
|
- 0
|
131
|
-
hash:
|
165
|
+
hash: 3125274407060127813
|
132
166
|
requirements: []
|
133
167
|
rubyforge_project:
|
134
168
|
rubygems_version: 1.8.10
|