mutability 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mutability/mutable.rb +48 -0
- data/lib/mutability/mutable_array.rb +9 -0
- data/lib/mutability/mutable_hash.rb +9 -0
- data/lib/mutability/version.rb +3 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a58152afaa3f64181624fbe5d87fe552ef741b7f
|
4
|
+
data.tar.gz: bb33dce7188e8e74dff1fb7a2bf7aa8def65f767
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c0bc0028bf22d5f69d2d9c964d71c0d91c04d0f34a8ede8864a4a4c1f0d945f9b61fae3b6baf9ed7c47801947eb7a592ae01b642d4bc6dc8e2d6eccd0225b1c
|
7
|
+
data.tar.gz: d8071bc563dcba0e1bf06a216fe930073e38abc92077766f792192bd2bc96d6e81aa98b0730c06071357056a41b6f9b229ac61044368b3bcb376de401a9e591f
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
# **************************************************************************
|
4
|
+
# Mutability::Mutable
|
5
|
+
#
|
6
|
+
# This class adds a wrapper around a an object that keeps an unchanged
|
7
|
+
# copy of the object. This allows it to be mutated as much as needed and
|
8
|
+
# still be compared against the original version, or even reset to the
|
9
|
+
# original.
|
10
|
+
#
|
11
|
+
# Adds the freeze! and revert! methods and delegates most methods to the
|
12
|
+
# working version (@self). The following methods act upon the Mutable
|
13
|
+
# object itself and not its working version:
|
14
|
+
# - #inspect
|
15
|
+
# - #=
|
16
|
+
# - any equality methods that are not built on #== or #===
|
17
|
+
# **************************************************************************
|
18
|
+
|
19
|
+
module Mutability
|
20
|
+
class Mutable
|
21
|
+
extend Forwardable
|
22
|
+
|
23
|
+
def_delegators :@self, *Mutability::DELEGATED_METHODS
|
24
|
+
|
25
|
+
attr_reader :original
|
26
|
+
attr_accessor :self
|
27
|
+
|
28
|
+
def initialize(original)
|
29
|
+
@original = original.freeze # don't modify, even accidentally!
|
30
|
+
revert!
|
31
|
+
end
|
32
|
+
|
33
|
+
def freeze!
|
34
|
+
@original = @self.dup.freeze
|
35
|
+
end
|
36
|
+
|
37
|
+
def revert!
|
38
|
+
@self = @original.dup # we want an unfrozen copy, so cannot clone
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# cheap delegation
|
44
|
+
def method_missing(sym, *args, &block)
|
45
|
+
@self.send(sym, *args, &block)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mutability
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelli Searfos
|
@@ -78,6 +78,10 @@ extensions: []
|
|
78
78
|
extra_rdoc_files: []
|
79
79
|
files:
|
80
80
|
- lib/mutability.rb
|
81
|
+
- lib/mutability/mutable.rb
|
82
|
+
- lib/mutability/mutable_array.rb
|
83
|
+
- lib/mutability/mutable_hash.rb
|
84
|
+
- lib/mutability/version.rb
|
81
85
|
homepage: https://github.com/ksearfos/mutability
|
82
86
|
licenses:
|
83
87
|
- MIT
|