Class: RobotLab::StateProxy

Inherits:
Object
  • Object
show all
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.

Examples:

data = { count: 0, name: "test" }
proxy = StateProxy.new(data)
proxy[:count] = 1
proxy.count       # => 1
proxy[:name]      # => "test"
proxy.to_h        # => { count: 1, name: "test" }

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, on_change: nil) ⇒ StateProxy

Creates a new StateProxy.

Parameters:

  • data (Hash) (defaults to: {})

    the initial data

  • on_change (Proc, nil) (defaults to: nil)

    callback invoked when a value changes



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

Examples:

proxy.name        # Same as proxy[:name]
proxy.name = "x"  # Same as proxy[:name] = "x"


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

Parameters:

  • key (Symbol, String)

Returns:

  • (Object)


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

Parameters:

  • key (Symbol, String)
  • value (Object)


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

Parameters:

  • key (Symbol, String)

Returns:

  • (Object)

    The deleted value



97
98
99
# File 'lib/robot_lab/state_proxy.rb', line 97

def delete(key)
  @data.delete(key.to_sym)
end

#dupStateProxy

Deep duplicate

Returns:



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

Yields:

  • (Symbol, Object)


88
89
90
# File 'lib/robot_lab/state_proxy.rb', line 88

def each(&block)
  @data.each(&block)
end

#empty?Boolean

Check if empty

Returns:

  • (Boolean)


132
133
134
# File 'lib/robot_lab/state_proxy.rb', line 132

def empty?
  @data.empty?
end

#inspectObject



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

Parameters:

  • key (Symbol, String)

Returns:

  • (Boolean)


57
58
59
# File 'lib/robot_lab/state_proxy.rb', line 57

def key?(key)
  @data.key?(key.to_sym)
end

#keysArray<Symbol>

Get all keys

Returns:

  • (Array<Symbol>)


72
73
74
# File 'lib/robot_lab/state_proxy.rb', line 72

def keys
  @data.keys
end

#merge!(other) ⇒ self

Merge in additional data

Parameters:

  • other (Hash)

Returns:

  • (self)


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

Returns:

  • (Boolean)


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

#sizeInteger Also known as: length

Number of keys

Returns:

  • (Integer)


140
141
142
# File 'lib/robot_lab/state_proxy.rb', line 140

def size
  @data.size
end

#to_hHash Also known as: to_hash

Convert to plain hash

Returns:

  • (Hash)


115
116
117
# File 'lib/robot_lab/state_proxy.rb', line 115

def to_h
  @data.dup
end

#valuesArray

Get all values

Returns:

  • (Array)


80
81
82
# File 'lib/robot_lab/state_proxy.rb', line 80

def values
  @data.values
end