memorandum 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/memorandum/version.rb +1 -1
- data/lib/memorandum.rb +30 -8
- 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: 8d3980188220f33f7940fc8b959308b0bbd38f8e
|
4
|
+
data.tar.gz: 5296434b08e758ad82b5813cf389ed8029db3297
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbad7ea1fe756c936a463c7bbb520e925db00f83877fe5146c95217872c245bbd26b733303437398038da7db850d47323559ec08b1eec19f928852f5bc80690f
|
7
|
+
data.tar.gz: 159b25d64d9d61e559ccbe51a506d6bb50d4273a470f9adde14a6667e5b01a80bcf6a23c0ee157d02debc55a5f48d94bdb371575045b239f7f771d755ae30dcb
|
data/lib/memorandum/version.rb
CHANGED
data/lib/memorandum.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
require 'ice_nine'
|
2
2
|
|
3
3
|
module Memorandum
|
4
|
+
def self.extended base
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
end
|
7
|
+
|
4
8
|
def memo *arguments
|
5
9
|
name = arguments.pop
|
6
10
|
flags = arguments
|
7
11
|
|
8
|
-
memoized_statuses =
|
9
|
-
memoized_values =
|
10
|
-
unmemoized_name = "
|
12
|
+
memoized_statuses = memorandum_ivar_name MEMORANDUM_STATUSES, name
|
13
|
+
memoized_values = memorandum_ivar_name MEMORANDUM_VALUES, name
|
14
|
+
unmemoized_name = "#{MEMORANDUM_UNMEMOIZED}_#{name}"
|
11
15
|
|
12
16
|
alias_method unmemoized_name, name
|
13
17
|
|
14
18
|
define_method name do |*args|
|
15
|
-
statuses =
|
16
|
-
|
17
|
-
|
18
|
-
values = instance_variable_get(memoized_values) \
|
19
|
-
|| instance_variable_set(memoized_values, Hash[])
|
19
|
+
statuses = memorandum_fetch memoized_statuses, Hash[]
|
20
|
+
values = memorandum_fetch memoized_values, Hash[]
|
20
21
|
|
21
22
|
unless statuses[args]
|
22
23
|
value = send unmemoized_name, *args
|
@@ -31,4 +32,25 @@ module Memorandum
|
|
31
32
|
values[args]
|
32
33
|
end
|
33
34
|
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
MEMORANDUM_MEMOIZED = 'memoized'.freeze
|
39
|
+
MEMORANDUM_STATUSES = 'statuses'.freeze
|
40
|
+
MEMORANDUM_UNMEMOIZED = 'unmemoized'.freeze
|
41
|
+
MEMORANDUM_VALUES = 'values'.freeze
|
42
|
+
MEMORANDUM_BANG = '__BANG__'.freeze
|
43
|
+
MEMORANDUM_BOOL = '__BOOL__'.freeze
|
44
|
+
|
45
|
+
def memorandum_ivar_name name, method_name
|
46
|
+
"@#{MEMORANDUM_MEMOIZED}_#{name}_for_#{method_name}"
|
47
|
+
.gsub('!', MEMORANDUM_BANG)
|
48
|
+
.gsub('?', MEMORANDUM_BOOL)
|
49
|
+
end
|
50
|
+
|
51
|
+
module InstanceMethods
|
52
|
+
def memorandum_fetch name, default
|
53
|
+
instance_variable_get(name) || instance_variable_set(name, default)
|
54
|
+
end
|
55
|
+
end
|
34
56
|
end
|