Class: Beaconable::AttributeSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/beaconable/attribute_snapshot.rb

Overview

Immutable snapshot of an ActiveRecord object’s attributes at a point in time. Replaces OpenStruct for better performance and explicit immutability.

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeSnapshot

Returns a new instance of AttributeSnapshot.



7
8
9
# File 'lib/beaconable/attribute_snapshot.rb', line 7

def initialize(attributes)
  @attributes = attributes.freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



11
12
13
14
15
16
# File 'lib/beaconable/attribute_snapshot.rb', line 11

def method_missing(method_name, *args)
  key = method_name.to_sym
  return @attributes[key] if @attributes.key?(key)

  super
end

Instance Method Details

#[](key) ⇒ Object

Direct hash-style access for attribute values



23
24
25
# File 'lib/beaconable/attribute_snapshot.rb', line 23

def [](key)
  @attributes[key.to_sym]
end

#inspectObject



32
33
34
# File 'lib/beaconable/attribute_snapshot.rb', line 32

def inspect
  "#<#{self.class.name} #{@attributes.inspect}>"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/beaconable/attribute_snapshot.rb', line 18

def respond_to_missing?(method_name, include_private = false)
  @attributes.key?(method_name.to_sym) || super
end

#to_hObject

Returns a copy of the attributes hash (for debugging/inspection)



28
29
30
# File 'lib/beaconable/attribute_snapshot.rb', line 28

def to_h
  @attributes.dup
end