Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active August 14, 2018 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/70a5761e4f2c078d090d4aa8a6aa04a4 to your computer and use it in GitHub Desktop.
Save krisleech/70a5761e4f2c078d090d4aa8a6aa04a4 to your computer and use it in GitHub Desktop.
ActiveRecord value object
class OrganisationCode
attr_reader :prefix, :suffix
def initialize(code)
@code = code
@prefix, @suffix = @code.split('@')
end
def to_s(format = :short)
case format
when :short
@prefix
when :long
@code
else
raise ArgumentError, '...'
end
end
end
class Site < AR::Base
def organisation_code
@organisation_code ||= OrganisationCode.new(super)
end
def organisation_code=(new_code)
@organisation_code = nil
super(new_code)
end
end
Site.create(organisation_code: 'abc@123') # => TypeError: can't quote OrganisationCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment