Sha256: 3a42f0e9df38b88e9667b1f366fd7c1787d7e7bcc7706ce641e8e14d87e70389

Contents?: true

Size: 442 Bytes

Versions: 27

Compression:

Stored size: 442 Bytes

Contents

module Johnson
  module Visitable
    # Based off the visitor pattern from RubyGarden
    def accept(visitor, &block)
      klass = self.class.ancestors.find { |ancestor|
        visitor.respond_to?("visit_#{ancestor.name.split(/::/)[-1]}")
      }

      if klass
        visitor.send(:"visit_#{klass.name.split(/::/)[-1]}", self, &block)
      else
        raise "No visitor for '#{self.class}' at line #{line}"
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 5 rubygems

Version Path
smparkes-johnson-1.1.2.6 lib/johnson/visitable.rb
smparkes-johnson-1.1.2.5 lib/johnson/visitable.rb
smparkes-johnson-1.1.2.4 lib/johnson/visitable.rb
smparkes-johnson-1.1.2.3 lib/johnson/visitable.rb
johnson-1.1.1 lib/johnson/visitable.rb
johnson-1.1.2 lib/johnson/visitable.rb
johnson-1.1.0 lib/johnson/visitable.rb