mighty_struct 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/mighty_struct.rb +5 -6
- data/lib/mighty_struct/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 642f851ca113981d37948247c72b591f899859a8
|
4
|
+
data.tar.gz: 299295c4805aef048033602b69507c4c15a6af3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55ff409bb2d69a8c1b701196b3606bac83f02183b7265fcc95639c32cecad7aeed2068bcacb00d3ed342a6e66cce2203a84f41becb5f45b8837b9be40eb32e6c
|
7
|
+
data.tar.gz: 694ad977679d128448db968c403222204dcda4caf9a4402264e05be27b5c0b9223a3d2ff51d03de63d084e8b74d501dd1d402c4695264c42ee57f1bfb981efc1
|
data/README.md
CHANGED
@@ -50,7 +50,7 @@ Or play with it on your own. It's just one command (line) away.
|
|
50
50
|
git clone https://github.com/msievers/mighty_struct.git && cd mighty_struct && bundle && bin/console
|
51
51
|
```
|
52
52
|
|
53
|
-
## Another of this "method invocation
|
53
|
+
## Another of this "method invocation hashes", really?!
|
54
54
|
|
55
55
|
Before I started coding this, I tried the following three alternatives
|
56
56
|
|
data/lib/mighty_struct.rb
CHANGED
@@ -15,17 +15,16 @@ class MightyStruct
|
|
15
15
|
raise ArgumentError.new("Cannot create a an instance of #{self.class} for the given object!")
|
16
16
|
end
|
17
17
|
|
18
|
-
@cache = {}
|
19
|
-
@cache_mode = options[:caching] || :enabled
|
18
|
+
@cache = options[:caching] == :enabled ? {} : nil
|
20
19
|
|
21
20
|
if (@object = object).respond_to?(:keys)
|
22
21
|
object.keys.each do |_key|
|
23
22
|
unless respond_to?(_key)
|
24
23
|
define_singleton_method(_key) do
|
25
|
-
if @
|
26
|
-
|
24
|
+
if @cache
|
25
|
+
@cache[_key] ||= self.class.new?(value = @object[_key]) ? self.class.new(value) : value
|
27
26
|
else
|
28
|
-
|
27
|
+
self.class.new?(value = @object[_key]) ? self.class.new(value) : value
|
29
28
|
end
|
30
29
|
end
|
31
30
|
end
|
@@ -38,7 +37,7 @@ class MightyStruct
|
|
38
37
|
#
|
39
38
|
def method_missing(method_name, *arguments, &block)
|
40
39
|
if @object.respond_to?(method_name)
|
41
|
-
@cache.clear if @
|
40
|
+
@cache.clear if @cache # clear the properties cache, because the called method may have side-effects
|
42
41
|
result = @object.send(method_name, *arguments, &block)
|
43
42
|
|
44
43
|
# ensure that results of called methods are mighty structs again
|