/webget_ruby_person_name

WebGet.com » Ruby » mixin methods for a person's full name, sort name, etc.

Primary LanguageRubyOtherNOASSERTION

WebGet Ruby Gem: Flexible ways to deal with real human names

Author

Joel Parker Henderson, joelparkerhenderson@gmail.com

Copyright

Copyright © 2006-2010 Joel Parker Henderson

License

CreativeCommons License, Non-commercial Share Alike

License

LGPL, GNU Lesser General Public License

Access a human name from the fields of an ActiveRecord-based model in several combinations:

  • first_name_middle_name

  • first_name_middle_initial

  • fullname: first_name_middle_name_last_name

  • first_name_middle_initial_last_name

  • first_name_last_name

  • list_name: last_name, first_name

Note that the model doesn’t need attributes called first_name, middle_name, and last_name to call the gem’s methods; the methods are protected by testing with respond_to?(name_field).

However all of these fields which are present must be strings.

Example

class User < ActiveRecord::Base
  include PersonName
end

u=User.first
=> {first_name => 'Zora', middle_name => 'Neale', last_name => 'Hurston'}

u.full_name => "Zora Neale Hurston"
u.list_name => "Hurston, Zora Neale"
u.first_name_middle_name => "Zora Neale"
u.first_name_middle_initial_last_name => "Zora N Hurston"

Performance Tip

Use memoize to make these very fast in Rails:

class User < ActiveRecord::Base
  include PersonName
  memoize :full_name
  memoize :list_name
end