Skip to content

Commit

Permalink
Add FullNameFormatter
Browse files Browse the repository at this point in the history
This adds a class which formats a full name (made up of a given name and
a family name) in given format context, either internal use or
communication with parents.

The internal use format will use "FAMILY, Given" as the format and for
parents it will remain as "Given Family".
  • Loading branch information
thomasleese committed Mar 4, 2025
1 parent 8aaaee1 commit 8712b8c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/lib/full_name_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

class FullNameFormatter
def self.call(nameable, context:, parts_prefix: nil)
parts_prefix = "#{parts_prefix}_" if parts_prefix.present?

given_name =
nameable.try(:"#{parts_prefix}given_name").presence ||
nameable.send(:given_name)
family_name =
nameable.try(:"#{parts_prefix}family_name").presence ||
nameable.send(:family_name)

case context
when :internal
"#{family_name.upcase}, #{given_name.upcase_first}"
when :parents
"#{given_name.upcase_first} #{family_name.upcase_first}"
else
raise "Unknown context: #{context}"
end
end
end
53 changes: 53 additions & 0 deletions spec/lib/full_name_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

describe FullNameFormatter do
context "with no parts prefix" do
subject { described_class.call(nameable, context:) }

let(:nameable) { OpenStruct.new(given_name: "John", family_name: "Smith") }

context "with internal context" do
let(:context) { :internal }

it { should eq("SMITH, John") }
end

context "with parents context" do
let(:context) { :parents }

it { should eq("John Smith") }
end
end

context "with a parts prefix" do
subject do
described_class.call(nameable, context: :parents, parts_prefix: :parent)
end

let(:nameable) do
OpenStruct.new(parent_given_name: "John", parent_family_name: "Smith")
end

it { should eq("John Smith") }
end

context "with a multi-case name" do
subject { described_class.call(nameable, context:) }

let(:nameable) do
OpenStruct.new(given_name: "John", family_name: "MacGyver")
end

context "with internal context" do
let(:context) { :internal }

it { should eq("MACGYVER, John") }
end

context "with parents context" do
let(:context) { :parents }

it { should eq("John MacGyver") }
end
end
end

0 comments on commit 8712b8c

Please sign in to comment.