Class: Object

Inherits:
BasicObject
Defined in:
lib/call-me/named.rb,
lib/call-me/memoize.rb

Instance Method Summary collapse

Instance Method Details

#is_memoized?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/call-me/memoize.rb', line 32

def is_memoized? (name)
	respond_to? "__memoize_#{name}"
end

#memoize(name = nil) ⇒ Object

Memoize the method name.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/call-me/memoize.rb', line 37

def memoize (name = nil)
	return if @__to_memoize__ = !name

	to_call = "__memoize_#{name}"

	begin; if instance_method(name).arity == 0
		refine_method name, :prefix => '__memoize' do
			(memoize_cache[name][nil] ||= [__send__(to_call)])[0]
		end

		return
	end; rescue; end

	refine_method name, :prefix => '__memoize' do |*args|
		if tmp = memoize_cache[name][args]
			tmp
		else
			memoize_cache[name][__memoize_try_to_clone__(args)] = [__send__(*([to_call] + args))]
		end[0]
	end

	nil
end

#memoize_cacheObject

Get the memoization cache



96
97
98
# File 'lib/call-me/memoize.rb', line 96

def memoize_cache
	@__memoize_cache__ ||= Hash.new { |h, k| h[k] = {} }
end

#memoize_clear(name = nil) ⇒ Object

Clear the memoize cache completely or only for the method name



87
88
89
90
91
92
93
# File 'lib/call-me/memoize.rb', line 87

def memoize_clear (name = nil)
	if name
		memoize_cache.delete(name.to_sym)
	else
		memoize_cache.clear
	end
end

#named(*args) ⇒ Object

Raises:

  • (ArgumentError)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/call-me/named.rb', line 183

def named (*args)
	raise ArgumentError, 'you have to pass at least one argument' if args.length == 0

	if args.first.nil?
		if @__named_last_method__
			names, options = Named.definition(instance_method(@__named_last_method__))

			named(Named.new(@__named_last_method__), *(names + [options]))
		end; true
	elsif !args.first.is_a?(Named)
		@__to_namedify__ = args
	end and return

	@__to_namedify__ = false

	method, names, options = Named.normalize(*args)

	instance_method(method).tap {|m|
		raise ArgumentError, 'method arity mismatch' if m.arity > 0 && m.arity != names.length
	}

	to_call = "__named_#{method}"

	refine_method method, :prefix => '__named' do |*args, &block|
		__send__ *([to_call] + Named.arguments(names, options, *args)), &block
	end

	nil
end

#singleton_memoize(name = nil) ⇒ Object

Memoize the singleton method name.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/call-me/memoize.rb', line 62

def singleton_memoize (name = nil)
	return if @__to_singleton_memoize__ = !name

	to_call = "__memoize_#{name}"

	begin; if method(name).arity == 0
		refine_singleton_method name, :prefix => '__memoize' do
			(memoize_cache[name][nil] ||= [__send__(to_call)])[0]
		end

		return
	end; rescue; end

	refine_singleton_method name, :prefix => '__memoize' do |*args, &block|
		if tmp = memoize_cache[name][args]
			tmp
		else
			memoize_cache[name][__memoize_try_to_clone__(args)] = [__send__(*([to_call] + args))]
		end[0]
	end

	nil
end

#singleton_named(*args) ⇒ Object

Raises:

  • (ArgumentError)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/call-me/named.rb', line 213

def singleton_named (*args)
	raise ArgumentError, 'you have to pass at least one argument' if args.length == 0

	if args.first.nil?
		if @__singleton_named_last_method__
			names, options = Named.definition(method(@__singleton_named_last_method__))

			singleton_named(Named.new(@__singleton_named_last_method__), *(names + [options]))
		end; true
	elsif !args.first.is_a?(Named)
		@__to_singleton_namedify__ = args
	end and return

	@__to_singleton_namedify__ = false

	method, names, options = Named.normalize(*args)

	method(method).tap {|m|
		raise ArgumentError, 'method arity mismatch' if m.arity > 0 && m.arity != names.length
	}

	to_call = "__named_#{method}"

	refine_singleton_method method, :prefix => '__named' do |*args, &block|
		__send__ *([to_call] + Named.arguments(names, options, *args)), &block
	end

	nil
end