Sha256: 8e67fb957427164e176e2ad19277aac449c4e099379422c49347365027aff9b8
Contents?: true
Size: 1.29 KB
Versions: 2
Compression:
Stored size: 1.29 KB
Contents
module RiceBubble class Attributes class Base def initialize(&block) @fetcher = block end def fetch(object, name) if @fetcher @fetcher.call(object, name) elsif object.respond_to?(name) object.public_send(name) else object[name] || object[name.to_s] end end def valid?(value) valid_types.any? { |t| value.is_a?(t) } end def valid_types [::Object] end def call(value, path: '') if valid?(value) value else validation_error(value:, path:) end end def optional Optional.new(self) end def description soft_name = self.class.name.split('::').last .gsub(/([a-z])([A-Z])/, '\1 \2') .downcase article = soft_name.start_with?(/[aeiou]/) ? 'an' : 'a' "#{article} #{soft_name}" end private def instantiate(class_or_instance) 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
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rice_bubble-0.2.1 | lib/rice_bubble/attributes/base.rb |
rice_bubble-0.2.0 | lib/rice_bubble/attributes/base.rb |