lib/arborist/node.rb in arborist-0.0.1.pre20160606141735 vs lib/arborist/node.rb in arborist-0.0.1.pre20160829140603

- old
+ new

@@ -248,10 +248,11 @@ @identifier = identifier @parent = parent_node ? parent_node.identifier : '_' @description = nil @tags = Set.new @properties = {} + @config = {} @source = nil @children = {} @dependencies = Arborist::Dependency.new( :all ) # Primary state @@ -419,11 +420,18 @@ def has_dependencies? return !self.dependencies.empty? end + ### Get or set the node's configuration hash. This can be used to pass per-node + ### information to systems using the tree (e.g., monitors, subscribers). + def config( new_config=nil ) + @config = stringify_keys( new_config ) if new_config + return @config + end + # # :section: Manager API # Methods used by the manager to manage its nodes. # @@ -552,10 +560,12 @@ self.log.debug "Checking node type %p against %p" % [ self.type, val ] self.type == val when 'tag' then @tags.include?( val.to_s ) when 'tags' then Array(val).all? {|tag| @tags.include?(tag) } when 'identifier' then @identifier == val + when 'config' + val.all? {|ikey, ival| hash_matches(@config, ikey, ival) } else hash_matches( @properties, key, val ) end end @@ -636,11 +646,11 @@ handler_name = "handle_%s_event" % [ event.type.gsub('.', '_') ] if self.respond_to?( handler_name ) self.log.debug "Handling a %s event." % [ event.type ] self.method( handler_name ).call( event ) end - super + super # to state-machine end ### Returns +true+ if this node's dependencies are not met. def dependencies_down? @@ -838,10 +848,11 @@ identifier: self.identifier, type: self.class.name.to_s.sub( /.+::/, '' ).downcase, parent: self.parent, description: self.description, tags: self.tags, + config: self.config, status: self.status, properties: self.properties.dup, ack: self.ack ? self.ack.to_h : nil, last_contacted: self.last_contacted ? self.last_contacted.iso8601 : nil, status_changed: self.status_changed ? self.status_changed.iso8601 : nil, @@ -859,20 +870,23 @@ ### Marshal API -- set up the object's state using the +hash+ from a ### previously-marshalled node. def marshal_load( hash ) + self.log.debug "Restoring from serialized hash: %p" % [ hash ] @identifier = hash[:identifier] @properties = hash[:properties] @parent = hash[:parent] @description = hash[:description] @tags = Set.new( hash[:tags] ) + @config = hash[:config] @children = {} - @status = 'unknown' + @status = hash[:status] @status_changed = Time.parse( hash[:status_changed] ) + @ack = Arborist::Node::Ack.from_hash( hash[:ack] ) if hash[:ack] @error = hash[:error] @properties = hash[:properties] || {} @last_contacted = Time.parse( hash[:last_contacted] ) @quieted_reasons = hash[:quieted_reasons] || {} @@ -884,10 +898,9 @@ end @pending_update_events = [] @subscriptions = {} - self.ack = hash[:ack] end ### Equality operator -- returns +true+ if +other_node+ has the same identifier, parent, and ### state as the receiving one.