Class: ActiveType::Util::UnmutableAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/active_type/util/unmutable_attributes.rb

Overview

This object is used as a substitute for a record’s @attributes. Reading from the original @attributes is still allowed, to enable ‘#inspect` and similar functions. But the @attributes can no longer be mutated and will raise instead.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ UnmutableAttributes

Returns a new instance of UnmutableAttributes.



14
15
16
# File 'lib/active_type/util/unmutable_attributes.rb', line 14

def initialize(attributes)
  @original_attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



30
31
32
# File 'lib/active_type/util/unmutable_attributes.rb', line 30

def method_missing(*args)
  raise MutationAfterCastError, 'Changing a record that has been used to create an ActiveType::Record could have unexpected side effects!'
end

Instance Attribute Details

#original_attributesObject (readonly)

Returns the value of attribute original_attributes.



12
13
14
# File 'lib/active_type/util/unmutable_attributes.rb', line 12

def original_attributes
  @original_attributes
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/active_type/util/unmutable_attributes.rb', line 22

def [](key)
  original_attributes[key]
end

#fetch_value(key) ⇒ Object



18
19
20
# File 'lib/active_type/util/unmutable_attributes.rb', line 18

def fetch_value(key)
  original_attributes.fetch_value(key)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/active_type/util/unmutable_attributes.rb', line 26

def key?(key)
  original_attributes.key?(key)
end