No commit activity in last 3 years
No release in over 3 years
attr_accessor with typecast feature.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0
>= 0

Runtime

 Project Readme

attr_typecastable

Build Status Coverage Status Code Climate

attr_accessor with typecast feature.

Requirements

  • Ruby 2.0 or later

Installation

Add this line to your application's Gemfile:

gem 'attr_typecastable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attr_typecastable

Default Typecasters

  • Object (no typecasting)
  • String
  • Symbol
  • Integer
  • Float
  • Rational
  • BigDecimal
  • Boolean
  • Time
  • Date
  • DateTime
  • ArrayFactory (Array Typecaster builder)

Usage

require 'attr_typecastable'

# Custome typecaster definition
class CastToMoney < AttrTypecastable::Types::Base
  private

  # must define #do_typecast method
  def do_typecast(value)
    # can use @options
    # @options is hash given to `typed_attr_accessor`
    return value if value.is_a?(Money)

    if value.is_a?(Integer)
      Money.new(value)
    else
      raise AttrTypecastable::Types::CastError, "Cannot convert to Money"
    end
  end
end

class User
  # define typed_attr_accessor and initialize hook
  include AttrTypecastable

  typed_attr_accessor :name, String

  # String typecaster uses #to_s
  typed_attr_accessor :default_name, String, default: "Foo"
  typed_attr_accessor :not_nil_name, String, allow_nil: false, default: ""

  # Date typecaster uses #to_date
  typed_attr_accessor :birthday, Date

  # Time typecaster uses #to_time
  typed_attr_accessor :birthday_time, Time

  # DateTime typecaster uses #to_time and #to_datetime
  typed_attr_accessor :birthday_datetime, DateTime

  # Integer typecaster uses #to_i
  typed_attr_accessor :age, Integer

  # Float typecaster uses #to_f
  typed_attr_accessor :bmi, Float

  # Custom Typecaster
  typed_attr_accessor :property, CastToMoney

  # Boolean typecaster uses `#===` method in order to check whether value is true or false.
  typed_attr_accessor :adult, Boolean # default true_values: ["true", 1], false_values: ["false", 0]
  typed_attr_accessor :admin, Boolean, true_values: ["yes"], false_values: ["no"]
  typed_attr_accessor :active, Boolean, true_values: [/true/i], false_value: [/false/i] # can use Regexp

  def initialize(name: nil, default_name: nil)
    self.name = name
    self.default_name = default_name
  end
end

user = User.new
user.name          # => nil
user.default_name  # => "Foo"

user.name = 1
user.name          # => "1"

user.birthday = "1990-10-1"
user.birthday      # => Mon, 01 Oct 1990

user.adult = "true"
user.adult         # => true

user.adult = "false"
user.adult         # => false

user.adult = "other"
user.adult         # => true

user.admin = "yes"
user.admin         # => true

user.active = "TrUe"
user.active        # => true
user.active = "FALSE"
user.active        # => false

user2 = User.new(default_name: "joker")
user2.default_name # => "joker"

Contributing

  1. Fork it ( https://github.com/joker1007/attr_typecastable/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request