Created
August 18, 2010 16:25
-
-
Save mauriciozaffari/535318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'shellwords' | |
class Widget | |
def self.parse_widgets(content) | |
regex = /\[widget (.+)\]/ | |
content =~ regex | |
params = Hash[*Shellwords.shellwords($1).map{ |s| s.split('=', 2) }.flatten] | |
widget = Kernel.const_get(params.delete("type")).render(params) | |
widget ? content.gsub(regex, widget) : content | |
end | |
end | |
# Example: | |
# | |
content = "[widget type=Feed src=http://example.com/rss]" | |
Widget.parse_widgets(content) | |
# This will result in a call like this: | |
Feed.render("src" => "http://example.com/rss") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment