Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Only warn once for each instance a deprecated method is called #42

Merged
merged 5 commits into from

2 participants

@arsduo

Right now, MultiJson prints a deprecation warning every time an outdated method is called. Unfortunately, in some cases this results in a flood of output. The preferred solution is obviously to patch the offending gem (I've submitted a pull request for Resque), but it looks like activesupport-3.1.4 itself uses decode, resulting in a flood of warnings whenever we run our test suite.

This pull request adds a deprecate method, which prints out a warning message once for each message/caller pair (e.g. decode called four times from in two places in the code will generate two messages). This should keep logs clean and manageable, but also get the point across.

I've also updated the readme to reflect the new sytax, no point in telling users to do something that'll generate warnings.

All specs pass; I've run the tests for our application against this fork as well, which pass and show the expected less noisy behavior.

Thanks for the great gem!

@sferik sferik merged commit d05c613 into from
@rosenfeld rosenfeld referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 11 deletions.
  1. +8 −7 README.md
  2. +14 −4 lib/multi_json.rb
View
15 README.md
@@ -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
18 lib/multi_json.rb
@@ -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,7 +102,7 @@ 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
@@ -110,4 +110,14 @@ def encode(object, options={})
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
Something went wrong with that request. Please try again.