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.
-
#merge!(other) ⇒ self
Merge in additional data.
-
#method_missing(method_name, *args, &block) ⇒ 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, &block) ⇒ Object
Allow method-style access to keys
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/robot_lab/state_proxy.rb', line 158 def method_missing(method_name, *args, &block) 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 50 |
# 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 value end |
#delete(key) ⇒ Object
Delete a key
97 98 99 |
# File 'lib/robot_lab/state_proxy.rb', line 97 def delete(key) @data.delete(key.to_sym) end |
#dup ⇒ StateProxy
Deep duplicate
124 125 126 |
# File 'lib/robot_lab/state_proxy.rb', line 124 def dup StateProxy.new(deep_dup(@data), on_change: @on_change) end |
#each {|Symbol, Object| ... } ⇒ Object
Iterate over key-value pairs
88 89 90 |
# File 'lib/robot_lab/state_proxy.rb', line 88 def each(&block) @data.each(&block) end |
#empty? ⇒ Boolean
Check if empty
132 133 134 |
# File 'lib/robot_lab/state_proxy.rb', line 132 def empty? @data.empty? end |
#inspect ⇒ Object
173 174 175 |
# File 'lib/robot_lab/state_proxy.rb', line 173 def inspect "#<RobotLab::StateProxy #{@data.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?, include?
Check if key exists
57 58 59 |
# File 'lib/robot_lab/state_proxy.rb', line 57 def key?(key) @data.key?(key.to_sym) end |
#keys ⇒ Array<Symbol>
Get all keys
72 73 74 |
# File 'lib/robot_lab/state_proxy.rb', line 72 def keys @data.keys end |
#merge!(other) ⇒ self
Merge in additional data
106 107 108 109 |
# File 'lib/robot_lab/state_proxy.rb', line 106 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
147 148 149 150 |
# File 'lib/robot_lab/state_proxy.rb', line 147 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
140 141 142 |
# File 'lib/robot_lab/state_proxy.rb', line 140 def size @data.size end |
#to_h ⇒ Hash Also known as: to_hash
Convert to plain hash
115 116 117 |
# File 'lib/robot_lab/state_proxy.rb', line 115 def to_h @data.dup end |
#values ⇒ Array
Get all values
80 81 82 |
# File 'lib/robot_lab/state_proxy.rb', line 80 def values @data.values end |