-
Notifications
You must be signed in to change notification settings - Fork 13
/
module_methods.rb
66 lines (58 loc) · 1.39 KB
/
module_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# encoding: utf-8
module Adamantium
# Methods mixed in to adamantium modules
module ModuleMethods
# Return default deep freezer
#
# @return [Freezer::Deep]
#
# @api private
def freezer
Freezer::Deep
end
# Memoize a list of methods
#
# @example
# memoize :hash
#
# @param [Array<#to_s>] methods
# a list of methods to memoize
#
# @return [self]
#
# @api public
def memoize(*methods)
options = methods.last.kind_of?(Hash) ? methods.pop : {}
method_freezer = Freezer.parse(options) || freezer
methods.each { |method| memoize_method(method, method_freezer) }
self
end
private
# Hook called when module is included
#
# @param [Module] descendant
# the module including ModuleMethods
#
# @return [undefined]
#
# @api private
def included(descendant)
super
descendant.module_eval { include Adamantium }
end
# Memoize the named method
#
# @param [Symbol] method_name
# a method name to memoize
# @param [#call] freezer
# a callable object to freeze the value
#
# @return [undefined]
#
# @api private
def memoize_method(method_name, freezer)
memoized_methods[method_name] = Memoizable::MethodBuilder
.new(self, method_name, freezer).call
end
end # ModuleMethods
end # Adamantium