summaryrefslogtreecommitdiffstats
path: root/lib/blink/interface.rb
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2005-04-13 15:24:36 +0000
committerLuke Kanies <luke@madstop.com>2005-04-13 15:24:36 +0000
commit6ee8b4e7a9731f969347e317456e8d9712fe2641 (patch)
tree0e2aa758bb523b4a2f1c752a625bd2d95462edf4 /lib/blink/interface.rb
parent5416017c05e44fc635ad14ffdf1ac1163a4cc6e5 (diff)
downloadpuppet-6ee8b4e7a9731f969347e317456e8d9712fe2641.tar.gz
puppet-6ee8b4e7a9731f969347e317456e8d9712fe2641.tar.xz
puppet-6ee8b4e7a9731f969347e317456e8d9712fe2641.zip
reorganizing
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@96 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/blink/interface.rb')
-rw-r--r--lib/blink/interface.rb108
1 files changed, 108 insertions, 0 deletions
diff --git a/lib/blink/interface.rb b/lib/blink/interface.rb
new file mode 100644
index 000000000..a1496f01b
--- /dev/null
+++ b/lib/blink/interface.rb
@@ -0,0 +1,108 @@
+#!/usr/local/bin/ruby -w
+
+# $Id$
+
+# our duck type interface
+
+# all of our first-class objects (objects, attributes, and components) will
+# respond to these methods
+# although attributes don't inherit from Blink::Interface
+# although maybe Blink::Attribute should...
+
+# the default behaviour that this class provides is to just call a given
+# method on each contained object, e.g., in calling 'sync', we just run:
+# object.each { |subobj| subobj.sync() }
+
+# to use this interface, just define an 'each' method and 'include Blink::Interface'
+module Blink
+ class Interface
+ # each subclass of Blink::Interface must create a class-local @objects
+ # variable
+ @objects = Hash.new # this one won't be used, since this class is abstract
+
+ # this is a bit of a hack, but it'll work for now
+ attr_accessor :performoperation
+
+ #---------------------------------------------------------------
+ def evaluate
+ self.retrieve
+ unless self.insync?
+ if @performoperation == :sync
+ self.sync
+ else
+ # we, uh, don't do anything
+ end
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # set up the "interface" methods
+ [:sync,:retrieve].each { |method|
+ self.send(:define_method,method) {
+ self.each { |subobj|
+ Blink.debug("sending '%s' to '%s'" % [method,subobj])
+ subobj.send(method)
+ }
+ }
+ }
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # if all contained objects are in sync, then we're in sync
+ def insync?
+ insync = true
+
+ self.each { |obj|
+ unless obj.insync?
+ Blink.debug("%s is not in sync" % obj)
+ insync = false
+ end
+ }
+
+ Blink.debug("%s sync status is %s" % [self,insync])
+ return insync
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # retrieve a named object
+ def Interface.[](name)
+ if @objects.has_key?(name)
+ return @objects[name]
+ else
+ raise "Object '#{name}' does not exist"
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # this is special, because it can be equivalent to running new
+ # this allows cool syntax like Blink::File["/etc/inetd.conf"] = ...
+ def Interface.[]=(name,object)
+ newobj = nil
+ if object.is_a?(Blink::Interface)
+ newobj = object
+ else
+ raise "must pass a Blink::Interface object"
+ end
+
+ if @objects.has_key?(newobj.name)
+ puts @objects
+ raise "'#{newobj.name}' already exists in " +
+ "class '#{newobj.class}': #{@objects[newobj.name]}"
+ else
+ Blink.debug("adding %s of type %s to class list" %
+ [object.name,object.class])
+ @objects[newobj.name] = newobj
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def Interface.has_key?(name)
+ return @objects.has_key?(name)
+ end
+ #---------------------------------------------------------------
+ end
+end