Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
wrap methods instead of delegation for versions <= ruby 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chancefeick committed Sep 23, 2016
1 parent b7c984c commit fba1555
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions lib/librato/metrics/smart_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,38 @@ class SmartJSON
extend SingleForwardable

if defined?(::MultiJson)
# MultiJSON >= 1.3.0
if MultiJson.respond_to?(:load)
def_delegator MultiJson, :load, :read
else
def_delegator MultiJson, :decode, :read
end
if RUBY_VERSION <= "2.3.0"
# MultiJSON >= 1.3.0
if MultiJson.respond_to?(:load)
def_delegator MultiJson, :load, :read
else
def_delegator MultiJson, :decode, :read
end

# MultiJSON <= 1.2.0
if MultiJson.respond_to?(:dump)
def_delegator MultiJson, :dump, :write
# MultiJSON <= 1.2.0
if MultiJson.respond_to?(:dump)
def_delegator MultiJson, :dump, :write
else
def_delegator MultiJson, :encode, :write
end
else
def_delegator MultiJson, :encode, :write
def self.read(json)
# MultiJSON >= 1.3.0
if MultiJson.respond_to?(:load)
MultiJson.load(json)
else
MultiJson.decode(json)
end
end

def self.write(json)
# MultiJSON <= 1.2.0
if MultiJson.respond_to?(:dump)
MultiJson.dump(json)
else
MultiJson.encode(json)
end
end
end

def self.handler
Expand All @@ -24,8 +44,18 @@ def self.handler
else
require "json"

def_delegator JSON, :parse, :read
def_delegator JSON, :generate, :write
if RUBY_VERSION <= "2.3.0"
def_delegator JSON, :parse, :read
def_delegator JSON, :generate, :write
else
def self.read(json)
JSON.parse(json)
end

def self.write(json)
JSON.generate(json)
end
end

def self.handler
:json
Expand Down

0 comments on commit fba1555

Please sign in to comment.