Sha256: 0a2b130904ac38e9f1b3a3e2e8c0e3d7b6645b42214789212b6f3c9086b82319
Contents?: true
Size: 1.94 KB
Versions: 1
Compression:
Stored size: 1.94 KB
Contents
module Alba # Representing typed attributes to encapsulate logic about types class TypedAttribute # @param name [Symbol, String] # @param type [Symbol, Class] # @param converter [Proc] def initialize(name:, type:, converter:) @name = name @type = type @converter = case converter when true then default_converter when false, nil then null_converter else converter end end # @param object [Object] target to check and convert type with # @return [String, Integer, Boolean] type-checked or type-converted object def value(object) value, result = check(object) result ? value : @converter.call(value) rescue TypeError raise TypeError, "Attribute #{@name} is expected to be #{@type} but actually #{display_value_for(value)}." end private def check(object) value = object.public_send(@name) type_correct = case @type when :String, ->(klass) { klass == String } value.is_a?(String) when :Integer, ->(klass) { klass == Integer } value.is_a?(Integer) when :Boolean [true, false].include?(value) else raise Alba::UnsupportedType, "Unknown type: #{@type}" end [value, type_correct] end def default_converter case @type when :String, ->(klass) { klass == String } ->(object) { object.to_s } when :Integer, ->(klass) { klass == Integer } ->(object) { Integer(object) } when :Boolean ->(object) { !!object } else raise Alba::UnsupportedType, "Unknown type: #{@type}" end end def null_converter ->(_) { raise TypeError } end def display_value_for(value) value.nil? ? 'nil' : value.class.name end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
alba-1.3.0 | lib/alba/typed_attribute.rb |