This plugin makes adding attribute-specific methods to ActiveRecord instances a breeze. It works by simply creating a ‘blank slate’ proxy class in place of the original attribute getter method, and allows you to create extension methods, like you can with associations and named scopes. Unless overwritten in the extension declaration block, methods will be delegated up to the attribute value instance - be it a string, integer, etc.
Given that your ‘people’ table has a string column ‘name’.
class Person < ActiveRecord::Base with_attribute :name do def has_middle_name? self.split(' ').size == 3 end def surname self.split(' ').last end end end
Which then gives you the ability to perform the following:
@person = Person.create :name => 'John C. Doe' @person.name.has_middle_name? # => true @person.name.surname # => "Doe" # and of course, @person.name # => "John C. Doe"
./script/plugin install git://github.com/brennandunn/attribute_assembler.git
Possibly get defined attribute extensions to work with dirty attribute accessors?
Copyright © 2008 Brennan Dunn, released under the MIT license