From cf08be12cc1418b02feb0ae52a54de265e440ea6 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 21 Apr 2005 15:46:01 +0000 Subject: renaming statetree to elements git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@201 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/blink/elements.rb | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/blink/statetree.rb | 109 ------------------------------------------------- 2 files changed, 109 insertions(+), 109 deletions(-) create mode 100644 lib/blink/elements.rb delete mode 100644 lib/blink/statetree.rb diff --git a/lib/blink/elements.rb b/lib/blink/elements.rb new file mode 100644 index 000000000..64fac9b24 --- /dev/null +++ b/lib/blink/elements.rb @@ -0,0 +1,109 @@ +#!/usr/local/bin/ruby -w + +# $Id$ + +# the definition of our state tree +# the base class for both leaves and branches, and the base class for each +# of them, also + +require 'blink' +require 'blink/statechange' + +#--------------------------------------------------------------- +class Blink::Element + attr_accessor :noop + + #--------------------------------------------------------------- + @@interface_methods = [ + :retrieve, :insync?, :sync, :fqpath, :evaluate, :refresh + ] + + @@interface_methods.each { |method| + self.send(:define_method,method) { + raise "%s has not overridden %s" % [self.class,method] + } + } + + class Blink::Element::Branch + attr_accessor :children, :parent, :states + + #--------------------------------------------------------------- + # iterate across all children, and then iterate across states + # we do children first so we're sure that all dependent objects + # are checked first + def each + [@children,@states].each { |child| + yield child + } + end + #--------------------------------------------------------------- + + #--------------------------------------------------------------- + # this method is responsible for collecting state changes + # we always descend into the children before we evaluate our current + # states + def evaluate(transaction) + self.each { |child| child.evaluate } + end + #--------------------------------------------------------------- + + #--------------------------------------------------------------- + def initialize + @childary = [] + @childhash = {} + @states = [] + @parent = nil + end + #--------------------------------------------------------------- + + #--------------------------------------------------------------- + # this method is responsible for handling changes in dependencies + # for instance, restarting a service if a config file is changed + # in general, we just hand the method up to our parent, but for + # objects that might need to refresh, they'll override this method + # XXX at this point, if all dependent objects change, then this method + # might get called for each change + def refresh(transaction) + unless @parent.nil? + @parent.refresh(transaction) + end + end + #--------------------------------------------------------------- + end + + #--------------------------------------------------------------- + class Blink::Element::Leaf + attr_accessor :is, :should, :parent + + #--------------------------------------------------------------- + # this assumes the controlling process will actually execute the change + # which will demonstrably not work with states that are part of a larger + # whole, like FileRecordStates + def evaluate(transaction) + self.retrieve + transaction.change(Blink::StateChange.new(state)) unless self.insync? + end + #--------------------------------------------------------------- + + #--------------------------------------------------------------- + def initialize + @is = nil + @should = nil + @parent = nil + end + #--------------------------------------------------------------- + + #--------------------------------------------------------------- + def refresh(transaction) + self.retrieve + + # we definitely need some way to batch these refreshes, so a + # given object doesn't get refreshed multiple times in a single + # run + @parent.refresh + end + #--------------------------------------------------------------- + end + #--------------------------------------------------------------- +end +#--------------------------------------------------------------- diff --git a/lib/blink/statetree.rb b/lib/blink/statetree.rb deleted file mode 100644 index 64fac9b24..000000000 --- a/lib/blink/statetree.rb +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/local/bin/ruby -w - -# $Id$ - -# the definition of our state tree -# the base class for both leaves and branches, and the base class for each -# of them, also - -require 'blink' -require 'blink/statechange' - -#--------------------------------------------------------------- -class Blink::Element - attr_accessor :noop - - #--------------------------------------------------------------- - @@interface_methods = [ - :retrieve, :insync?, :sync, :fqpath, :evaluate, :refresh - ] - - @@interface_methods.each { |method| - self.send(:define_method,method) { - raise "%s has not overridden %s" % [self.class,method] - } - } - - class Blink::Element::Branch - attr_accessor :children, :parent, :states - - #--------------------------------------------------------------- - # iterate across all children, and then iterate across states - # we do children first so we're sure that all dependent objects - # are checked first - def each - [@children,@states].each { |child| - yield child - } - end - #--------------------------------------------------------------- - - #--------------------------------------------------------------- - # this method is responsible for collecting state changes - # we always descend into the children before we evaluate our current - # states - def evaluate(transaction) - self.each { |child| child.evaluate } - end - #--------------------------------------------------------------- - - #--------------------------------------------------------------- - def initialize - @childary = [] - @childhash = {} - @states = [] - @parent = nil - end - #--------------------------------------------------------------- - - #--------------------------------------------------------------- - # this method is responsible for handling changes in dependencies - # for instance, restarting a service if a config file is changed - # in general, we just hand the method up to our parent, but for - # objects that might need to refresh, they'll override this method - # XXX at this point, if all dependent objects change, then this method - # might get called for each change - def refresh(transaction) - unless @parent.nil? - @parent.refresh(transaction) - end - end - #--------------------------------------------------------------- - end - - #--------------------------------------------------------------- - class Blink::Element::Leaf - attr_accessor :is, :should, :parent - - #--------------------------------------------------------------- - # this assumes the controlling process will actually execute the change - # which will demonstrably not work with states that are part of a larger - # whole, like FileRecordStates - def evaluate(transaction) - self.retrieve - transaction.change(Blink::StateChange.new(state)) unless self.insync? - end - #--------------------------------------------------------------- - - #--------------------------------------------------------------- - def initialize - @is = nil - @should = nil - @parent = nil - end - #--------------------------------------------------------------- - - #--------------------------------------------------------------- - def refresh(transaction) - self.retrieve - - # we definitely need some way to batch these refreshes, so a - # given object doesn't get refreshed multiple times in a single - # run - @parent.refresh - end - #--------------------------------------------------------------- - end - #--------------------------------------------------------------- -end -#--------------------------------------------------------------- -- cgit