Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of a basic decorator that uses `method_missing` rather than Delegation
class AccountDecorator
attr_accessor :account
def initialize(account)
@account = account
end
def method_missing(method_name, *args, &blk)
if account.respond_to?(method_name)
account_method_result(method_name, *args, &blk)
else
super
end
end
def respond_to_missing?(method_name)
attributes.include?(method_name.to_s) || super
end
def attributes
account.class.columns.map(&:name).map(&:to_s)
end
private
def account_method_result(method_name, *args, &blk)
if attributes.include?(method_name)
result = account.public_send(method_name, *args, &blk)
result.blank? ? '(none given)' : result
else
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.