Skip to content

Instantly share code, notes, and snippets.

@adriangohjw
Created August 7, 2021 08:09
Show Gist options
  • Save adriangohjw/2fb900e738fc1c4ff2adf1d64fa866a6 to your computer and use it in GitHub Desktop.
Save adriangohjw/2fb900e738fc1c4ff2adf1d64fa866a6 to your computer and use it in GitHub Desktop.
value objects to avoid primitive obsession
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
title = 'some job title'
seniority = title.include?('junior') ? 'junior' : nil
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
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
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