Skip to content

Rails plugin that gives extra power to the ActiveRecord#serialize method.

License

Notifications You must be signed in to change notification settings

arydjmal/super_serialize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

super_serialize plugin

Rails plugin that gives extra power to the ActiveRecord#serialize method.

It creates setters and getters for the fake columns, and cast values to the appropriate class.

Tested using shoulda.

NOTE: 1) There are a lot of similar plugins out there. I wrote my own because it is the only one that supports classes + defaults + tested. Some ideas (like support.rb) were taken from mongomapper. 2) For now the only accepted classes are Boolean, String, Integer, and Float. Why? …it’s what I need…

Usage

In your model:

# Given the following model
# User(id: integer, username: string, serialized_data: text, created_at: datetime)

super_serialize :male,       Boolean, :default => true
super_serialize :occupation, String
super_serialize :age,        Integer
super_serialize :weight,     Float

# Gives you setters and getters to do the following
user = User.new(:username => 'ary', :male => true, :age => '25')
user.occupation = 'professional fútbol player'
user.username == 'ary' # => true

# Integer
user.age = 'ary' # => 0
user.age = 10.8  # => 10
user.age = nil   # => nil

# Float
user.weight = 135       # => 135.0
user.weight = '61.2 kg' # => 61.2
user.weight = nil       # => nil

# String
user.occupation = true # => 'true'
user.occupation = 10.2 # => '10.2'
user.occupation = nil  # => nil

# Boolean
user.male = 'true' # => true
user.male = 't'    # => true
user.male = 1      # => true
# Also gives you a #{name}? method
user.male? # => true

Dependencies

None.

Install

./script/plugin install git://github.com/arydjmal/super_serialize.git

Copyright © 2010 Ary Djmal, released under the MIT license

About

Rails plugin that gives extra power to the ActiveRecord#serialize method.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages