Extract Value Object

Value Objects are objects that represent a value (such as a dollar amount) rather than a unique, identifiable entity (such as a particular user).

Value Objects often implement information derived from a primitive object, such as the dollars and cents from a float, or the user name and domain from an email string.

Example

# app/controllers/invitations_controller.rb
def recipient_list
	@recipient_list ||= recipients.gsub(/\s+/, '').split(/[\n,;]+/)
end
 
def recipients 
	params[:invitation][:recipients]
end

⬇️

# app/models/recipient_list.rb
class RecipientList 
	include Enumerable
	
	def initialize(recipient_string) 
		@recipient_string = recipient_string
	end
	
	def each(&block) 
		recipients.each(&block)
	end
		
	def to_s 
		@recipient_string
	end
	
	private
	def recipients
		@recipient_string.to_s.gsub(/\s+/, '').split(/[\n,;]+/)
	end 
end
 
# app/controllers/invitations_controller.rb
def recipient_list
	@recipient_list ||= RecipientList.new(params[:invitation][:recipients])
end