# Creating a plugin based app

**URL:** <https://www.ruby-forum.com/t/creating-a-plugin-based-app/138654>\
**Category:** Ruby\
**Created:** [May 26, 2008, 11:56am UTC](https://www.ruby-forum.com/t/creating-a-plugin-based-app/138654 "2008-05-26T11:56:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimmoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19adc/32.png) [@kimmoke](https://www.ruby-forum.com/u/kimmoke)\
**Post date:** [May 26, 2008, 11:56am UTC](https://www.ruby-forum.com/t/creating-a-plugin-based-app/138654/1 "2008-05-26T11:56:48Z")

</div>

Any ideas how to do something like this…?

I’d like to drop new parsers into a plugin-directory of my app without  
making modifications to the main app, so there would be something like:

# main.rb:

require ‘plugins/\*’  
parsed\_data = Parser.parse\_data(data\_goes\_here)

# plugins/foo.rb:

class FooParser \< Parser  
handles /^foo/  
def parse\_data(data)  
data.do\_something if data.match(@handles)  
end  
end

# plugins/bar.rb:

class BarParser \< Parser  
handles /^bar/  
def parse\_data(data)  
data.do\_something if data.match(@handles)  
end  
end

Then if I wanted to add a new parser i would just throw it in there with  
the rest. Perhaps some sort of “register\_handler”-call in each plugin?

---

<div class="post-metadata">

**Author:** ![kimmoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19adc/32.png) [@kimmoke](https://www.ruby-forum.com/u/kimmoke)\
**Post date:** [May 26, 2008, 12:17pm UTC](https://www.ruby-forum.com/t/creating-a-plugin-based-app/138654/2 "2008-05-26T12:17:04Z")

</div>

On Mon, May 26, 2008 at 11:56 AM, Kimmo L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> class FooParser \< Parser  
> data.do\_something if data.match(@handles)  
> end  
> end
> 
> Then if I wanted to add a new parser i would just throw it in there with  
> the rest. Perhaps some sort of “register\_handler”-call in each plugin?

I think you already have it. The “handles” method could be implemented  
to register every handler, and then instantiate a child parser based on  
which handler the data matches. You would need to move the “if  
data.match…”  
part to the parent Parser class. Something like:

class Parser  
@handlers = {}  
class \<\< self  
attr\_accessor :handlers

```
def handles reg
  Parser.handlers[reg] = self
end

def parse_data data
  handler = @handlers.keys.find {|x| data.match(x)}
  raise "No parser found for #{data}" unless handler
  parser = @handlers[handler]
  parser.new.parse_data data
end

```

end  
end

class FooParser \< Parser  
handles /^foo/

def parse\_data data  
puts “FooParser parsing #{data}”  
end  
end

class BarParser \< Parser  
handles /^bar/  
def parse\_data data  
puts “BarParser parsing #{data}”  
end  
end

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![kimmoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19adc/32.png) [@kimmoke](https://www.ruby-forum.com/u/kimmoke)\
**Post date:** [May 26, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/creating-a-plugin-based-app/138654/3 "2008-05-26T12:35:40Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> Hope this helps,

Perfect, thanks.
