lib/alba.rb in alba-1.0.0 vs lib/alba.rb in alba-1.0.1
- old
+ new
@@ -31,12 +31,14 @@
# @return [String] serialized JSON string
# @raise [ArgumentError] if block is absent or `with` argument's type is wrong
def serialize(object, key: nil, &block)
raise ArgumentError, 'Block required' unless block
- resource_class.class_eval(&block)
- resource = resource_class.new(object)
+ klass = Class.new
+ klass.include(Alba::Resource)
+ klass.class_eval(&block)
+ resource = klass.new(object)
resource.serialize(key: key)
end
# Enable inference for key and resource name
def enable_inference!
@@ -59,11 +61,10 @@
# @param [Block]
def on_error(handler = nil, &block)
raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block
raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block
- p block if block
@_on_error = handler || block
end
private
@@ -82,30 +83,25 @@
def try_oj
require 'oj'
->(hash) { Oj.dump(hash, mode: :strict) }
rescue LoadError
+ Kernel.warn '`Oj` is not installed, falling back to default JSON encoder.'
default_encoder
end
def try_active_support
require 'active_support/json'
->(hash) { ActiveSupport::JSON.encode(hash) }
rescue LoadError
+ Kernel.warn '`ActiveSupport` is not installed, falling back to default JSON encoder.'
default_encoder
end
def default_encoder
lambda do |hash|
require 'json'
JSON.dump(hash)
- end
- end
-
- def resource_class
- @resource_class ||= begin
- klass = Class.new
- klass.include(Alba::Resource)
end
end
end
@encoder = default_encoder