Class: YPetri::Agent::HashKeyPointer

Inherits:
Object
  • Object
show all
Defined in:
lib/y_petri/agent/hash_key_pointer.rb

Overview

Represents a pointer to a key of a specific hash associated with the pointer instance. Used to implement pointers of the Agent class.

Direct Known Subclasses

SimulationAspect::SimulationPoint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash: nil, hash_value_is: '', default_key: nil) ⇒ HashKeyPointer

Upon initalization, hash key pointer requires a hash, with which the instance will be associated, a textual description explaining what does a value of the associated hash represent, and the default hash key.



17
18
19
20
21
# File 'lib/y_petri/agent/hash_key_pointer.rb', line 17

def initialize( hash: nil, hash_value_is: '', default_key: nil )
  @hash = hash
  @what_is_hash_value = hash_value_is
  @default_key = default_key
end

Instance Attribute Details

#keyObject (readonly)

Key at which the pointer points.



7
8
9
# File 'lib/y_petri/agent/hash_key_pointer.rb', line 7

def key
  @key
end

#what_is_hash_valueObject (readonly)

Short text explaining what does a value of the associated hash represent.



11
12
13
# File 'lib/y_petri/agent/hash_key_pointer.rb', line 11

def what_is_hash_value
  @what_is_hash_value
end

Instance Method Details

#getObject

Gets the value paired in the hash associated with the current key to which this pointer points.



34
35
36
37
# File 'lib/y_petri/agent/hash_key_pointer.rb', line 34

def get
  return @hash[@default_key] if @key.nil?
  @hash[@key] or raise "No #{what_is_hash_value} identified by #{arg}!"
end

#resetObject

Resets the key to the default key.



25
# File 'lib/y_petri/agent/hash_key_pointer.rb', line 25

def reset; @key = @default_key end

#set(arg) ⇒ Object

Sets the pointer key to the one given in the argument.



29
# File 'lib/y_petri/agent/hash_key_pointer.rb', line 29

def set arg; @key = arg end