Module: Uinit::Memoizable::ClassMethods
- Defined in:
- lib/uinit/memoizable.rb
Instance Method Summary collapse
- #memo(method_sym) ⇒ Object
- #memo_self(method_sym) ⇒ Object
- #memo_unset(method_sym) ⇒ Object
- #memo_variable_name(method_sym) ⇒ Object
Instance Method Details
#memo(method_sym) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/uinit/memoizable.rb', line 12 def memo(method_sym) method = instance_method(method_sym) ivar = memo_variable_name(method_sym) define_method(method_sym) do |*args, **kwargs| return instance_variable_get(ivar) if instance_variable_defined?(ivar) instance_variable_set(ivar, method.bind_call(self, *args, **kwargs)) end end |
#memo_self(method_sym) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/uinit/memoizable.rb', line 23 def memo_self(method_sym) method = singleton_class.instance_method(method_sym) ivar = memo_variable_name(method_sym) define_singleton_method(method_sym) do |*args, **kwargs| return singleton_class.instance_variable_get(ivar) if singleton_class.instance_variable_defined?(ivar) singleton_class.instance_variable_set(ivar, method.bind_call(self, *args, **kwargs)) end end |
#memo_unset(method_sym) ⇒ Object
34 35 36 |
# File 'lib/uinit/memoizable.rb', line 34 def memo_unset(method_sym) singleton_class.remove_instance_variable(memo_variable_name(method_sym)) end |
#memo_variable_name(method_sym) ⇒ Object
38 39 40 41 42 |
# File 'lib/uinit/memoizable.rb', line 38 def memo_variable_name(method_sym) predicate = method_sym.to_s.end_with?('?') :"@#{predicate ? '__memois_' : '__memo_'}#{method_sym.to_s.tr('?', '_')}" end |