Created
February 5, 2016 02:06
-
-
Save fukajun/8d476965cf11a184bcec to your computer and use it in GitHub Desktop.
ValueObject
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 User < ActinveRecord::Base | |
# address = '1540022 tokyo aa street | |
Address = Struct.new(:zip, :street) | |
before_save :rebuild_address | |
def address_object | |
@address_object ||= Address.new(address[0..5], address[6..-1]) | |
end | |
def rebuild_address | |
address = @address_object.zip + ... | |
end | |
end | |
hoge.address_zip | |
hoge.address_streeet | |
↓ | |
hoge.address.zip | |
hoge.address.streeet | |
hoge.address_objct.zip = '12345678' | |
hoge.save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment