Skip to content

brennandunn/attribute_assembler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AttributeAssembler

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.

Example

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"

Install

./script/plugin install git://github.com/brennandunn/attribute_assembler.git

Todo

Possibly get defined attribute extensions to work with dirty attribute accessors?

Copyright © 2008 Brennan Dunn, released under the MIT license

About

Easily add attribute extension methods to AR, reminiscent of association/named_scope extensions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages