(no commit message) origin/HEAD origin/master
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 16 Nov 2007 17:41:45 +0000 (18:41 +0100)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 16 Nov 2007 17:41:45 +0000 (18:41 +0100)
examples/test-1.rb [new file with mode: 0644]
examples/test-2.rb [new file with mode: 0644]
examples/test.rb [new file with mode: 0644]
lib/base.rb [new file with mode: 0644]
lib/plugin.rb [new file with mode: 0644]
lib/scr.rb [new file with mode: 0644]
lib/ylog.rb [new file with mode: 0644]

diff --git a/examples/test-1.rb b/examples/test-1.rb
new file mode 100644 (file)
index 0000000..141e072
--- /dev/null
@@ -0,0 +1,21 @@
+require 'scr'
+require 'yast'
+require 'pp'
+
+Plugin.define "yast_agent" do
+  author "Duncan"
+  version "1.0.0"
+  extends :agent
+  run_before :other
+  
+  def read(path)
+    m = YaST::Module.new("SCR")
+    return m.Read(path.to_s)
+  end
+      
+  def supports_path?(path)
+    return true
+  end
+end
+
+pp SCR::read(:'.proc.modules')
diff --git a/examples/test-2.rb b/examples/test-2.rb
new file mode 100644 (file)
index 0000000..14fae6a
--- /dev/null
@@ -0,0 +1,24 @@
+require 'scr'
+
+Plugin.define "release_agent" do
+  author "Duncan"
+  version "1.0.0"
+  extends :agent
+  run_before :other
+  
+  def read(path)
+    if path == :'.system.product.edition'
+      f = File.open('/etc/SuSE-release', 'r')
+      f.each_line do |l|
+        return l
+      end
+      return ""
+    end
+  end
+      
+  def supports_path?(path)
+    return path == :'.system.product.edition'
+  end
+end
+
+puts SCR::read(:'.system.product.edition')
diff --git a/examples/test.rb b/examples/test.rb
new file mode 100644 (file)
index 0000000..24c7af1
--- /dev/null
@@ -0,0 +1,12 @@
+require 'rubygems'
+require 'network-facade'
+
+class Digg < NetworkFacade::REST::Client 'http://services.digg.com'
+  mapping :apple_stories, '/stories/topic/apple'
+  mapping :me, '/user/pyros'
+end
+
+d = Digg.new
+puts d.apple_stories(:appkey => 'http://network-facade.rubyforge.org',
+                  :count => 3, :type => :json)
+puts d.me(:appkey => 'http://network-facade.rubyforge.org')
\ No newline at end of file
diff --git a/lib/base.rb b/lib/base.rb
new file mode 100644 (file)
index 0000000..f42d81f
--- /dev/null
@@ -0,0 +1 @@
+require 'rubygems'
\ No newline at end of file
diff --git a/lib/plugin.rb b/lib/plugin.rb
new file mode 100644 (file)
index 0000000..0c00a92
--- /dev/null
@@ -0,0 +1,69 @@
+
+# Based on 
+# http://majakari.net/2006/11/23/adding-plugins-to-your-ruby-application
+# http://eigenclass.org/hiki.rb?ruby+plugins
+
+require 'tsort'
+
+class Hash
+  include TSort
+  alias tsort_each_node each_key
+  
+  # hash values contain dependencies
+  def tsort_each_child(node, &block)
+    fetch(node, []).each(&block)
+  end
+end
+
+module PluginSugar
+  # allow accessing attributes using def_field
+  def def_field(*names)
+    class_eval do
+      names.each do |name|
+        define_method(name) do |*args|
+          case args.size
+          when 0: instance_variable_get("@#{name}")
+          else    instance_variable_set("@#{name}", *args)
+          end
+        end
+      end
+    end
+  end
+end
+
+class Plugin
+  @registered_plugins = {}
+  class << self
+    attr_reader :registered_plugins
+    private :new
+  end
+
+  def self.extensions(ext_point)
+    plugins = Plugin.registered_plugins.select do |n,o|
+      o.extends == ext_point
+    end
+    
+    deps = Hash.new([])
+    plugins.each do |name,obj|
+      if obj.run_after
+        deps[name] = obj.run_after.to_s
+      elsif obj.run_before
+        if obj.run_before.respond_to?(:pop) 
+          obj.run_before.each {|d| deps[d.to_s] = name}
+        else
+          deps[obj.run_before.to_s] = name
+        end
+      end
+    end
+    deps.tsort.map {|name| Plugin.registered_plugins[name]}
+  end
+
+  def self.define(name, &block)
+    p = new
+    p.instance_eval(&block)
+    Plugin.registered_plugins[name] = p
+  end
+
+  extend PluginSugar
+  def_field :author, :version, :extends, :run_before, :run_after
+end
\ No newline at end of file
diff --git a/lib/scr.rb b/lib/scr.rb
new file mode 100644 (file)
index 0000000..f9f52f2
--- /dev/null
@@ -0,0 +1,27 @@
+
+require 'plugin'
+require 'base'
+
+module SCR
+
+  class Exception < RuntimeError
+  end
+
+  class NoAgent < SCR::Exception
+  end
+
+  # tries to find an agent for a path
+  def self.find_agent_for_path(path)
+    Plugin.extensions(:agent).each do |plugin|
+      return plugin if plugin.supports_path?(path)
+    end
+    raise NoAgent
+  end
+
+  def self.read(path)
+    agent = find_agent_for_path(path)
+    return agent.read(path)
+  end
+end
+
+
diff --git a/lib/ylog.rb b/lib/ylog.rb
new file mode 100644 (file)
index 0000000..009f519
--- /dev/null
@@ -0,0 +1,12 @@
+
+module YaST
+  module Logger
+    def logger
+      if @logger.nil?
+        @logger = Logger.new(STDOUT)
+        @logger.level = Logger::INFO
+      end
+      return @logger
+    end
+  end
+end
\ No newline at end of file