lib/alba.rb in alba-0.9.0 vs lib/alba.rb in alba-0.10.0
- old
+ new
@@ -5,10 +5,11 @@
require 'alba/resources/default_resource'
# Core module
module Alba
class Error < StandardError; end
+ class UnsupportedBackend < Error; end
class << self
attr_reader :backend, :encoder
attr_accessor :default_serializer
@@ -29,27 +30,31 @@
private
def set_encoder
@encoder = case @backend
when :oj
- begin
- require 'oj'
- ->(hash) { Oj.dump(hash, mode: :strict) }
- rescue LoadError
- default_encoder
- end
+ try_oj
when :active_support
- begin
- require 'active_support/json'
- ->(hash) { ActiveSupport::JSON.encode(hash) }
- rescue LoadError
- default_encoder
- end
+ try_active_support
when nil, :default, :json
default_encoder
else
- raise Alba::Error, "Unsupported backend, #{backend}"
+ raise Alba::UnsupportedBackend, "Unsupported backend, #{backend}"
end
+ end
+
+ def try_oj
+ require 'oj'
+ ->(hash) { Oj.dump(hash, mode: :strict) }
+ rescue LoadError
+ default_encoder
+ end
+
+ def try_active_support
+ require 'active_support/json'
+ ->(hash) { ActiveSupport::JSON.encode(hash) }
+ rescue LoadError
+ default_encoder
end
def default_encoder
lambda do |hash|
require 'json'