Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Privatize rails_to_json

  • Loading branch information...
commit 3bd32754e24c4894abd2316eb0be3aeed08cf906 1 parent 4a8d2ef
@jeremy jeremy authored
View
37 activesupport/lib/active_support/json/encoders/date.rb
@@ -1,21 +1,22 @@
class Date
- # Returns a JSON string representing the date. If ActiveSupport.use_standard_json_time_format is set to true, the
- # ISO 8601 format is used.
- #
- # ==== Examples
- #
- # # With ActiveSupport.use_standard_json_time_format = true
- # Date.new(2005,2,1).to_json
- # # => "2005-02-01"
- #
- # # With ActiveSupport.use_standard_json_time_format = false
- # Date.new(2005,2,1).to_json
- # # => "2005/02/01"
- def rails_to_json(options = nil)
- if ActiveSupport.use_standard_json_time_format
- %("#{strftime("%Y-%m-%d")}")
- else
- %("#{strftime("%Y/%m/%d")}")
+ private
+ # Returns a JSON string representing the date. If ActiveSupport.use_standard_json_time_format is set to true, the
+ # ISO 8601 format is used.
+ #
+ # ==== Examples
+ #
+ # # With ActiveSupport.use_standard_json_time_format = true
+ # Date.new(2005,2,1).to_json
+ # # => "2005-02-01"
+ #
+ # # With ActiveSupport.use_standard_json_time_format = false
+ # Date.new(2005,2,1).to_json
+ # # => "2005/02/01"
+ def rails_to_json(options = nil)
+ if ActiveSupport.use_standard_json_time_format
+ %("#{strftime("%Y-%m-%d")}")
+ else
+ %("#{strftime("%Y/%m/%d")}")
+ end
end
- end
end
View
37 activesupport/lib/active_support/json/encoders/date_time.rb
@@ -1,21 +1,22 @@
class DateTime
- # Returns a JSON string representing the datetime. If ActiveSupport.use_standard_json_time_format is set to true, the
- # ISO 8601 format is used.
- #
- # ==== Examples
- #
- # # With ActiveSupport.use_standard_json_time_format = true
- # DateTime.civil(2005,2,1,15,15,10).to_json
- # # => "2005-02-01T15:15:10+00:00"
- #
- # # With ActiveSupport.use_standard_json_time_format = false
- # DateTime.civil(2005,2,1,15,15,10).to_json
- # # => "2005/02/01 15:15:10 +0000"
- def rails_to_json(options = nil)
- if ActiveSupport.use_standard_json_time_format
- xmlschema.inspect
- else
- strftime('"%Y/%m/%d %H:%M:%S %z"')
+ private
+ # Returns a JSON string representing the datetime. If ActiveSupport.use_standard_json_time_format is set to true, the
+ # ISO 8601 format is used.
+ #
+ # ==== Examples
+ #
+ # # With ActiveSupport.use_standard_json_time_format = true
+ # DateTime.civil(2005,2,1,15,15,10).to_json
+ # # => "2005-02-01T15:15:10+00:00"
+ #
+ # # With ActiveSupport.use_standard_json_time_format = false
+ # DateTime.civil(2005,2,1,15,15,10).to_json
+ # # => "2005/02/01 15:15:10 +0000"
+ def rails_to_json(options = nil)
+ if ActiveSupport.use_standard_json_time_format
+ xmlschema.inspect
+ else
+ strftime('"%Y/%m/%d %H:%M:%S %z"')
+ end
end
- end
end
View
21 activesupport/lib/active_support/json/encoders/enumerable.rb
@@ -1,12 +1,13 @@
module Enumerable
- # Returns a JSON string representing the enumerable. Any +options+
- # given will be passed on to its elements. For example:
- #
- # users = User.find(:all)
- # # => users.to_json(:only => :name)
- #
- # will pass the <tt>:only => :name</tt> option to each user.
- def rails_to_json(options = nil) #:nodoc:
- "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]"
- end
+ private
+ # Returns a JSON string representing the enumerable. Any +options+
+ # given will be passed on to its elements. For example:
+ #
+ # users = User.find(:all)
+ # # => users.to_json(:only => :name)
+ #
+ # will pass the <tt>:only => :name</tt> option to each user.
+ def rails_to_json(options = nil) #:nodoc:
+ "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]"
+ end
end
View
7 activesupport/lib/active_support/json/encoders/false_class.rb
@@ -1,5 +1,6 @@
class FalseClass
- def rails_to_json(options = nil) #:nodoc:
- 'false'
- end
+ private
+ def rails_to_json(options = nil)
+ 'false'
+ end
end
View
87 activesupport/lib/active_support/json/encoders/hash.rb
@@ -1,50 +1,51 @@
require 'active_support/core_ext/array/wrap'
class Hash
- # Returns a JSON string representing the hash.
- #
- # Without any +options+, the returned JSON string will include all
- # the hash keys. For example:
- #
- # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
- # # => {"name": "Konata Izumi", "1": 2, "age": 16}
- #
- # The keys in the JSON string are unordered due to the nature of hashes.
- #
- # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
- # attributes included, and will accept 1 or more hash keys to include/exclude.
- #
- # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:only => [:name, 'age'])
- # # => {"name": "Konata Izumi", "age": 16}
- #
- # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:except => 1)
- # # => {"name": "Konata Izumi", "age": 16}
- #
- # The +options+ also filter down to any hash values. This is particularly
- # useful for converting hashes containing ActiveRecord objects or any object
- # that responds to options in their <tt>to_json</tt> method. For example:
- #
- # users = User.find(:all)
- # { :users => users, :count => users.size }.to_json(:include => :posts)
- #
- # would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
- # allowing the posts association in the User model to be converted to JSON
- # as well.
- def rails_to_json(options = nil) #:nodoc:
- hash_keys = self.keys
+ private
+ # Returns a JSON string representing the hash.
+ #
+ # Without any +options+, the returned JSON string will include all
+ # the hash keys. For example:
+ #
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
+ # # => {"name": "Konata Izumi", "1": 2, "age": 16}
+ #
+ # The keys in the JSON string are unordered due to the nature of hashes.
+ #
+ # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
+ # attributes included, and will accept 1 or more hash keys to include/exclude.
+ #
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:only => [:name, 'age'])
+ # # => {"name": "Konata Izumi", "age": 16}
+ #
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:except => 1)
+ # # => {"name": "Konata Izumi", "age": 16}
+ #
+ # The +options+ also filter down to any hash values. This is particularly
+ # useful for converting hashes containing ActiveRecord objects or any object
+ # that responds to options in their <tt>to_json</tt> method. For example:
+ #
+ # users = User.find(:all)
+ # { :users => users, :count => users.size }.to_json(:include => :posts)
+ #
+ # would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
+ # allowing the posts association in the User model to be converted to JSON
+ # as well.
+ def rails_to_json(options = nil) #:nodoc:
+ hash_keys = self.keys
- if options
- if except = options[:except]
- hash_keys = hash_keys - Array.wrap(except)
- elsif only = options[:only]
- hash_keys = hash_keys & Array.wrap(only)
+ if options
+ if except = options[:except]
+ hash_keys = hash_keys - Array.wrap(except)
+ elsif only = options[:only]
+ hash_keys = hash_keys & Array.wrap(only)
+ end
end
- end
- result = '{'
- result << hash_keys.map do |key|
- "#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(self[key], options)}"
- end * ','
- result << '}'
- end
+ result = '{'
+ result << hash_keys.map do |key|
+ "#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(self[key], options)}"
+ end * ','
+ result << '}'
+ end
end
View
7 activesupport/lib/active_support/json/encoders/nil_class.rb
@@ -1,5 +1,6 @@
class NilClass
- def rails_to_json(options = nil) #:nodoc:
- 'null'
- end
+ private
+ def rails_to_json(options = nil)
+ 'null'
+ end
end
View
7 activesupport/lib/active_support/json/encoders/numeric.rb
@@ -1,5 +1,6 @@
class Numeric
- def rails_to_json(options = nil) #:nodoc:
- to_s
- end
+ private
+ def rails_to_json(options = nil)
+ to_s
+ end
end
View
9 activesupport/lib/active_support/json/encoders/object.rb
@@ -2,9 +2,12 @@
class Object
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
- def rails_to_json(options = nil)
- ActiveSupport::JSON.encode(instance_values, options)
+ def to_json(options = nil)
+ rails_to_json(options)
end
- alias to_json rails_to_json
+ private
+ def rails_to_json(options = nil)
+ ActiveSupport::JSON.encode(instance_values, options)
+ end
end
View
7 activesupport/lib/active_support/json/encoders/regexp.rb
@@ -1,5 +1,6 @@
class Regexp
- def rails_to_json(options = nil) #:nodoc:
- inspect
- end
+ private
+ def rails_to_json(options = nil)
+ inspect
+ end
end
View
7 activesupport/lib/active_support/json/encoders/string.rb
@@ -1,5 +1,6 @@
class String
- def rails_to_json(options = nil) #:nodoc:
- ActiveSupport::JSON::Encoding.escape(self)
- end
+ private
+ def rails_to_json(options = nil)
+ ActiveSupport::JSON::Encoding.escape(self)
+ end
end
View
7 activesupport/lib/active_support/json/encoders/symbol.rb
@@ -1,5 +1,6 @@
class Symbol
- def rails_to_json(options = nil) #:nodoc:
- ActiveSupport::JSON.encode(to_s, options)
- end
+ private
+ def rails_to_json(options = nil)
+ ActiveSupport::JSON.encode(to_s, options)
+ end
end
View
37 activesupport/lib/active_support/json/encoders/time.rb
@@ -1,23 +1,24 @@
require 'active_support/core_ext/time/conversions'
class Time
- # Returns a JSON string representing the time. If ActiveSupport.use_standard_json_time_format is set to true, the
- # ISO 8601 format is used.
- #
- # ==== Examples
- #
- # # With ActiveSupport.use_standard_json_time_format = true
- # Time.utc(2005,2,1,15,15,10).to_json
- # # => "2005-02-01T15:15:10Z"
- #
- # # With ActiveSupport.use_standard_json_time_format = false
- # Time.utc(2005,2,1,15,15,10).to_json
- # # => "2005/02/01 15:15:10 +0000"
- def rails_to_json(options = nil)
- if ActiveSupport.use_standard_json_time_format
- xmlschema.inspect
- else
- %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
+ private
+ # Returns a JSON string representing the time. If ActiveSupport.use_standard_json_time_format is set to true, the
+ # ISO 8601 format is used.
+ #
+ # ==== Examples
+ #
+ # # With ActiveSupport.use_standard_json_time_format = true
+ # Time.utc(2005,2,1,15,15,10).to_json
+ # # => "2005-02-01T15:15:10Z"
+ #
+ # # With ActiveSupport.use_standard_json_time_format = false
+ # Time.utc(2005,2,1,15,15,10).to_json
+ # # => "2005/02/01 15:15:10 +0000"
+ def rails_to_json(options = nil)
+ if ActiveSupport.use_standard_json_time_format
+ xmlschema.inspect
+ else
+ %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
+ end
end
- end
end
View
7 activesupport/lib/active_support/json/encoders/true_class.rb
@@ -1,5 +1,6 @@
class TrueClass
- def rails_to_json(options = nil) #:nodoc:
- 'true'
- end
+ private
+ def rails_to_json(options = nil)
+ 'true'
+ end
end
View
7 activesupport/lib/active_support/json/variable.rb
@@ -2,9 +2,10 @@ module ActiveSupport
module JSON
# A string that returns itself as its JSON-encoded form.
class Variable < String
- def rails_to_json(options=nil)
- self
- end
+ private
+ def rails_to_json(options = nil)
+ self
+ end
end
end
end
View
42 activesupport/lib/active_support/time_with_zone.rb
@@ -118,28 +118,6 @@ def xmlschema(fraction_digits = 0)
end
alias_method :iso8601, :xmlschema
- # Returns a JSON string representing the TimeWithZone. If ActiveSupport.use_standard_json_time_format is set to
- # true, the ISO 8601 format is used.
- #
- # ==== Examples
- #
- # # With ActiveSupport.use_standard_json_time_format = true
- # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
- # # => "2005-02-01T15:15:10Z"
- #
- # # With ActiveSupport.use_standard_json_time_format = false
- # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
- # # => "2005/02/01 15:15:10 +0000"
- def rails_to_json(options = nil)
- if !ActiveSupport.respond_to?(:use_standard_json_time_format) || ActiveSupport.use_standard_json_time_format
- xmlschema.inspect
- else
- %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
- end
- end
-
- alias to_json rails_to_json
-
def to_yaml(options = {})
if options.kind_of?(YAML::Emitter)
utc.to_yaml(options)
@@ -323,6 +301,26 @@ def method_missing(sym, *args, &block)
end
private
+ # Returns a JSON string representing the TimeWithZone. If ActiveSupport.use_standard_json_time_format is set to
+ # true, the ISO 8601 format is used.
+ #
+ # ==== Examples
+ #
+ # # With ActiveSupport.use_standard_json_time_format = true
+ # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
+ # # => "2005-02-01T15:15:10Z"
+ #
+ # # With ActiveSupport.use_standard_json_time_format = false
+ # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
+ # # => "2005/02/01 15:15:10 +0000"
+ def rails_to_json(options = nil)
+ if !ActiveSupport.respond_to?(:use_standard_json_time_format) || ActiveSupport.use_standard_json_time_format
+ xmlschema.inspect
+ else
+ %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
+ end
+ end
+
def get_period_and_ensure_valid_local_time
# we don't want a Time.local instance enforcing its own DST rules as well,
# so transfer time values to a utc constructor if necessary
View
2  activesupport/test/json/encoding_test.rb
@@ -134,6 +134,6 @@ def test_enumerable_should_passthrough_options_to_elements
ActiveSupport::JSON.expects(:encode).with(2, json_options)
ActiveSupport::JSON.expects(:encode).with('foo', json_options)
- [1, 2, 'foo'].rails_to_json(json_options)
+ [1, 2, 'foo'].send(:rails_to_json, json_options)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.