json_builder 3.0.6 → 3.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/Gemfile +6 -0
  2. data/Gemfile.lock +6 -105
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +2 -2
  5. data/Rakefile +10 -16
  6. data/json_builder.gemspec +15 -19
  7. data/lib/json_builder.rb +5 -0
  8. data/lib/json_builder/compiler.rb +108 -19
  9. data/lib/json_builder/elements.rb +17 -3
  10. data/lib/json_builder/extensions.rb +3 -3
  11. data/lib/json_builder/member.rb +20 -5
  12. data/lib/json_builder/template.rb +1 -1
  13. data/lib/json_builder/value.rb +15 -3
  14. data/lib/json_builder/version.rb +1 -1
  15. data/{spec → test}/benchmarks/builder.rb +0 -0
  16. data/test/compiler_test.rb +87 -0
  17. data/test/elements_test.rb +21 -0
  18. data/test/extensions_test.rb +47 -0
  19. data/test/member_test.rb +39 -0
  20. data/test/test_helper.rb +22 -0
  21. data/test/value_test.rb +60 -0
  22. metadata +20 -146
  23. data/spec/dummy/Rakefile +0 -7
  24. data/spec/dummy/app/controllers/application_controller.rb +0 -3
  25. data/spec/dummy/app/controllers/users_controller.rb +0 -6
  26. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  27. data/spec/dummy/app/helpers/users_helper.rb +0 -5
  28. data/spec/dummy/app/models/user.rb +0 -11
  29. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  30. data/spec/dummy/app/views/users/index.json_builder +0 -15
  31. data/spec/dummy/config.ru +0 -4
  32. data/spec/dummy/config/application.rb +0 -45
  33. data/spec/dummy/config/boot.rb +0 -10
  34. data/spec/dummy/config/database.yml +0 -22
  35. data/spec/dummy/config/environment.rb +0 -5
  36. data/spec/dummy/config/environments/development.rb +0 -26
  37. data/spec/dummy/config/environments/production.rb +0 -49
  38. data/spec/dummy/config/environments/test.rb +0 -35
  39. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  40. data/spec/dummy/config/initializers/inflections.rb +0 -10
  41. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  42. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  43. data/spec/dummy/config/initializers/session_store.rb +0 -8
  44. data/spec/dummy/config/locales/en.yml +0 -5
  45. data/spec/dummy/config/routes.rb +0 -5
  46. data/spec/dummy/db/development.sqlite3 +0 -0
  47. data/spec/dummy/db/migrate/20111127061428_create_users.rb +0 -14
  48. data/spec/dummy/db/schema.rb +0 -23
  49. data/spec/dummy/db/test.sqlite3 +0 -0
  50. data/spec/dummy/public/404.html +0 -26
  51. data/spec/dummy/public/422.html +0 -26
  52. data/spec/dummy/public/500.html +0 -26
  53. data/spec/dummy/public/favicon.ico +0 -0
  54. data/spec/dummy/public/javascripts/application.js +0 -2
  55. data/spec/dummy/public/javascripts/controls.js +0 -965
  56. data/spec/dummy/public/javascripts/dragdrop.js +0 -974
  57. data/spec/dummy/public/javascripts/effects.js +0 -1123
  58. data/spec/dummy/public/javascripts/prototype.js +0 -6001
  59. data/spec/dummy/public/javascripts/rails.js +0 -191
  60. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  61. data/spec/dummy/script/rails +0 -6
  62. data/spec/dummy/spec/controllers/users_controller_spec.rb +0 -14
  63. data/spec/integration/navigation_spec.rb +0 -9
  64. data/spec/json_builder_spec.rb +0 -7
  65. data/spec/spec_helper.rb +0 -34
  66. data/spec/support/json_builder.rb +0 -7
  67. data/spec/unit/json_builder_spec.rb +0 -77
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ gem 'rake'
6
+
7
+ group :test do
8
+ gem 'i18n'
9
+ end
@@ -1,121 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_builder (3.0.6)
4
+ json_builder (3.0.7)
5
5
  activesupport (>= 2.0.0)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- abstract (1.0.0)
