Sha256: be594e2dcc82f936303828631df8a60aaedd8f0f3223998eea35216d4de0e2f7

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

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

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


# The inner class for the 'ack' operational property
class Arborist::Node::Ack
	extend Arborist::HashUtilities

	### Construct an instance from the values in the specified +hash+.
	def self::from_hash( hash )
		hash = symbolify_keys( hash )

		message = hash.delete( :message ) or raise ArgumentError, "Missing required ACK message"
		sender  = hash.delete( :sender )  or raise ArgumentError, "Missing required ACK sender"

		if hash[:time]
			hash[:time] = Time.at( hash[:time] ) if hash[:time].is_a?( Numeric )
			hash[:time] = Time.parse( hash[:time] ) unless hash[:time].is_a?( Time )
		end

		return new( message, sender, **hash )
	end


	### Create a new acknowledgement
	def initialize( message, sender, via: nil, time: nil )
		time ||= Time.now

		@message = message
		@sender  = sender
		@via     = via
		@time    = time.to_time
	end

	##
	# The object's message, :sender, :via, :time
	attr_reader :message, :sender, :via, :time


	### Return a string description of the acknowledgement for logging and inspection.
	def description
		return "by %s%s -- %s" % [
			self.sender,
			self.via ? " via #{self.via}" : '',
			self.message
		]
	end


	### Return the Ack as a Hash.
	def to_h
		return {
			message: self.message,
			sender: self.sender,
			via: self.via,
			time: self.time.iso8601,
		}
	end


	### Returns true if the +other+ object is an Ack with the same values.
	def ==( other )
		return other.is_a?( self.class ) &&
			self.to_h == other.to_h
	end

end # class Arborist::Node::Ack


Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
arborist-0.2.0.pre20170519125456 lib/arborist/node/ack.rb
arborist-0.1.0 lib/arborist/node/ack.rb
arborist-0.0.1.pre20161005182540 lib/arborist/node/ack.rb
arborist-0.0.1.pre20161005112841 lib/arborist/node/ack.rb
arborist-0.0.1.pre20160829140603 lib/arborist/node/ack.rb
arborist-0.0.1.pre20160606141735 lib/arborist/node/ack.rb