Skip to content

Commit

Permalink
Update forwardable to 1.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sho-h committed Sep 3, 2013
1 parent 287cb48 commit f480c5e
Showing 1 changed file with 127 additions and 9 deletions.
136 changes: 127 additions & 9 deletions refm/api/src/forwardable.rd
Expand Up @@ -52,8 +52,8 @@ category DesignPattern

== Instance Methods

--- def_instance_delegators(accessor, *methods) -> nil
--- def_delegators(accessor, *methods) -> nil
--- def_instance_delegators(accessor, *methods) -> ()
--- def_delegators(accessor, *methods) -> ()

メソッドの委譲先をまとめて設定します。

Expand All @@ -66,9 +66,18 @@ category DesignPattern

def_delegatorsdef_instance_delegators の別名になります。

また、以下の 2 つの例は同じ意味です。

--- def_instance_delegator(accessor, method, ali = method) -> nil
--- def_delegator(accessor, method, ali = method) -> nil
def_delegators :@records, :size, :<<, :map

def_delegator :@records, :size
def_delegator :@records, :<<
def_delegator :@records, :map

@see [[m:Forwardable#def_delegator]]

--- def_instance_delegator(accessor, method, ali = method) -> ()
--- def_delegator(accessor, method, ali = method) -> ()

メソッドの委譲先を設定します。

Expand All @@ -85,6 +94,61 @@ def_delegators は def_instance_delegators の別名になります。

def_delegatordef_instance_delegator の別名になります。

:

class MyQueue
extend Forwardable
attr_reader :queue
def initialize
@queue = []
end

def_delegator :@queue, :push, :mypush
end

q = MyQueue.new
q.mypush 42
q.queue # => [42]
q.push 23 # => NoMethodError

@see [[m:Forwardable#def_delegators]]

#@since 1.9.1
--- instance_delegate(hash) -> ()
--- delegate(hash) -> ()

メソッドの委譲先を設定します。

@param hash 委譲先のメソッドがキー、委譲先のオブジェクトが値の
[[c:Hash]] を指定します。キーは [[c:Symbol]] か
[[c:Symbol]] の配列で指定します。

#@# ruby-core:05899 のパッチに付いてたテストコードより。

:

require 'forwardable'
class Zap
extend Forwardable
delegate :length => :@str
delegate [:first, :last] => :@arr
def initialize
@arr = %w/foo bar baz/
@str = "world"
end
end

zap = Zap.new
zap.length # => 5
zap.first # => "foo"
zap.last # => "baz"

== Constants

--- FORWARDABLE_VERSION -> "1.1.0"

[[lib:forwardable]] ライブラリのバージョンを返します。
#@end

= module SingleForwardable

Expand All @@ -101,11 +165,38 @@ def_delegator は def_instance_delegator の別名になります。
g.def_delegator("@out", :puts)
g.puts ...

== Instance Methods
また、[[c:SingleForwardable]] はクラスやモジュールに対して以下のようにする事もできます。

class Implementation
def self.service
puts "serviced!"
end
end

module Facade
extend SingleForwardable
def_delegator :Implementation, :service
end

Facade.service # => serviced!

--- def_singleton_delegators(accessor, *methods) -> nil
--- def_delegators(accessor, *methods) -> nil
もし [[c:Forwardable]] と [[c:SingleForwardable]] の両方を使いたい場合、
#@since 1.9.1
def_instance_delegatordef_single_delegator メソッドの方を呼び出して
ください。
#@else
def_instance_delegatordef_singleton_delegator メソッドの方を呼び出
してください。
#@end

== Instance Methods

#@since 1.9.1
--- def_single_delegators(accessor, *methods) -> ()
#@else
--- def_singleton_delegators(accessor, *methods) -> ()
#@end
--- def_delegators(accessor, *methods) -> ()

メソッドの委譲先をまとめて設定します。

Expand All @@ -118,9 +209,22 @@ def_delegator は def_instance_delegator の別名になります。

def_delegatorsdef_singleton_delegators の別名になります。

また、以下の 2 つの例は同じ意味です。

def_delegators :@records, :size, :<<, :map

def_delegator :@records, :size
def_delegator :@records, :<<
def_delegator :@records, :map

--- def_singleton_delegator(accessor, method, ali = method) -> nil
--- def_delegator(accessor, method, ali = method) -> nil
@see [[m:SingleForwardable#def_delegator]]

#@since 1.9.1
--- def_single_delegator(accessor, method, ali = method) -> ()
#@else
--- def_singleton_delegator(accessor, method, ali = method) -> ()
#@end
--- def_delegator(accessor, method, ali = method) -> ()

メソッドの委譲先を設定します。

Expand All @@ -137,3 +241,17 @@ def_delegators は def_singleton_delegators の別名になります。

def_delegatordef_singleton_delegator の別名になります。

@see [[m:SingleForwardable#def_delegators]]

#@since 1.9.1
--- single_delegate(hash) -> ()
--- delegate(hash) -> ()

メソッドの委譲先を設定します。

@param hash 委譲先のメソッドがキー、委譲先のオブジェクトが値の
[[c:Hash]] を指定します。キーは [[c:Symbol]] か
[[c:Symbol]] の配列で指定します。

@see [[m:Forwardable#delegate]]
#@end

1 comment on commit f480c5e

@kachick
Copy link
Contributor

@kachick kachick commented on f480c5e Sep 3, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Please sign in to comment.