Module: LLIP::Visitable

Defined in:
lib/llip/visitable.rb

Overview

It makes a class visitable like it’s defined in the Visitor pattern, using the double dispatch tecnique.

It adds the accept method so for every instance of a class, ie TempClass, including it, it’s possible to call instance.accept(visitor) and the visitor will receive a :visit_temp_class message.

It passes this ability to its subclasses, so if the subclass is TempClassChild the visitor method which will be called is :visit_temp_class_child.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.add_accept(klass) ⇒ Object

It adds the accept method following the visitor pattern and the double dispatch tecnique.



20
21
22
23
24
25
26
27
# File 'lib/llip/visitable.rb', line 20

def self.add_accept(klass)
  name = klass.name.gsub(/[A-Z]+/) { |s| " " + s.downcase}.strip.gsub(" ","_")
  klass.class_eval "      def accept(visitor)\nvisitor.visit_\#{name}(self)\n      end\n    CODE\nend\n"

.included(other) ⇒ Object



14
15
16
17
# File 'lib/llip/visitable.rb', line 14

def self.included(other)
  add_accept(other)
  other.extend(ClassMethods)
end