Rasti::Types

Gem Version Build Status Coverage Status Code Climate

Type casting

Installation

Add this line to your application's Gemfile:

gem 'rasti-types'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rasti-types

Usage

T = Rasti::Types

Type casting

T::Integer.cast '10'   # => 10
T::Integer.cast '10.5' # => 10
T::Integer.cast 'text' # => Rasti::Types::CastError: Invalid cast: 'text' -> Rasti::Types::Integer

T::Boolean.cast 'true'  # => true
T::Boolean.cast 'FALSE' # => false
T::Boolean.cast 'text'  # => Rasti::Types::CastError: Invalid cast: 'text' -> Rasti::Types::Boolean

T::Time['%Y-%m-%d'].cast '2016-10-22' # => 2016-10-22 00:00:00 -0300
T::Time['%Y-%m-%d'].cast '2016-10'    # => Rasti::Types::CastError: Invalid cast: '2016-10' -> Rasti::Types::Time['%Y-%m-%d']

T::Array[T::Symbol].cast [1, 'test', :sym] # => [:"1", :test, :sym]

Built-in types

  • Array
  • Boolean
  • Enum
  • Float
  • Hash
  • Integer
  • IO
  • Model
  • Regexp
  • String
  • Symbol
  • Time
  • UUID

Plugable types

class UpcaseString
  class << self

    extend Castable

    private

    def valid?(value)
      valid.is_a?(String)
    end

    def transform(value)
      value.upcase
    end

  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/rasti-types.

License

The gem is available as open source under the terms of the MIT License.