Created
December 13, 2012 17:05
-
-
Save jacortinas/4277991 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Name | |
def initialize name='' | |
@name = name | |
end | |
def name | |
@name ||= @name.to_s | |
end | |
alias :full :name | |
def first | |
split_name.first | |
end | |
def last | |
split_name.last | |
end | |
def == other | |
self.to_s == other.to_s | |
end | |
def to_s | |
name | |
end | |
def inspect | |
"#<Name \"#{name}\">" | |
end | |
private | |
def split_name | |
@split_name ||= name.split ' ', 2 | |
end | |
end |
This file contains hidden or 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 < ActiveRecord::Base | |
def name | |
@name ||= Name.new super | |
end | |
def name= new_name | |
@name = Name.new new_name | |
super @name | |
end | |
def first_name | |
name.first | |
end | |
def last_name | |
name.last | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
User has a name column that is a string.