delegate - khanhld

8
Delegate in Ruby on Rails

Upload: framgia-vietnam

Post on 12-May-2015

124 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Delegate - KhanhLD

Delegatein Ruby on Rails

Page 2: Delegate - KhanhLD

● Delegate● Options● Demo

Page 3: Delegate - KhanhLD

Delegate

● Easily expose contained objects’ public methods as your own.

● Useful with ActiveRecord associations● delegate :method_name, to: :target

Page 4: Delegate - KhanhLD

Delegate

● Multiple delegates to the same target are allowed

● delegate :method1, :method2, to: :target● Methods can be delegated to instance

variables, class variables, or constants

Page 5: Delegate - KhanhLD

Delegate

● Delegate a method to the class by using :class

● delegate :hello, to: :class

Page 6: Delegate - KhanhLD

Options

● :allow_nil● If the target is nil and does not respond to

the delegated method a NoMethodError is raised

● delegate :name, to: :profile, allow_nil: true● Returns nil

Page 7: Delegate - KhanhLD

Options

● delegate :name, to: :profile, prefix: true● Delegate methods are prefixed with the

name of the object being delegated to.● delegate :name, to: :client, prefix: :customer● Supply a custom prefix

Page 8: Delegate - KhanhLD

Thank you