11
- actionmailer (3.0.10)
12
- actionpack (= 3.0.10)
13
- mail (~> 2.2.19)
14
- actionpack (3.0.10)
15
- activemodel (= 3.0.10)
16
- activesupport (= 3.0.10)
17
- builder (~> 2.1.2)
18
- erubis (~> 2.6.6)
19
- i18n (~> 0.5.0)
20
- rack (~> 1.2.1)
21
- rack-mount (~> 0.6.14)
22
- rack-test (~> 0.5.7)
23
- tzinfo (~> 0.3.23)
24
- activemodel (3.0.10)
25
- activesupport (= 3.0.10)
26
- builder (~> 2.1.2)
27
- i18n (~> 0.5.0)
28
- activerecord (3.0.10)
29
- activemodel (= 3.0.10)
30
- activesupport (= 3.0.10)
31
- arel (~> 2.0.10)
32
- tzinfo (~> 0.3.23)
33
- activeresource (3.0.10)
34
- activemodel (= 3.0.10)
35
- activesupport (= 3.0.10)
36
- activesupport (3.0.10)
37
- arel (2.0.10)
38
- builder (2.1.2)
39
- capybara (1.1.1)
40
- mime-types (>= 1.16)
41
- nokogiri (>= 1.3.3)
42
- rack (>= 1.0.0)
43
- rack-test (>= 0.5.4)
44
- selenium-webdriver (~> 2.0)
45
- xpath (~> 0.1.4)
46
- childprocess (0.2.2)
47
- ffi (~> 1.0.6)
48
- diff-lcs (1.1.3)
49
- erubis (2.6.6)
50
- abstract (>= 1.0.0)
51
- ffi (1.0.9)
10
+ activesupport (3.1.3)
11
+ multi_json (~> 1.0)
52
12
  i18n (0.5.0)
53
- json (1.6.1)
54
- json_pure (1.6.1)
55
- mail (2.2.19)
56
- activesupport (>= 2.3.6)
57
- i18n (>= 0.4.0)
58
- mime-types (~> 1.16)
59
- treetop (~> 1.4.8)
60
- mime-types (1.17.2)
61
- nokogiri (1.5.0)
62
- polyglot (0.3.3)
63
- rack (1.2.4)
64
- rack-mount (0.6.14)
65
- rack (>= 1.0.0)
66
- rack-test (0.5.7)
67
- rack (>= 1.0)
68
- rails (3.0.10)
69
- actionmailer (= 3.0.10)
70
- actionpack (= 3.0.10)
71
- activerecord (= 3.0.10)
72
- activeresource (= 3.0.10)
73
- activesupport (= 3.0.10)
74
- bundler (~> 1.0)
75
- railties (= 3.0.10)
76
- railties (3.0.10)
77
- actionpack (= 3.0.10)
78
- activesupport (= 3.0.10)
79
- rake (>= 0.8.7)
80
- rdoc (~> 3.4)
81
- thor (~> 0.14.4)
13
+ multi_json (1.0.4)
82
14
  rake (0.9.2.2)
83
- rdoc (3.11)
84
- json (~> 1.4)
85
- rspec (2.8.0.rc1)
86
- rspec-core (= 2.8.0.rc1)
87
- rspec-expectations (= 2.8.0.rc1)
88
- rspec-mocks (= 2.8.0.rc1)
89
- rspec-core (2.8.0.rc1)
90
- rspec-expectations (2.8.0.rc1)
91
- diff-lcs (~> 1.1.2)
92
- rspec-mocks (2.8.0.rc1)
93
- rspec-rails (2.8.0.rc1)
94
- actionpack (~> 3.0)
95
- activesupport (~> 3.0)
96
- railties (~> 3.0)
97
- rspec (= 2.8.0.rc1)
98
- rubyzip (0.9.4)
99
- selenium-webdriver (2.10.0)
100
- childprocess (>= 0.2.1)
101
- ffi (= 1.0.9)
102
- json_pure
103
- rubyzip
104
- sqlite3 (1.3.4)
105
- thor (0.14.6)
106
- treetop (1.4.10)
107
- polyglot
108
- polyglot (>= 0.3.1)
109
- tzinfo (0.3.31)
110
- xpath (0.1.4)
111
- nokogiri (~> 1.3)
112
15
 
