-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
8aaaee1
commit 8712b8c
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |