Created
December 27, 2012 01:59
-
-
Save amite/4384799 to your computer and use it in GitHub Desktop.
Breaking up fat models example from http://svs.io/post/38883550750/breaking-up-fat-models-with-delegation#disqus_thread
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
require 'forwardable' | |
class User | |
extend Forwardable | |
attr_accessor :view_delegate_class, :first_name, :last_name | |
def_delegators :view_delegate, :full_name | |
def initialize(first_name, last_name) | |
@first_name = first_name | |
@last_name = last_name | |
end | |
def view_delegate | |
(@view_delegate_class || UserViewDelegate).new(self) | |
end | |
end | |
class UserViewDelegate | |
def initialize(user) | |
@user = user | |
end | |
def full_name | |
"#{first_name} #{last_name}" | |
end | |
def method_missing(name, *args) | |
@user.send(name, *args) | |
end | |
end | |
abhay = User.new('abhay', 'tandon') | |
puts abhay.full_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment