0.0
No commit activity in last 3 years
No release in over 3 years
Type casting
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.8
~> 5.0, < 5.11
~> 0.2
~> 12.0
~> 0.12

Runtime

 Project Readme

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.