charge.statement.customer.nameThe above code could be used to display the customer's name in the partial. Unfortunately, it assumes
statementwill have a
customerwill have a
name. This is easily fixed by changing
chargeto only talk to it's friends.
charge.customer_nameThe simple fix to support this is to define
class ChargeThis change is simple enough; however, as the list of methods that require delegation grows your class can become littered with delegation code.
Luckily, Forwardable is included in the standard library. Forwardable allows you delegate method calls to an object, on a method by method basis. Using Forwardable the above code becomes:
class ChargeForwardable becomes even more valuable when you need to delegate several methods
def_delegators :statement, :customer_name
def_delegator :customer, :name, :customer_name
def_delegators :amount_info, :units, :fractions, :currencyFor more info on Law of Demeter and it's advantages check out the definition on Wikipedia. For more info on Forwardable check out the documentation on Ruby-Doc.