lib/rice_bubble/attributes/base.rb in rice_bubble-0.1.2 vs lib/rice_bubble/attributes/base.rb in rice_bubble-0.2.0
- old
+ new
@@ -9,33 +9,28 @@
if @fetcher
@fetcher.call(object, name)
elsif object.respond_to?(name)
object.public_send(name)
else
- object[name]
+ object[name] || object[name.to_s]
end
end
- def valid?(_value)
- true
+ def valid?(value)
+ valid_types.any? { |t| value.is_a?(t) }
end
- def coerce(value)
- value
+ def valid_types
+ [::Object]
end
- def validate!(value, coerced:, path:)
- return if valid?(coerced)
-
- raise ValidationError,
- "#{path} expected #{description} but received #{value.inspect}"
- end
-
def call(value, path: '')
- coerced = coerce(value)
- validate!(value, coerced:, path:)
- coerced
+ if valid?(value)
+ value
+ else
+ validation_error(value:, path:)
+ end
end
def optional
Optional.new(self)
end
@@ -54,9 +49,13 @@
if class_or_instance.is_a?(Class) && class_or_instance < Serializer
class_or_instance.new
else
class_or_instance
end
+ end
+
+ def validation_error(value:, path: '')
+ raise ValidationError.new(value:, path:, attribute: self)
end
end
end
end