lib/arstotzka/wrapper.rb in arstotzka-1.0.4 vs lib/arstotzka/wrapper.rb in arstotzka-1.1.0
- old
+ new
@@ -4,19 +4,26 @@
# @api private
#
# Class responsible for wrapping / parsing a value fetched
class Wrapper
include Arstotzka::TypeCast
+ include Base
# Returns a new instance of Wrapper
#
- # @param clazz [Class] class to wrap the value
- # @param type [String/Symbol] type to cast the value. The
- # possible type_cast is defined by {TypeCast}
- def initialize(clazz: nil, type: nil)
- @clazz = clazz
- @type = type
+ # @overload initialize(options_hash={})
+ # @param options_hash [Hash] options of initialization
+ # @option options_hash klass [Class] class to wrap the value
+ # @option options_hash type [String/Symbol] type to cast the value. The
+ # possible type_cast is defined by {TypeCast}
+ #
+ # @overload initialize(options)
+ # @param options [Arstotzka::Options] options of initialization object
+ #
+ # @return [Arstotzka::Wrapper]
+ def initialize(options = {})
+ self.options = options
end
# wrap a value
#
# @return [Object]
@@ -28,11 +35,11 @@
# def initialize(name)
# @name = name
# end
# end
#
- # wrapper = Arstotzka::Wrapper.new(clazz: Person)
+ # wrapper = Arstotzka::Wrapper.new(klass: Person)
# wrapper.wrap('John') # retruns Person.new('John')
#
# @example Casting type
# wrapper = Arstotzka::Wrapper.new(type: :integer)
# wrapper.wrap(['10', '20', '30']) # retruns [10, 20, 30]
@@ -44,11 +51,11 @@
# def initialize(payload)
# @payload = payload
# end
# end
#
- # wrapper = Arstotzka::Wrapper.new(type: :string, clazz: Request)
+ # wrapper = Arstotzka::Wrapper.new(type: :string, klass: Request)
# request = wrapper.wrap(value)
#
# request.payload # returns '{"key"=>"value"}'
def wrap(value)
return wrap_array(value) if value.is_a?(Array)
@@ -56,21 +63,22 @@
end
private
# @private
- attr_reader :clazz, :type
+ attr_reader :options
+ delegate :klass, :type, to: :options
# @private
#
# Wraps an element with a class and perform typecasting
#
# @return [Object]
def wrap_element(value)
value = cast(value) if type? && !value.nil?
return if value.nil?
- clazz ? clazz.new(value) : value
+ klass ? klass.new(value) : value
end
# @private
#
# Wraps each element of the array