Module: Types

Defined in:
lib/types.rb

Constant Summary collapse

Rid =

include in attribute definitions

String.constrained( format:  /\A[#]{1}[0-9]{1,}:[0-9]{1,}\z/ )
Blockchain =

add other blockchain symbols here

String.constrained( format: /^(algo|eth|btc)$/ )
Email =
String.constrained( format: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i )
DateTime =

the ‘.constructor` to handle the string parsing and potential errors. (source: Gemini)

Types::Any.constructor do |value|
  begin
    case value
      when String
    ::DateTime.parse(value)
      when Integer
      ::Time.at(value).to_datetime
      else
      ::DateTime.now
    end
  rescue ArgumentError
    # Handle invalid date strings.  Options:
    # 1. Raise an exception (recommended for API responses)
    raise Dry::Types::CoercionError.new(
      value, DateTime, "Invalid DateTime format: #{value}"
    )
    # 2. Return nil (less strict, but might lead to unexpected behavior)
    # nil
    # 3. Use a default value (be careful with this, as it might mask errors)
    # DateTime.now
  end
end