dirty-memoize 0.0.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Claudio Bustos
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = dirty-memoize
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
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.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Claudio Bustos. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dirty-memoize"
8
+ gem.summary = %Q{Memoize like object, with dirty setters}
9
+ gem.description = %Q{Works like Memoize [http://raa.ruby-lang.org/project/memoize/http://raa.ruby-lang.org/project/memoize/], but with a global cache. Thats allows to delete all cache when you use #clean() or call one of the methods specified on #dirty_writer }
10
+ gem.email = "clbustos@gmail.com"
11
+ gem.homepage = "http://github.com/clbustos/dirty-memoize"
12
+ gem.authors = ["Claudio Bustos"]
13
+ gem.add_development_dependency 'rspec'
14
+ gem.add_development_dependency 'jeweler'
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :spec => :check_dependencies
41
+
42
+ task :default => :spec
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "dirty-memoize #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,84 @@
1
+ # Like Memoize, but designed for mutable and parametizable objects
2
+ #
3
+ # Use when:
4
+ # 1. You have one expensive method (\#compute) which set many internal
5
+ # variables. So, is preferable lazy evaluation of these dependent variables.
6
+ # 2. The expensive operation depends on one or more parameters
7
+ # 3. Changes on one or more parameters affect all dependent variables
8
+ # 4. You may want to hide the call of 'compute' operation
9
+ # 5. The user could want test several different parameters values
10
+ #
11
+ # By default, the method to compute should be called \#compute.
12
+ # Set constant DIRTY_COMPUTE to the name of other method if you need it
13
+ #
14
+ # Example:
15
+ # class ExpensiveCalculation
16
+ # extend DirtyMemoize
17
+ # attr_accessor :y, :z
18
+ # def initialize(y=nil,z=nil)
19
+ # @y=y
20
+ # @z=z
21
+ # def compute
22
+ # @a=@y*1000+@z*1000
23
+ # end
24
+ # def a
25
+ # @a.nil? nil : "This is the value: #{@a}"
26
+ # end
27
+ #
28
+ # end
29
+ # puts ExpensiveCalculation.new(1,2).a
30
+
31
+ module DirtyMemoize
32
+ # Trick from http://github.com/ecomba/memoizable
33
+ def self.included(receiver) #:nodoc:
34
+ receiver.extend DirtyMemoize::ClassMethods
35
+ end
36
+ module ClassMethods
37
+ # Set variables which
38
+ def dirty_writer(*independent)
39
+ independent.each do |sym|
40
+ sym=sym.to_s+"="
41
+ alias_method((sym.to_s+"_whitout_dirty").intern, sym)
42
+ define_method(sym) do |*args|
43
+ @dirty=true
44
+ send(sym.to_s+"_whitout_dirty", *args)
45
+ end
46
+ end
47
+ end
48
+
49
+ def dirty_memoize(*dependent)
50
+ dependent.each do |sym|
51
+ alias_method((sym.to_s+"_without_dirty").intern, sym)
52
+ define_method(sym) {|*args|
53
+ if(dirty?)
54
+ clean_cache
55
+ if self.class.const_defined? "DIRTY_COMPUTE"
56
+ send(self.class.const_get("DIRTY_COMPUTE"))
57
+ else
58
+ send(:compute)
59
+ end
60
+ @compute_count||=0
61
+ @compute_count+=1
62
+ @dirty=false
63
+ end
64
+ @cache[sym]||=Hash.new
65
+ @cache[sym][args]||=send(sym.to_s+"_without_dirty", *args)
66
+ }
67
+ end
68
+ end
69
+ end # end of ClassMethods
70
+ # Is the object dirty?
71
+ def dirty?
72
+ @dirty=true if @dirty.nil?
73
+ @dirty
74
+ end
75
+ # Number of compute's runs
76
+ def compute_count
77
+ @compute_count||=0
78
+ end
79
+ def clean_cache
80
+ @cache=Hash.new
81
+ @dirty=true
82
+ end
83
+
84
+ end
@@ -0,0 +1,133 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class ExpensiveClass
4
+ attr_writer :x, :y
5
+ attr_reader :count
6
+ include DirtyMemoize
7
+ def initialize
8
+ @a=nil
9
+ @b=nil
10
+ @x='x'
11
+ @y='y'
12
+ @compute_called=false
13
+ end
14
+ def set_a(aa)
15
+ @a=aa
16
+ end
17
+ def compute_called?
18
+ @compute_called
19
+ end
20
+ def compute
21
+ @a=@x
22
+ @b=@y
23
+ @compute_called=true
24
+ end
25
+ def a
26
+ "@a=#{@a}"
27
+ end
28
+ def b
29
+ "@b=#{@b}"
30
+ end
31
+ dirty_writer :x, :y
32
+ dirty_memoize :a, :b
33
+ end
34
+
35
+ class ExpensiveClass2 < ExpensiveClass
36
+ DIRTY_COMPUTE=:compute2
37
+ def compute2
38
+ @a=@x+".2"
39
+ end
40
+ end
41
+ describe DirtyMemoize, "extended object" do
42
+ before(:each) do
43
+ @ec=ExpensiveClass.new
44
+ end
45
+ it "should initialize with dirty? to true" do
46
+ @ec.dirty?.should==true
47
+ end
48
+ it "should initialize with number of computation to 0" do
49
+ @ec.compute_count.should==0
50
+ end
51
+ describe "reads 'dirty' attributes " do
52
+ before(:each) do
53
+ @ec.a
54
+ end
55
+ it "#compute is called" do
56
+ @ec.compute_called?.should==true
57
+ end
58
+ it 'compute_count set to 1' do
59
+ @ec.compute_count.should==1
60
+ end
61
+ it 'dirty? set to false' do
62
+ @ec.dirty?.should==false
63
+ end
64
+ it "compute_count doesn't change with multiple calls" do
65
+ 5.times {@ec.a}
66
+ @ec.compute_count.should==1
67
+ end
68
+ end
69
+ describe "calls dirty writers before dirty getter" do
70
+ before(:each) do
71
+ @ec.x="cache"
72
+ end
73
+ it 'set dirty? to true' do
74
+ @ec.dirty?.should==true
75
+ end
76
+ it "doesn't call compute" do
77
+ @ec.compute_called?.should==false
78
+ end
79
+ it "doesn't change dirty getters" do
80
+ @ec.instance_variable_get("@a").nil?.should==true
81
+ end
82
+ end
83
+
84
+ describe "calls dirty getter after call dirty writer" do
85
+ before(:each) do
86
+ @ec.x="cache"
87
+ @ec.a
88
+ end
89
+ it 'set dirty? to false' do
90
+ @ec.dirty?.should==false
91
+ end
92
+ it "calls compute, only once" do
93
+ @ec.compute_called?.should==true
94
+ @ec.compute_count.should==1
95
+ end
96
+ it "set value or internal variable" do
97
+ @ec.instance_variable_get("@a").should=='cache'
98
+ end
99
+ it 'set getter method with a different value' do
100
+ @ec.a.should=='@a=cache'
101
+ end
102
+ end
103
+ describe "uses cache" do
104
+ before(:each) do
105
+ @ec.x='cache'
106
+ @ec.a
107
+ @ec.set_a('not_cache')
108
+ end
109
+ it "so changing internal variables doesn't produce external changes" do
110
+ @ec.instance_variable_get("@a").should=='not_cache'
111
+ @ec.compute_count.should==1
112
+ @ec.a.should=='@a=cache'
113
+ end
114
+ it "so deleting it implies calculate all again" do
115
+ @ec.dirty?.should==false
116
+ @ec.clean_cache
117
+ @ec.dirty?.should==true
118
+ @ec.compute_count.should==1
119
+ @ec.a.should=='@a=cache'
120
+ @ec.compute_count.should==2
121
+
122
+ end
123
+ end
124
+ describe "could call other computation method" do
125
+ it "using DIRTY_COMPUTER" do
126
+ @ec2=ExpensiveClass2.new
127
+ @ec2.x='cache'
128
+ @ec2.a.should=='@a=cache.2'
129
+ @ec2.compute_count.should==1
130
+
131
+ end
132
+ end
133
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ -f s
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'dirty-memoize.rb'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
10
+
11
+ class String
12
+ def deindent
13
+ gsub /^[ \t]*/, ''
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dirty-memoize
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Claudio Bustos
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-01 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: jeweler
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ description: "Works like Memoize [http://raa.ruby-lang.org/project/memoize/http://raa.ruby-lang.org/project/memoize/], but with a global cache. Thats allows to delete all cache when you use #clean() or call one of the methods specified on #dirty_writer "
45
+ email: clbustos@gmail.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README.rdoc
53
+ files:
54
+ - .document
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - VERSION
60
+ - lib/dirty-memoize.rb
61
+ - spec/dirty_memoize_spec.rb
62
+ - spec/spec.opts
63
+ - spec/spec_helper.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/clbustos/dirty-memoize
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --charset=UTF-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.6
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Memoize like object, with dirty setters
94
+ test_files:
95
+ - spec/spec_helper.rb
96
+ - spec/dirty_memoize_spec.rb