113
16
  PLATFORMS
114
17
  ruby
115
18
 
116
19
  DEPENDENCIES
117
- capybara (>= 0.4.0)
20
+ i18n
118
21
  json_builder!
119
- rails (>= 2.0.0)
120
- rspec-rails (>= 2.0.0.beta)
121
- sqlite3
22
+ rake
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 Garrett Bjerkhoel
1
+ Copyright (c) 2012 Garrett Bjerkhoel
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- JSON Builder
1
+ JSON Builder [![Build Status](https://secure.travis-ci.org/dewski/json_builder.png)](http://travis-ci.org/dewski/json_builder)
2
2
  ============
3
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.
4
4
 
@@ -259,4 +259,4 @@ There are quite a few other alternatives to JSON Builder, each good in their own
259
259
  - Send me a pull request. Bonus points for topic branches.
260
260
 
261
261
  ## Copyright
262
- Copyright © 2011 Garrett Bjerkhoel. See [MIT-LICENSE](http://github.com/dewski/json_builder/blob/master/MIT-LICENSE) for details.
262
+ Copyright © 2012 Garrett Bjerkhoel. See [MIT-LICENSE](http://github.com/dewski/json_builder/blob/master/MIT-LICENSE) for details.
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  # encoding: UTF-8
2
2
  require 'rubygems'
3
+ require 'rake/testtask'
3
4
  begin
4
5
  require 'bundler/setup'
5
6
  Bundler::GemHelper.install_tasks
@@ -7,20 +8,13 @@ rescue LoadError
7
8
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
8
9
  end
9
10
 
10
- require 'rake'
11
- require 'rake/task'
11
+ desc 'Default: run tests'
12
+ task :default => :test
12
13
 
13
- require 'rspec/core'
14
- require 'rspec/core/rake_task'
15
-
16
- RSpec::Core::RakeTask.new(:spec)
17
-
18
- task :default => :spec
19
-
20
- # Rake::Task.new(:rdoc) do |rdoc|
21
- # rdoc.rdoc_dir = 'rdoc'
22
- # rdoc.title = 'JsonBuilder'
23
- # rdoc.options << '--line-numbers' << '--inline-source'
24
- # rdoc.rdoc_files.include('README.rdoc')
25
- # rdoc.rdoc_files.include('lib/**/*.rb')
26
- # end
14
+ desc 'Run JSONBuilder tests.'
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.libs << 'lib'
17
+ t.libs << 'test'
18
+ t.test_files = FileList['test/*_test.rb']
19
+ t.verbose = true
20
+ end
@@ -1,23 +1,19 @@
1
- $:.push File.expand_path("../lib", __FILE__)
2
- require "json_builder/version"
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'json_builder/version'
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "json_builder"
5
+ s.name = 'json_builder'
6
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"]
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
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", ">= 2.0.0"
21
- s.add_development_dependency "sqlite3"
22
- s.add_development_dependency "capybara", ">= 0.4.0"
23
- end
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
+ end
@@ -1,3 +1,8 @@
1
1
  require 'json_builder/version'
2
2
  require 'json_builder/compiler'
3
3
  require 'json_builder/template' if defined? Rails
4
+
5
+ module JSONBuilder
6
+ class InvalidArgument < StandardError; end
7
+ class MissingKeyError < StandardError; end
8
+ end
@@ -3,6 +3,13 @@ require 'json_builder/member'
3
3
  module JSONBuilder
4
4
  class Compiler
5
5
  class << self
6
+ # Public: The helper that builds the JSON structure by calling the
7
+ # specific methods needed to build the JSON.
8
+ #
9
+ # args - Any number of arguments needed for the JSONBuilder::Compiler.
10
+ # block - Yielding a block to generate the JSON.
11
+ #
12
+ # Returns a String.
6
13
  def generate(*args, &block)
7
14
  options = args.extract_options!
8
15
  compiler = self.new(options)
@@ -10,34 +17,68 @@ module JSONBuilder
10
17
  compiler.finalize
11
18
  end
12
19
  end
13
-
20
+
14
21
  attr_accessor :members
15
22
  attr_accessor :array
16
23
  attr_accessor :scope
17
24
  attr_accessor :callback
18
25
  attr_accessor :pretty_print
19
-
26
+
27
+ # Needed to allow for the id key to be used
20
28
  undef_method :id if methods.include? 'id'
21
-
29
+
30
+ # Public: Creates a new Compiler instance used to hold any and
31
+ # all JSONBuilder::Member objects.
32
+ #
33
+ # options - Hash of options used to modify JSON output.
34
+ #
35
+ # Examples
36
+ #
37
+ # json = JSONBuilder::Compiler.new(:callback => false)
38
+ # json.compile do
39
+ # name 'Garrett'
40
+ # end
41
+ # json.finalize
42
+ # # => {"name": "Garrett"}
43
+ #
44
+ # Returns instance of JSONBuilder::Compiler.
22
45
  def initialize(options={})
23
46
  @_members = []
24
47
  @_scope = options[:scope]
25
48
  @_callback = options[:callback] || true
26
49
  @_pretty_print = options[:pretty] || false
27
-
50
+
28
51
  # Only copy instance variables if there is a scope and presence of Rails
29
52
  copy_instance_variables_from(@_scope) if @_scope
30
53
  end
31
-
54
+
55
+ # Public: Takes a block to generate the JSON structure by calling method_missing
56
+ # on all members passed to it through the block.
57
+ #
58
+ # args - An array of values passed to JSONBuilder::Value.
59
+ # block - Yielding a block to generate the JSON.
60
+ #
61
+ # Returns nothing.
32
62
  def compile(*args, &block)
33
63
  instance_exec(*args, &block)
34
64
  end
35
-
65
+
66
+ # Public: Takes a set number of items to generate a plain JSON array response.
67
+ #
68
+ # Returns instance of JSONBuilder::Elements.
36
69
  def array(items, &block)
37
70
  @_array = Elements.new(@_scope, items, &block)
38
71
  end
39
-
40
- # Need to return a Key instance to allow for arrays to be handled appropriately
72
+
73
+ # Public: Called anytime the compiler is passed JSON keys,
74
+ # first checks to see if the parent object contains the method like
75
+ # a Rails helper.
76
+ #
77
+ # key_name - The key for the JSON member.
78
+ # args - An array of values passed to JSONBuilder::Value.
79
+ # block - Yielding any block passed to the element.
80
+ #
81
+ # Returns nothing.
41
82
  def method_missing(key_name, *args, &block)
42
83
  if @_scope.respond_to?(key_name) && !ignore_scope_methods.include?(key_name)
43
84
  @_scope.send(key_name, *args, &block)
@@ -45,42 +86,90 @@ module JSONBuilder
45
86
  key(key_name, *args, &block)
46
87
  end
47
88
  end
48
-
89
+
90
+ # Public: Generates the start of the JSON member. Useful if the key you are
91
+ # generating is dynamic.
92
+ #
93
+ # key - Used to generate the JSON member's key. Can be a String or Symbol.
94
+ # args - An array of values passed to JSONBuilder::Value.
95
+ # block - Yielding any block passed to the element.
96
+ #
97
+ # Examples
98
+ #
99
+ # key :hello, 'Hi'
100
+ # # => "hello": "Hi"
101
+ #
102
+ # key "item-#{rand(0, 500)}", "I'm random!"
103
+ # # => "item-250": "I'm random!"
104
+ #
105
+ # Returns instance of JSONBuilder::Member.
49
106
  def key(key_name, *args, &block)
50
107
  member = Member.new(key_name, @_scope, *args, &block)
51
108
  @_members << member
52
109
  member
53
110
  end
54
-
111
+
112
+ # Public: Combines the output of the compiled members and the change
113
+ # there is a JSONP callback. This is what is returned in the response.
114
+ #
115
+ # Returns a String.
55
116
  def finalize
56
117
  include_callback to_s
57
118
  end
58
-
59
- # Once all nodes are compiled, build the string
119
+
120
+ # Public: Gathers the JSON structure and calls it's compiler within each
121
+ # instance.
122
+ #
123
+ # Returns a String.
60
124
  def to_s
61
125
  @_array ? @_array.to_s : "{#{@_members.collect(&:to_s).join(', ')}}"
62
126
  end
63
-
127
+
64
128
  private
65
-
129
+
130
+ # Private: Determines whether or not to include a JSONP callback in
131
+ # the response.
132
+ #
133
+ # json - The String representation of the JSON structure.
134
+ #
135
+ # Returns a String.
66
136
  def include_callback(json)
67
137
  @_callback && request_params[:callback] ? "#{request_params[:callback]}(#{pretty_print(json)})" : pretty_print(json)
68
138
  end
69
-
139
+
140
+ # Private: Determines whether or not to pass the string through the a
141
+ # JSON prettifier to help with debugging.
142
+ #
143
+ # json - The String representation of the JSON structure.
144
+ #
145
+ # Returns a String.
70
146
  def pretty_print(json)
71
147
  @_pretty_print ? JSON.pretty_generate(JSON[json]) : json
72
148
  end
73
-
149
+
150
+ # Private: Contains the params from the request.
151
+ #
152
+ # Returns a Hash.
74
153
  def request_params
75
154
  @_scope.respond_to?(:params) ? @_scope.params : {}
76
155
  end
77
-
156
+
157
+ # Private: Takes all instance variables from the scope passed to it
158
+ # and makes them available to the block that gets compiled.
159
+ #
160
+ # object - The scope which contains the instance variables.
161
+ # exclude - Any instance variables that should not be set.
162
+ #
163
+ # Returns nothing.
78
164
  def copy_instance_variables_from(object, exclude = []) #:nodoc:
79
165
  vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
80
166
  vars.each { |name| instance_variable_set(name.to_sym, object.instance_variable_get(name)) }
81
167
  end
82
-
83
- # There are some special methods that need to be ignored that may be matched within +@_scope+.
168
+
169
+ # Private: Array of instance variable names that should not be set for
170
+ # the scope.
171
+ #
172
+ # Returns an Array of Symbols.
84
173
  def ignore_scope_methods
85
174
  [:id]
86
175
  end
@@ -1,15 +1,29 @@
1
1
  module JSONBuilder
2
2
  class Elements
3
3
  attr_accessor :compilers
4
-
4
+
5
+ # Public: Creates a new instance of the Elements that generates
6
+ # an array of JSONBuilder::Member objects.
7
+ #
8
+ # scope - The view scope context for any variables.
9
+ # items - The array of elements to create values from.
10
+ # block - Yielding any block passed to the element.
11
+ #
12
+ # Raises InvalidArgument if the items passed does not respond to each.
13
+ # Returns a new instance of Elements.
5
14
  def initialize(scope, items, &block)
15
+ raise InvalidArgument.new('items does not respond to each') unless items.respond_to?(:each)
16
+
6
17
  @compilers = []
7
-
18
+
8
19
  items.each do |item|
9
20
  @compilers << Value.new(scope, item, &block)
10
21
  end
11
22
  end
12
-
23
+
24
+ # Public: Generates the array JSON block local values
25
+ #
26
+ # Returns a formated JSON String
13
27
  def to_s
14
28
  "[#{@compilers.collect(&:to_s).join(', ')}]"
15
29
  end