lib/koine/attributes/adapter/base.rb in koine-attributes-0.2.3 vs lib/koine/attributes/adapter/base.rb in koine-attributes-0.3.0
- old
+ new
@@ -1,25 +1,46 @@
module Koine
module Attributes
module Adapter
class Base
- def coerce(*_values)
- raise NotImplementedError
+ def coerce(value)
+ return coerce_nil if value.nil?
+
+ coerce_not_nil(value)
end
def default_value
@default_value.respond_to?(:call) &&
@default_value.call ||
@default_value
end
+ def with_nil_value(value = nil, &block)
+ @nil_value_set = true
+ @nil_value = value
+ @nil_value = block if block
+ self
+ end
+
def with_default_value(value = nil, &block)
@default_value = value
@default_value = block if block
self
end
- protected
+ private
+
+ def coerce_nil
+ if @nil_value_set
+ return @nil_value.respond_to?(:call) ? @nil_value.call : @nil_value
+ end
+
+ raise ArgumentError, 'Cannot be nil'
+ end
+
+ def coerce_not_nil(_value)
+ raise NotImplementedError
+ end
# duplicates if possible and freezes object
def secure
value = yield