dirty-memoize 0.0.1 → 0.0.3
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.
- data/README.rdoc +29 -9
- data/VERSION +1 -1
- data/lib/dirty-memoize.rb +5 -5
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,16 +1,36 @@
|
|
1
1
|
= dirty-memoize
|
2
2
|
|
3
|
-
|
3
|
+
Like Memoize, but designed for mutable and parametizable objects
|
4
4
|
|
5
|
-
|
5
|
+
Use when:
|
6
|
+
1. You have one expensive method (\compute) which set many internal
|
7
|
+
variables. So, is preferable lazy evaluation of these dependent variables.
|
8
|
+
2. The expensive operation depends on one or more parameters
|
9
|
+
3. Changes on one or more parameters affect all dependent variables
|
10
|
+
4. You may want to hide the call of 'compute' operation
|
11
|
+
5. The user could want test several different parameters values
|
12
|
+
|
13
|
+
By default, the method to compute should be called \#compute.
|
14
|
+
Set constant DIRTY_COMPUTE to the name of other method if you need it
|
15
|
+
|
16
|
+
Example:
|
6
17
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
18
|
+
class ExpensiveCalculation
|
19
|
+
include DirtyMemoize
|
20
|
+
attr_reader :a
|
21
|
+
attr_accesor :x
|
22
|
+
# Your evil function
|
23
|
+
def compute
|
24
|
+
@a=x**x**x
|
25
|
+
end
|
26
|
+
end
|
27
|
+
a=new ExpensiveCalculation
|
28
|
+
a.x=1
|
29
|
+
puts a.a
|
30
|
+
|
31
|
+
== Sugestions
|
32
|
+
|
33
|
+
* Fork, modify and do wathever you need with it.
|
14
34
|
|
15
35
|
== Copyright
|
16
36
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/dirty-memoize.rb
CHANGED
@@ -40,7 +40,7 @@ module DirtyMemoize
|
|
40
40
|
sym=sym.to_s+"="
|
41
41
|
alias_method((sym.to_s+"_whitout_dirty").intern, sym)
|
42
42
|
define_method(sym) do |*args|
|
43
|
-
@dirty
|
43
|
+
@dirty=:true
|
44
44
|
send(sym.to_s+"_whitout_dirty", *args)
|
45
45
|
end
|
46
46
|
end
|
@@ -59,7 +59,7 @@ module DirtyMemoize
|
|
59
59
|
end
|
60
60
|
@compute_count||=0
|
61
61
|
@compute_count+=1
|
62
|
-
@dirty
|
62
|
+
@dirty=:false
|
63
63
|
end
|
64
64
|
@cache[sym]||=Hash.new
|
65
65
|
@cache[sym][args]||=send(sym.to_s+"_without_dirty", *args)
|
@@ -69,8 +69,8 @@ module DirtyMemoize
|
|
69
69
|
end # end of ClassMethods
|
70
70
|
# Is the object dirty?
|
71
71
|
def dirty?
|
72
|
-
@dirty
|
73
|
-
@dirty
|
72
|
+
@dirty||=:true
|
73
|
+
@dirty==:true
|
74
74
|
end
|
75
75
|
# Number of compute's runs
|
76
76
|
def compute_count
|
@@ -78,7 +78,7 @@ module DirtyMemoize
|
|
78
78
|
end
|
79
79
|
def clean_cache
|
80
80
|
@cache=Hash.new
|
81
|
-
@dirty
|
81
|
+
@dirty=:true
|
82
82
|
end
|
83
83
|
|
84
84
|
end
|