All Projects → jqr → typed_serialize

jqr / typed_serialize

Licence: MIT license
Typed serialize makes sure your serialized attribute is always the specified type.

Programming Languages

ruby
36898 projects - #4 most used programming language

Typed Serialize

Typed serialize makes sure your serialized attribute is always the specified type. This is especially nice for serialized hashes and new models.

Example

class User < ActiveRecord::Base
  typed_serialize :settings, Hash
end

User.new.settings        # => {}

u = User.new
u.settings[:theme] = 1
u.settings               # => { :theme => 1 }

Define serialized attributes

Avalaible only for Hash columns!!!

class User < ActiveRecord::Base
  typed_serialize :settings, Hash

  serialized_accessor :settings, :photo, :city, :nickname

end

u = User.new
u.photo = '/home/photos/me.png'
u.photo
=> '/home/photos/me.png'
u.settings
=>{:photo=>'/home/photos/me.png'}

or even better

class User < ActiveRecord::Base
  typed_serialize :settings, Hash, :photo, :city, :nickname

end

u = User.new(:city=>'NY')
u.city
=> 'NY'

Also avalaible serialized_reader and serialized_writer.

Install

As a Rails plugin:

./script/plugin install git://github.com/jqr/typed_serialize.git

Prefer gems? Add this to your environment.rb and run the following command.

config.gem 'typed_serialize'

$ rake gems:install

Docs

rdoc.info/projects/jqr/typed_serialize

Contributors

Homepage

github.com/jqr/typed_serialize

License

Copyright © 2008 Elijah Miller <[email protected]>, released under the MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].