Skip to content

Instantly share code, notes, and snippets.

@nateklaiber
Created April 17, 2012 18:37
Show Gist options
  • Save nateklaiber/2408067 to your computer and use it in GitHub Desktop.
Save nateklaiber/2408067 to your computer and use it in GitHub Desktop.
Person Name Forms Evolution
class NameFormat
attr_reader :first_name, :last_name, :middle_name
def initialize(person)
@person = person
@first_name = person.first_name
@last_name = person.last_name
@middle_name = person.middle_name
@forms = []
end
def name_forms
@forms << "#{first_name[0..0]} #{last_name}".squeeze(' ') unless first_name.blank? or last_name.blank?
@forms << "#{first_name[0..0]} #{middle_name[0..0]} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
@forms << "#{first_name[0..0]} #{middle_name} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
@forms << "#{first_name} #{last_name}".squeeze(' ') unless first_name.blank? or last_name.blank?
@forms << "#{first_name} #{middle_name[0..0]} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
@forms << "#{first_name} #{middle_name} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
@forms
end
end
class Person < ActiveRecord::Base
# Larger model with many methods
#
# ....
#
# We want to retrieve an array of different name forms for the member.
# This is used as a helper method to pass to a search engine as an
# initial filter.
#
# For instance, if my name was "John James Doe", this would create:
#
# >> person = Person.find(1)
#
# >> person.full_name
# => "John James Doe"
#
# >> person.get_name_forms
# => ["J Doe", "J J Doe", "J James Doe", "John Doe", "John J Doe", "John James Doe"]
def get_name_forms
name_forms = []
name_forms << "#{first_name[0..0]} #{last_name}".squeeze(' ') unless first_name.blank? or last_name.blank?
name_forms << "#{first_name[0..0]} #{middle_name[0..0]} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
name_forms << "#{first_name[0..0]} #{middle_name} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
name_forms << "#{first_name} #{last_name}".squeeze(' ') unless first_name.blank? or last_name.blank?
name_forms << "#{first_name} #{middle_name[0..0]} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
name_forms << "#{first_name} #{middle_name} #{last_name}".squeeze(' ') unless first_name.blank? or middle_name.blank? or last_name.blank?
name_forms
end
end
require 'active_support/core_ext/object/blank.rb' # We use the #blank? helper method. No need to re-invent the wheel.
module Formatter
class PersonNameForm
attr_reader :first, :middle, :last
def initialize(first, middle, last)
@first = (first || '')
@middle = (middle || '')
@last = (last || '')
end
def self.for_full_name(first, middle, last)
klass = self.new(first, middle, last)
klass.forms
end
def first_initial
get_initial(self.first)
end
def middle_initial
get_initial(self.middle)
end
def last_initial
get_initial(self.last)
end
def forms
forms ||= []
forms << ("%s %s" % [self.first_initial, self.last]).strip.squeeze(' ')
forms << ("%s %s %s" % [self.first_initial, self.middle_initial, self.last]).strip.squeeze(' ')
forms << ("%s %s %s" % [self.first_initial, self.middle, self.last]).strip.squeeze(' ')
forms << ("%s %s" % [self.first, self.last]).strip.squeeze(' ')
forms << ("%s %s %s" % [self.first, self.middle_initial, self.last]).strip.squeeze(' ')
forms << ("%s %s %s" % [self.first, self.middle, self.last]).strip.squeeze(' ')
forms.reject(&:blank?).uniq
end
private
def get_initial(part)
part.to_s[0..0]
end
end
end
require File.join(File.dirname(__FILE__), 'location/to/ib/formatter/person_name_form')
describe Formatter::PersonNameForm do
subject { described_class.new('Lester', 'Bart', 'Tester') }
it "returns the initial of the first name" do
subject.first_initial.should == "L"
end
it "returns the initial of the middle name" do
subject.middle_initial.should == "B"
end
it "returns the initial of the last name" do
subject.last_initial.should == "T"
end
it "returns all possible name forms" do
subject.forms.should =~ ["L Tester", "L B Tester", "L Bart Tester", "Lester Tester", "Lester B Tester", "Lester Bart Tester"]
end
context "Class methods" do
it "returns the name forms for a provided full name" do
described_class.for_full_name("Lester", "A", "Tester").should =~ ["L A Tester", "L Tester", "Lester A Tester", "Lester Tester"]
end
end
context "With only first and last name" do
subject { described_class.new('Lester', nil, 'Tester') }
it "returns an empty string for the middle initial" do
subject.middle_initial.should == ''
end
it "returns all possible name forms" do
subject.forms.should =~ ["L Tester", "Lester Tester"]
end
end
context "Empty strings provided for all name parts" do
subject { described_class.new('','','') }
it "returns an empty string for #first_initial" do
subject.first_initial.should == ''
end
it "returns an empty string for #middle_initial" do
subject.middle_initial.should == ''
end
it "returns an empty string for #last_initial" do
subject.last_initial.should == ''
end
it "returns an empty array for all possible name forms" do
subject.forms.should == []
end
end
context "Nil values provided for all name parts" do
subject { described_class.new(nil, nil, nil) }
it "returns an empty string for #first_initial" do
subject.first_initial.should == ''
end
it "returns an empty string for #middle_initial" do
subject.middle_initial.should == ''
end
it "returns an empty string for #last_initial" do
subject.last_initial.should == ''
end
it "returns an empty array for all possible name forms" do
subject.forms.should == []
end
end
context "When initials are provided for all name parts" do
subject { described_class.new('L', 'B', 'T') }
it "returns all possible name forms" do
subject.forms.should =~ ['L B T', 'L T']
end
end
context "When initials have a period" do
subject { described_class.new('D.J.', 'M.', 'F.') }
it "returns all possible name forms" do
subject.forms.should =~ ["D F.", "D M F.", "D M. F.", "D.J. F.", "D.J. M F.", "D.J. M. F."]
end
end
end
require 'spec_helper' # This file loads our less than fast Rails environment
describe Person do
# This file contains many more test blocks for all of the methods in our
# larger Person model.
context "Person Name Forms for Search" do
subject { Factory.build(:person, :first_name => 'Lester', :middle_name => 'The', :last_name => 'Tester') }
it "returns an array of name form variations" do
subject.get_name_forms.should =~ ["L Tester", "L T Tester", "L The Tester", "Lester Tester", "Lester T Tester", "Lester The Tester"]
end
end
end
module ActiveRecord; end
module ActiveRecord::Base; end
class Person < ActiveRecord::Base; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment