Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 virtual_attributes.gemspec
README.markdown

VirtualAttributes

DESCRIPTION

Usually you can use attr_accessor to define virtual attribute. You could even use attr_accessor_with_default to set default value.

But in any case you are unable to typecast values.

Let's assume you receive data from form submit.

> params[:user].inspect
=> {'enabled' => 'true', 'priority' => '0.5'}

Having attributes defined as table columns values would be actual true and 0.5.

But with attr_accessor fields will still be strings.

This is what virtual_attribute handles.

Origins

Inspired with virtual_attribute gem by Philip Roberts I tried to use ActiveRecord to typecast.

Usage

class User < ActiveRecord::Base
  virtual_attribute :enabled,  :type => :boolean, :default => false
  virtual_attribute :priority, :type => :float,   :default => 1.0
end
Something went wrong with that request. Please try again.