- class_method_defined?
- lazy_attr
- private_class_method_defined?
- protected_class_method_defined?
- public_class_method_defined?
Returns true if the named class method is defined by the invoking class (or its extended modules or ancestors). Public and protected (yes, protected) class methods are matched.
[ show source ]
# File lib/metable/class_methods.rb, line 11
11: def class_method_defined?(method_name)
12: eigenclass.method_defined? method_name
13: end
Returns true if the named private class method is defined by the invoking class (or its extended modules or ancestors).
[ show source ]
# File lib/metable/class_methods.rb, line 29
29: def private_class_method_defined?(method_name)
30: eigenclass.private_method_defined? method_name
31: end
Returns true if the named protected class method is defined by the invoking class (or its extended modules or ancestors).
[ show source ]
# File lib/metable/class_methods.rb, line 23
23: def protected_class_method_defined?(method_name)
24: eigenclass.protected_method_defined? method_name
25: end
Returns true if the named public class method is defined by the invoking class (or its extended modules or ancestors).
[ show source ]
# File lib/metable/class_methods.rb, line 17
17: def public_class_method_defined?(method_name)
18: eigenclass.public_method_defined? method_name
19: end
Defines an instance method for name which evaluates the thunk the first time it is invoked on a given instance and returns the resulting object, and then returns that same object on each subsequent call. The thunk block is evaluated within the context of the invoking instance object using instance_eval.
The access argument can be a symbol or string representing the access level for the defined method (public, private, or protected). The default access level is private.
class Foo
include Metable
lazy_attr(:time_of_evaluation, :public) { Time.now }
end
foo = Foo.new
sleep 7
Time.now -> Fri Sep 05 16:20:00 -0700 2008
foo.time_of_evaluation -> Fri Sep 05 16:20:00 -0700 2008
sleep 86407
foo.time_of_evaluation -> Fri Sep 05 16:20:00 -0700 2008
foo.instance_variables -> []
[ show source ]
# File lib/metable/class_methods.rb, line 70
70: def lazy_attr(name, access = :private, &thunk) # :doc:
71: define_method name do
72: result = instance_eval &thunk
73: eigen_eval do
74: method_defined?(name) && undef_method(name)
75: define_method(name) { result } && __send__(access, name)
76: end
77: result
78: end
79: __send__ access, name
80: end