Sha256: df9c4ef13ee160373845ae9b5713de354bbc895633c06e4e9607e754f0348dd4

Contents?: true

Size: 750 Bytes

Versions: 6

Compression:

Stored size: 750 Bytes

Contents

# frozen_string_literal: true

# @api public
# @since 0.1.0
# @version 0.3.0
SmartCore::Types::Value.define_type(:Text) do |type|
  type.define_checker do |value|
    SmartCore::Types::Value::String.valid?(value) || SmartCore::Types::Value::Symbol.valid?(value)
  end

  type.define_caster do |value|
    next value if SmartCore::Types::Value::String.valid?(value)
    next value if SmartCore::Types::Value::Symbol.valid?(value)

    begin
      SmartCore::Types::Value::String.cast(value)
    rescue SmartCore::Types::TypeCastingError
      begin
        SmartCore::Types::Value::Symbol.cast(value)
      rescue SmartCore::Types::TypeCastingError
        raise(SmartCore::Types::TypeCastingError, 'Non-castable to text')
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smart_types-0.8.0 lib/smart_core/types/value/text.rb
smart_types-0.7.1 lib/smart_core/types/value/text.rb
smart_types-0.7.0 lib/smart_core/types/value/text.rb
smart_types-0.6.0 lib/smart_core/types/value/text.rb
smart_types-0.4.0 lib/smart_core/types/value/text.rb
smart_types-0.3.0 lib/smart_core/types/value/text.rb