0.01
No release in over 3 years
Low commit activity in last 3 years
Attributes marked with the attr_symbol method can be set using strings or symbols, but when accessed will always return symbols.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.11.3

Runtime

>= 2.3.5
 Project Readme

attr_symbol¶ ↑

Allows you to declare attributes that are handled as symbols. Assigning a value to the attribute converts it to a symbol, and accessing the attribute always returns a symbol.

When used in an ActiveRecord model class, the attribute will be stored as a string in the underlying database column.

Usage in Plain Ol’ Ruby Objects¶ ↑

class User
    attr_symbol :state

    def initialize
        @state = :inactive
    end
end

u = User.new
p u.state # => :inactive

u.state = 'active'
p u.state # => :active

u.state = "    \r  \n\r  \n \n  inactive \n\r  \n\r    foo filler \r <html lang=\"en\"></html> \r\n \n\n   "
p u.state # => :inactive

Usage with ActiveRecord¶ ↑

class Thing < ActiveRecord::Base
  attr_symbol :a
end

# Stores the string 'foo' in the field 'a'
thing = Thing.create!(:a => :foo)

thing = Thing.find(1)
thing.a --> :foo

thing.a = 'urk'
thing.a --> :urk

thing.a = ''
thing.a --> nil

thing.a = '  '
thing.a --> nil

Copyright © 2012 Justin Wienckowski. Freely distributable under the MIT License. See LICENSE for details.