Class: HermesAgent::Client::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/hermes_agent/client/entity.rb

Overview

Base class for lightweight wrappers around parsed JSON payloads.

Subclasses add method readers for the fields we have mapped, but the raw parsed payload is always available as the source of truth: #to_h returns the full hash and #[] reads an individual key.

Entities are immutable value objects: both the entity and its underlying payload are frozen on construction, and equality (#== / #eql? / #hash) is by class and payload, so entities can be compared and used as Hash keys.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ boolean

Whether this entity equals another: true when other is an instance of the same class wrapping equal payload data.

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (boolean)


64
65
66
# File 'lib/hermes_agent/client/entity.rb', line 64

def ==(other)
  other.instance_of?(self.class) && other.to_h == @data
end

#[](key) ⇒ Object?

Read a raw field by its server-side (string) key.

Parameters:

  • key (String)

    The field name.

Returns:

  • (Object, nil)

    The raw value, or nil if absent.



44
45
46
# File 'lib/hermes_agent/client/entity.rb', line 44

def [](key)
  @data[key]
end

#eql?(other) ⇒ boolean

Alias of #==, so entities behave consistently as Hash keys (paired with #hash).

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (boolean)


75
76
77
# File 'lib/hermes_agent/client/entity.rb', line 75

def eql?(other)
  self == other
end

#hashInteger

A hash code consistent with #== and #eql?.

Returns:

  • (Integer)


84
85
86
# File 'lib/hermes_agent/client/entity.rb', line 84

def hash
  [self.class, @data].hash
end

#to_hHash

The full parsed payload (frozen).

Returns:

  • (Hash)

    The raw response body with string keys.



53
54
55
# File 'lib/hermes_agent/client/entity.rb', line 53

def to_h
  @data
end