Module: BufferAffects
- Defined in:
- lib/bufferaffects/bufferaffects.rb
Overview
This module is intended to be used as extension (class level mixin) for classes using some buffers that may be altered by calling certain methods.
It automates resetting of buffers by installing wrappers for invasive methods you choose. It rewrites selected methods by adding to them code that calls buffer(s) flushing method created by you.
Markers
To select which methods are invasive for your buffer(s) you should use markers which in usage are similar to accessors, e.g:
attr_affects_buffers :domain
Markers may be placed anywhere in the class. Wrapping routine will wait for methods to be defined if you mark them too early in your code.
Marking methods
To mark methods which should trigger reset operation when called use method_affects_buffers which takes comma-separated list of symbols describing names of these methods.
Marking attributes (setters)
The marker attr_affects_buffers is similar but it takes instance members not methods as arguments. It just installs hooks for corresponding setters.
Buffers flushing method
Default instance method called to reset buffers should be defined under name reset_buffers
You may also want to set up your own name by calling buffers_reset_method class method.
Buffers flushing method may take none or exactly one argument. If your method will take an argument then a name of calling method will be passed to it as symbol.
The name of your buffers flushing method is passed to subclasses but each subclass may redefine it.
Be aware that if you have a class that is subclass of a class using BufferAffects then by setting new buffers_reset_method you may experience two methods being called. That may happen when:
-
your base class has different resetting method assigned than your derivative class
-
you mark some method or attribute again using attr_affects_buffers or method_affects_buffers
-
you will not redefine that method in subclass (despite two facts above)
That’s because we assume that resetting method assigned to a method in superclass may be needed there and it shouldn’t be taken away.
Inherited classes
This module tries to be inheritance-safe but you will have to mark methods and members in subclasses if you are going to redefine them. That will install triggers again which is needed since redefining creates new code for method which overrides the code altered by BufferAffects.
The smooth way is of course to use super
in overloaded methods so it will also do the job.
To be sure that everything will work fine try to place buffers_reset_method clause before any other markers.
Caution
This code uses method_added hook. If you’re going to redefine that special method in your class while still using this module then remember to call original version or explicitly invoke ba_check_method in your version of method_added:
ba_check_method(name)
Example
class Main
include BufferAffects
buffers_reset_method :reset_path_buffer
attr_affects_buffers :subpart
attr_accessor :subpart, :otherpart
def reset_path_buffer(name)
@path = nil
p "reset called for #{name}"
end
def path
@path ||= @subpart.to_s + @otherpart.to_s
end
end
obj = Main.new
obj.subpart = 'test'
p obj.path
obj.subpart = '1234'
p obj.path
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Class Method Details
.included(base) ⇒ Object
:nodoc:
120 121 122 |
# File 'lib/bufferaffects/bufferaffects.rb', line 120 def self.included(base) #:nodoc: base.extend(ClassMethods) end |