Permalink
Browse files

Merge pull request #42 from arsduo/master

Only warn once for each instance a deprecated method is called
  • Loading branch information...
2 parents 2989d6e + 527a058 commit d05c613df1d20def5599af16d063bf0f42c4cf06 @sferik sferik committed Apr 16, 2012
Showing with 22 additions and 11 deletions.
  1. +8 −7 README.md
  2. +14 −4 lib/multi_json.rb
View
@@ -10,13 +10,14 @@ fastest available JSON coder. Here's how to use it:
require 'multi_json'
- MultiJson.decode('{"abc":"def"}') #=> {"abc" => "def"}
- MultiJson.decode('{"abc":"def"}', :symbolize_keys => true) #=> {:abc => "def"}
- MultiJson.encode({:abc => 'def'}) # convert Ruby back to JSON
- MultiJson.encode({:abc => 'def'}, :pretty => true) # encoded in a pretty form (if supported by the coder)
-
-The `engine` setter takes either a symbol or a class (to allow for custom JSON
-parsers) that responds to both `.decode` and `.encode` at the class level.
+ MultiJson.load('{"abc":"def"}') #=> {"abc" => "def"}
+ MultiJson.load('{"abc":"def"}', :symbolize_keys => true) #=> {:abc => "def"}
+ MultiJson.dump({:abc => 'def'}) # convert Ruby back to JSON
+ MultiJson.dump({:abc => 'def'}, :pretty => true) # encoded in a pretty form (if supported by the coder)
+
+The `use` method, which sets the MultiJson adapter, takes either a symbol or a
+class (to allow for custom JSON parsers) that responds to both `.load` and `.dump`
+at the class level.
MultiJSON tries to have intelligent defaulting. That is, if you have any of the
supported engines already loaded, it will utilize them before attempting to
View
@@ -43,7 +43,7 @@ def default_adapter
# TODO: Remove for 2.0 release (but no sooner)
def engine
- Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.engine is deprecated and will be removed in the next major version. Use MultiJson.adapter instead."
+ deprecate("MultiJson.engine is deprecated and will be removed in the next major version. Use MultiJson.adapter instead.")
self.adapter
end
@@ -56,7 +56,7 @@ def adapter
# TODO: Remove for 2.0 release (but no sooner)
def adapter=(new_adapter)
- Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.adapter= is deprecated and will be removed in the next major version. Use MultiJson.use instead."
+ deprecate("MultiJson.adapter= is deprecated and will be removed in the next major version. Use MultiJson.use instead.")
self.use(new_adapter)
end
@@ -85,7 +85,7 @@ def use(new_adapter)
# TODO: Remove for 2.0 release (but no sooner)
def decode(string, options={})
- Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.decode is deprecated and will be removed in the next major version. Use MultiJson.load instead."
+ deprecate("MultiJson.decode is deprecated and will be removed in the next major version. Use MultiJson.load instead.")
self.load(string, options)
end
@@ -102,12 +102,22 @@ def load(string, options={})
# TODO: Remove for 2.0 release (but no sooner)
def encode(object, options={})
- Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead."
+ deprecate("MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead.")
self.dump(object, options)
end
# Encodes a Ruby object as JSON.
def dump(object, options={})
adapter.dump(object, options)
end
+
+ # Sends of a deprecation warning
+ def deprecate(raw_message)
+ @messages ||= {}
+ message = "#{Kernel.caller[1]}: [DEPRECATION] #{raw_message}"
+ unless @messages[message]
+ @messages[message] = true
+ Kernel.warn message
+ end
+ end
end

0 comments on commit d05c613

Please sign in to comment.