55502f40dc8b7c769880b10874abc9d0

I am pretty lost on this, this is about as far as I got before my brain busted.

Essentially I have a class that reads a file and outputs. I want a kind of hook based system that loads a plugin with class definitions that will be able to modify the output.

I know this is terrible and not how you do this at all. Someone show me the light!

class Plugin
end

class PluginManager
	Dir.glob("plugins/*.rb").each {|plugin| require plugin}
end

class BasicReader
	File.open("testfile.txt", "r") do |file|
		while line = file.gets
			#if defined? PluginManager.file_output_hook 
				line = PluginManager.file_output_hook(line)
			#end
			puts "#{line}"
		end
	end
end

#plugins/upcase.rb
public
	def file_output_hook(line)
		line.upcase
	end

Refactorings

No refactoring yet !

Ee0505bbd355292778077fb662c88f13

Fu86

June 25, 2010, June 25, 2010 10:54, permalink

3 ratings. Login to rate!
class PluginManager
  def initialize
    @plugins = Dir.glob("plugins/*.rb").map do |file|
      require file
      Module.const_get(file[8..-4].capitalize).new
    end
  end

  def call method, *params
    mod_params = *params
    @plugins.each do |plugin_class|
      mod_params = plugin_class.send(method, *mod_params) if plugin_class.respond_to? method
    end
    mod_params
  end
end
class Upcase
  def file_output_hook line
    line.upcase
  end
end
this is a
multiline test.
#!/usr/bin/env ruby
require "plugin_framework.rb"

class BasicReader
  def initialize
    @pm = PluginManager.new
  end

  def read file
    File.open(file, "r").each_line do |line|
      puts @pm.call(:file_output_hook, line)
    end
  end
end

BasicReader.new.read("test.txt")
Faccb37537ef4619e07bf0da950d725b

clonecd219

May 9, 2011, May 09, 2011 00:30, permalink

No rating. Login to rate!

Не that has no children knows not what love is.

Your refactoring





Format Copy from initial code

or Cancel