Class: Arborist::Node::Ack

Inherits:
Object
  • Object
show all
Extended by:
HashUtilities
Defined in:
lib/arborist/node/ack.rb

Overview

The inner class for the ‘ack’ operational property

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashUtilities

compact_hash, hash_matches, merge_recursively, stringify_keys, symbolify_keys

Constructor Details

#initialize(message, sender, via: nil, time: nil) ⇒ Ack

Create a new acknowledgement



29
30
31
32
33
34
35
36
# File 'lib/arborist/node/ack.rb', line 29

def initialize( message, sender, via: nil, time: nil )
  time ||= Time.now

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

Instance Attribute Details

#messageObject (readonly)

The object’s message, :sender, :via, :time



40
41
42
# File 'lib/arborist/node/ack.rb', line 40

def message
  @message
end

#senderObject (readonly)

The object’s message, :sender, :via, :time



40
41
42
# File 'lib/arborist/node/ack.rb', line 40

def sender
  @sender
end

#timeObject (readonly)

The object’s message, :sender, :via, :time



40
41
42
# File 'lib/arborist/node/ack.rb', line 40

def time
  @time
end

#viaObject (readonly)

The object’s message, :sender, :via, :time



40
41
42
# File 'lib/arborist/node/ack.rb', line 40

def via
  @via
end

Class Method Details

.from_hash(hash) ⇒ Object

Construct an instance from the values in the specified hash.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/arborist/node/ack.rb', line 13

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

Instance Method Details

#==(other) ⇒ Object

Returns true if the other object is an Ack with the same values.



65
66
67
68
# File 'lib/arborist/node/ack.rb', line 65

def ==( other )
  return other.is_a?( self.class ) &&
    self.to_h == other.to_h
end

#descriptionObject

Return a string description of the acknowledgement for logging and inspection.



44
45
46
47
48
49
50
# File 'lib/arborist/node/ack.rb', line 44

def description
  return "by %s%s -- %s" % [
    self.sender,
    self.via ? " via #{self.via}" : '',
    self.message
  ]
end

#to_hObject

Return the Ack as a Hash.



54
55
56
57
58
59
60
61
# File 'lib/arborist/node/ack.rb', line 54

def to_h( * )
  return {
    message: self.message,
    sender: self.sender,
    via: self.via,
    time: self.time.iso8601,
  }
end