Module: Cache

Defined in:
lib/RubyExt/Cache.rb

Constant Summary collapse

DISABLED =
false

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.monitorObject (readonly)

Returns the value of attribute monitor.



76
77
78
# File 'lib/RubyExt/Cache.rb', line 76

def monitor
  @monitor
end

Class Method Details

.alias_counterObject



19
20
21
22
# File 'lib/RubyExt/Cache.rb', line 19

def alias_counter
	@alias_counter += 1
	return :"m#{@alias_counter}"
end

.cached(*arg) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/RubyExt/Cache.rb', line 24

def cached *arg
	vnames, klass, methods = parse_and_check_arguments *arg
	
	return if DISABLED						
	methods.each do |m|				
		als = (m.to_s =~ /^[_a-zA-Z0-9]+$/) ? m : RubyExt::Cache.alias_counter.to_sym 
		
		klass.class_eval{alias_method :"cached_#{als}", :"#{m}"}
		unless vnames.is_a? Array
			script = Cache["single_version_without_args.txt"].substitute binding					
			@versions[vnames] = 0 unless @versions.include? vnames
		else
			vnames_str = vnames.collect{|vname| "'#{vname}' => nil"}.join(', ')
			script = Cache["multiple_version_without_args.txt"].substitute binding
			vnames.each{|vname| @versions[vname] = 0 unless @versions.include? vname}
		end
		klass.class_eval script, __FILE__, __LINE__								
	end			
end

.cached_with_params(*arg) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/RubyExt/Cache.rb', line 44

def cached_with_params *arg
	vnames, klass, methods = parse_and_check_arguments *arg
	
	return if DISABLED			
	methods.each do |m|
		als = (m.to_s =~ /^[_a-zA-Z0-9]+$/) ? m : RubyExt::Cache.alias_counter
		
		klass.class_eval{alias_method :"cached_#{als}", :"#{m}"}
		unless vnames.is_a? Array
			script = Cache["single_version_with_args.txt"].substitute binding					
			@versions[vnames] = 0 unless @versions.include? vnames
		else
			vnames_str = vnames.collect{|vname| "'#{vname}' => nil"}.join(', ')
			script = Cache["multiple_version_with_args.txt"].substitute binding
			vnames.each{|vname| @versions[vname] = 0 unless @versions.include? vname}
		end
		klass.class_eval script, __FILE__, __LINE__
	end	
end

.update(*names) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/RubyExt/Cache.rb', line 68

def update *names
	names.each do |n| 
		n = n.to_s
		@versions[n] = 0 unless @versions.include? n 
		@versions[n] += 1
	end
end

.version(name) ⇒ Object



64
65
66
# File 'lib/RubyExt/Cache.rb', line 64

def version name
	@versions[name]
end