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.
Files changed (4) hide show
  1. data/README.rdoc +29 -9
  2. data/VERSION +1 -1
  3. data/lib/dirty-memoize.rb +5 -5
  4. metadata +2 -2
@@ -1,16 +1,36 @@
1
1
  = dirty-memoize
2
2
 
3
- Description goes here.
3
+ Like Memoize, but designed for mutable and parametizable objects
4
4
 
5
- == Note on Patches/Pull Requests
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
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
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
1
+ 0.0.3
@@ -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=true
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=false
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=true if @dirty.nil?
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=true
81
+ @dirty=:true
82
82
  end
83
83
 
84
84
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Claudio Bustos