Module: Kinda::Core::Forwardable::ClassMethods
Instance Method Summary collapse
-
#delegate_attr_accessor(hash, kind = (:accessor), with_filter = false, &block) ⇒ Object
(also: #delegate_attr)
Create both reader and writer delegate Same usage than the simple “delegate” method except the following form in case the getter and setter symbols are different: delegate_attr :title => [:book, [:read_title, :write_title]].
- #delegate_attr_accessor_with_filter(hash, &block) ⇒ Object (also: #delegate_attr_with_filter)
- #delegate_attr_reader(hash, &block) ⇒ Object
- #delegate_attr_reader_with_filter(hash, &block) ⇒ Object
- #delegate_attr_writer(hash, &block) ⇒ Object
- #delegate_attr_writer_with_filter(hash, &block) ⇒ Object
-
#instance_delegate(hash) ⇒ Object
(also: #delegate)
Add a nice way to specify a different method for the accessor: delegate :remove => [:book, :delete] delegate [:add, :remove] => [:book, :<<, :delete].
Instance Method Details
#delegate_attr_accessor(hash, kind = (:accessor), with_filter = false, &block) ⇒ Object Also known as: delegate_attr
Create both reader and writer delegate Same usage than the simple “delegate” method except the following form in case the getter and setter symbols are different: delegate_attr :title => [:book, [:read_title, :write_title]]
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/core/forwardable.rb', line 29 def delegate_attr_accessor(hash, kind=(:accessor), with_filter=false, &block) hash.each do |delegate_methods, accessor| accessor_methods = [*accessor] accessor_object = accessor_methods.shift [*delegate_methods].each do |delegate_method| accessor_method = accessor_methods.shift || delegate_method accessor_getter, accessor_setter = case kind when :accessor getter, setter = [*accessor_method] setter ||= getter [getter, setter] when :reader [accessor_method, nil] when :writer [nil, accessor_method] end if accessor_getter __attr_reader__({ delegate_method => [accessor_object, accessor_getter] }, with_filter, &block) end if accessor_setter __attr_writer__({ delegate_method => [accessor_object, accessor_setter] }, with_filter, &block) end end end end |
#delegate_attr_accessor_with_filter(hash, &block) ⇒ Object Also known as: delegate_attr_with_filter
59 60 61 |
# File 'lib/core/forwardable.rb', line 59 def delegate_attr_accessor_with_filter(hash, &block) delegate_attr_accessor(hash, :accessor, true, &block) end |
#delegate_attr_reader(hash, &block) ⇒ Object
65 66 67 |
# File 'lib/core/forwardable.rb', line 65 def delegate_attr_reader(hash, &block) delegate_attr_accessor(hash, :reader, false, &block) end |
#delegate_attr_reader_with_filter(hash, &block) ⇒ Object
69 70 71 |
# File 'lib/core/forwardable.rb', line 69 def delegate_attr_reader_with_filter(hash, &block) delegate_attr_accessor(hash, :reader, true, &block) end |
#delegate_attr_writer(hash, &block) ⇒ Object
73 74 75 |
# File 'lib/core/forwardable.rb', line 73 def delegate_attr_writer(hash, &block) delegate_attr_accessor(hash, :writer, false, &block) end |
#delegate_attr_writer_with_filter(hash, &block) ⇒ Object
77 78 79 |
# File 'lib/core/forwardable.rb', line 77 def delegate_attr_writer_with_filter(hash, &block) delegate_attr_accessor(hash, :writer, true, &block) end |
#instance_delegate(hash) ⇒ Object Also known as: delegate
Add a nice way to specify a different method for the accessor: delegate :remove => [:book, :delete] delegate [:add, :remove] => [:book, :<<, :delete]
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/core/forwardable.rb', line 12 def instance_delegate(hash) hash.each do |delegate_methods, accessor| accessor_methods = [*accessor] accessor_object = accessor_methods.shift [*delegate_methods].each do |delegate_method| accessor_method = accessor_methods.shift || delegate_method def_instance_delegator(accessor_object, accessor_method, delegate_method) end end end |