Class: RobotLab::StateProxy
- Inherits:
-
Object
- Object
- RobotLab::StateProxy
- Includes:
- Utils
- Defined in:
- lib/robot_lab/state_proxy.rb
Overview
Proxy wrapper for state data that tracks mutations
StateProxy wraps a hash and intercepts read/write operations, providing a clean interface for state access while enabling optional change tracking.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get value by key.
-
#[]=(key, value) ⇒ Object
Set value by key.
-
#delete(key) ⇒ Object
Delete a key.
-
#dup ⇒ StateProxy
Deep duplicate.
-
#each {|Symbol, Object| ... } ⇒ Object
Iterate over key-value pairs.
-
#empty? ⇒ Boolean
Check if empty.
-
#initialize(data = {}, on_change: nil) ⇒ StateProxy
constructor
Creates a new StateProxy.
- #inspect ⇒ Object
-
#key?(key) ⇒ Boolean
(also: #has_key?, #include?)
Check if key exists.
-
#keys ⇒ Array<Symbol>
Get all keys.
- #map ⇒ Object
-
#merge!(other) ⇒ self
Merge in additional data.
-
#method_missing(method_name, *args) ⇒ Object
Allow method-style access to keys.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to method calls as hash access.
-
#size ⇒ Integer
(also: #length)
Number of keys.
-
#to_h ⇒ Hash
(also: #to_hash)
Convert to plain hash.
-
#values ⇒ Array
Get all values.
Constructor Details
#initialize(data = {}, on_change: nil) ⇒ StateProxy
Creates a new StateProxy.
25 26 27 28 |
# File 'lib/robot_lab/state_proxy.rb', line 25 def initialize(data = {}, on_change: nil) @data = data.transform_keys(&:to_sym) @on_change = on_change end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Allow method-style access to keys
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/robot_lab/state_proxy.rb', line 161 def method_missing(method_name, *args, &) method_str = method_name.to_s if method_str.end_with?("=") # Setter key = method_str.chomp("=").to_sym self[key] = args.first elsif @data.key?(method_name.to_sym) # Getter self[method_name] else super end end |
Instance Method Details
#[](key) ⇒ Object
Get value by key
35 36 37 |
# File 'lib/robot_lab/state_proxy.rb', line 35 def [](key) @data[key.to_sym] end |
#[]=(key, value) ⇒ Object
Set value by key
44 45 46 47 48 49 |
# File 'lib/robot_lab/state_proxy.rb', line 44 def []=(key, value) key = key.to_sym old_value = @data[key] @data[key] = value @on_change&.call(key, old_value, value) if old_value != value end |
#delete(key) ⇒ Object
Delete a key
100 101 102 |
# File 'lib/robot_lab/state_proxy.rb', line 100 def delete(key) @data.delete(key.to_sym) end |
#dup ⇒ StateProxy
Deep duplicate
127 128 129 |
# File 'lib/robot_lab/state_proxy.rb', line 127 def dup StateProxy.new(deep_dup(@data), on_change: @on_change) end |
#each {|Symbol, Object| ... } ⇒ Object
Iterate over key-value pairs
87 88 89 |
# File 'lib/robot_lab/state_proxy.rb', line 87 def each(&) @data.each(&) end |
#empty? ⇒ Boolean
Check if empty
135 136 137 |
# File 'lib/robot_lab/state_proxy.rb', line 135 def empty? @data.empty? end |
#inspect ⇒ Object
176 177 178 |
# File 'lib/robot_lab/state_proxy.rb', line 176 def inspect "#<RobotLab::StateProxy #{@data.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?, include?
Check if key exists
56 57 58 |
# File 'lib/robot_lab/state_proxy.rb', line 56 def key?(key) @data.key?(key.to_sym) end |
#keys ⇒ Array<Symbol>
Get all keys
71 72 73 |
# File 'lib/robot_lab/state_proxy.rb', line 71 def keys @data.keys end |
#map ⇒ Object
91 92 93 |
# File 'lib/robot_lab/state_proxy.rb', line 91 def map(&) @data.map(&) end |
#merge!(other) ⇒ self
Merge in additional data
109 110 111 112 |
# File 'lib/robot_lab/state_proxy.rb', line 109 def merge!(other) other.each { |k, v| self[k] = v } self end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to method calls as hash access
150 151 152 153 |
# File 'lib/robot_lab/state_proxy.rb', line 150 def respond_to_missing?(method_name, include_private = false) key = method_name.to_s.chomp("=").to_sym @data.key?(key) || super end |
#size ⇒ Integer Also known as: length
Number of keys
143 144 145 |
# File 'lib/robot_lab/state_proxy.rb', line 143 def size @data.size end |
#to_h ⇒ Hash Also known as: to_hash
Convert to plain hash
118 119 120 |
# File 'lib/robot_lab/state_proxy.rb', line 118 def to_h @data.dup end |
#values ⇒ Array
Get all values
79 80 81 |
# File 'lib/robot_lab/state_proxy.rb', line 79 def values @data.values end |