Sha256: 433f3da1c7ee05b4f227512c33cf3a88217e95d3c59b5f4dedd98f13cf20e4ca

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# -*- ruby -*-
#encoding: utf-8

require 'arborist/event' unless defined?( Arborist::Event )
require 'arborist/event/node'
require 'arborist/mixins'


# An event sent when one or more attributes of a node changes.
class Arborist::Event::NodeDelta < Arborist::Event::Node
	include Arborist::HashUtilities


	### Create a new NodeDelta event for the specified +node+. The +delta+
	### is a Hash of:
	###    attribute_name => [ old_value, new_value ]
	def initialize( node, delta )
		super # Overridden for the documentation
	end


	### Overridden so delta events only contain the diff of attributes that changed.
	def payload
		return @payload
	end


	### Returns +true+ if the specified +object+ matches this event.
	def match( object )
		return super &&
			object.respond_to?( :criteria ) && self.delta_matches?( object.criteria )
	end


	### Returns +true+ if the 'delta' value of the specified +criteria+ (which
	### must respond to .all?) matches the delta this event represents.
	def delta_matches?( criteria )
		delta_criteria = criteria['delta'] || {}
		self.log.debug "Matching event against delta criteria: %p" % [ delta_criteria ]

		return delta_criteria.all? do |key, val|
			self.log.debug "  matching %p: %p against %p" % [ key, val, self.payload ]
			hash_matches( self.payload, key, val )
		end.tap {|match| self.log.debug "  event delta %s match." % [ match ? "DID" : "did not"] }
	end

end # class Arborist::Event::NodeDelta

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arborist-0.0.1.pre20160829140603 lib/arborist/event/node_delta.rb
arborist-0.0.1.pre20160606141735 lib/arborist/event/node_delta.rb