Class: Mutability::Mutable

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mutability/mutable.rb

Direct Known Subclasses

MutableArray, MutableHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original) ⇒ Mutable

Returns a new instance of Mutable.



28
29
30
31
# File 'lib/mutability/mutable.rb', line 28

def initialize(original)
  @original = original.freeze   # don't modify, even accidentally!
  revert!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)

cheap delegation



44
45
46
# File 'lib/mutability/mutable.rb', line 44

def method_missing(sym, *args, &block)
  @self.send(sym, *args, &block)
end

Instance Attribute Details

#originalObject (readonly)

Returns the value of attribute original.



25
26
27
# File 'lib/mutability/mutable.rb', line 25

def original
  @original
end

#selfObject

Returns the value of attribute self.



26
27
28
# File 'lib/mutability/mutable.rb', line 26

def self
  @self
end

Instance Method Details

#freeze!Object



33
34
35
# File 'lib/mutability/mutable.rb', line 33

def freeze!
  @original = @self.dup.freeze
end

#revert!Object



37
38
39
# File 'lib/mutability/mutable.rb', line 37

def revert!
  @self = @original.dup         # we want an unfrozen copy, so cannot clone
end