Created
August 7, 2021 08:09
-
-
Save adriangohjw/2fb900e738fc1c4ff2adf1d64fa866a6 to your computer and use it in GitHub Desktop.
value objects to avoid primitive obsession
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
class Title | |
def initialize(title) | |
@title = title | |
end | |
def to_seniority | |
@title.include?('junior') ? 'junior' : nil | |
end | |
end | |
title = 'some job title' | |
seniority = Title.new(title).to_seniority |
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
title = 'some job title' | |
seniority = title.include?('junior') ? 'junior' : nil |
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
class Title | |
def initialize(title) | |
@title = title | |
end | |
def to_seniority | |
return 'junior' if @title.include?('junior') | |
return 'senior' if @title.include?('senior') | |
return 'manager' if @title.include?('manager') | |
nil | |
end | |
end | |
# code remains unchanged! | |
seniority = Title.new(title).to_seniority |
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
class Title | |
def initialize(title) | |
@title = title | |
end | |
def to_seniority | |
@title.include?('junior') ? 'junior' : nil | |
end | |
# easy to add a new method to the class! | |
def to_specialisation | |
return 'software engineer' if @title.include?('software') | |
return 'data scientist' if @title.include?('data sci') | |
nil | |
end | |
end | |
specialisation = Title.new(title).to_specialisation |
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
def title_to_seniority(title) | |
title.include?('junior') ? 'junior' : nil | |
end | |
seniority = title_to_seniority(title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://adriangohjw.github.io/posts/2021/05/26/value-objects-to-avoid-primitive-obsession.html