SixArm.com » Ruby » PersonName provides methods for names, initials, etc.
Switch branches/tags
Nothing to show
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.codeclimate/badges
doc
lib
test
.codeclimate.yml
.document
.gemtest
.gitattributes
.gitignore
.rspec
.travis.yml
CHANGES.md
CODEOWNERS
CONTRIBUTING.md
Gemfile
LICENSE.md
README.md
Rakefile
VERSION
sixarm_ruby_person_name-2.0.0-sha512.txt
sixarm_ruby_person_name-2.0.0.gem
sixarm_ruby_person_name.gemspec

README.md

SixArm.com → Ruby →
PersonName mixin methods for a person model

Gem Version Build Status Code Climate

Introduction

Our user models typically have accessors like these:

user.given_name => "Martin" user.middle_name => "Luther" user.family_name => "King"

This gem is a utility to concatenate the user's name various common ways:

user.full_name => "Martin Luther King" user.list_name => "King, Martin Luther" user.initials => "MLK"

It's fine if the model doesn't have a middle_name field, or if any of the values of any of the fields are nil or blank; this gem will do the right thing.

For docs go to http://sixarm.com/sixarm_ruby_person_name/doc

Want to help? We're happy to get pull requests.

Install

Gem

To install this gem in your shell or terminal:

gem install sixarm_ruby_person_name

Gemfile

To add this gem to your Gemfile:

gem 'sixarm_ruby_person_name'

Require

To require the gem in your code:

require 'sixarm_ruby_person_name'

Example

Create a typical user class, include this mixin, the use it:

class User < ActiveRecord::Base
  include PersonName
end

user = User.new(
  given_name => 'Martin',
  middle_name => 'Luther',
  family_name => 'King'
)

user.full_name => "Martin Luther King"
user.list_name => "King, Martin Luther"
user.initials => "MLK"
user.given_name_middle_name => "Martin Luther"
user.given_name_middle_initial => "Martin L"
user.given_name_middle_initial_family_name => "Martin L King"

Naming conventions

You can use either of these naming conventions:

  • given name, middle name, family name
  • first name, middle name, last name

Speed Tip

To make these very fast in Rails, you can use memoize:

class User < ActiveRecord::Base
  extend ActiveSupport::Memoizable
  include PersonName
  memoize :full_name,
          :list_name,
          :initials,
          :given_name_middle_name,
          :given_name_middle_initial,
          :given_name_middle_initial_family_name,
          :given_name_family_name
end