Class: YPetri::Agent::HashKeyPointer
- Inherits:
-
Object
- Object
- YPetri::Agent::HashKeyPointer
- 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
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Key at which the pointer points.
-
#what_is_hash_value ⇒ Object
readonly
Short text explaining what does a value of the associated hash represent.
Instance Method Summary collapse
-
#get ⇒ Object
Gets the value paired in the hash associated with the current key to which this pointer points.
-
#initialize(hash: nil, hash_value_is: '', default_key: nil) ⇒ HashKeyPointer
constructor
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.
-
#reset ⇒ Object
Resets the key to the default key.
-
#set(arg) ⇒ Object
Sets the pointer key to the one given in the argument.
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
#key ⇒ Object (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_value ⇒ Object (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
#get ⇒ Object
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 |
#reset ⇒ Object
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 |