mixin 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/metable.rb +1 -4
- data/lib/metable/class_methods.rb +21 -35
- data/lib/mixin.rb +2 -2
- metadata +2 -2
data/lib/metable.rb
CHANGED
@@ -45,10 +45,7 @@ require 'mixin'
|
|
45
45
|
# * define_singleton_method <em>(aliased as define_class_method, define_public_class_method, define_public_singleton_method)</em>
|
46
46
|
# * define_private_singleton_method <em>(aliased as define_private_class_method)</em>
|
47
47
|
# * define_protected_singleton_method <em>(aliased as define_protected_class_method)</em>
|
48
|
-
# *
|
49
|
-
# * private_enclosure
|
50
|
-
# * protected_enclosure
|
51
|
-
# * public_enclosure
|
48
|
+
# * lazy_attr
|
52
49
|
#
|
53
50
|
module Metable
|
54
51
|
include Metable::InstanceMethods
|
@@ -44,54 +44,40 @@ module Metable
|
|
44
44
|
alias define_private_class_method define_private_singleton_method
|
45
45
|
|
46
46
|
|
47
|
-
# Defines an instance method for _name_ which
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
47
|
+
# Defines an instance method for _name_ which evaluates the _thunk_ the
|
48
|
+
# first time it is invoked on a given instance and returns the resulting
|
49
|
+
# object, and then returns that same object on each subsequent call. The
|
50
|
+
# _thunk_ block is evaluated within the context of the invoking instance
|
51
|
+
# object using +instance_eval+.
|
52
|
+
#
|
53
|
+
# The _access_ argument can be a symbol or string representing the access
|
54
|
+
# level for the defined method (public, private, or protected). The default
|
55
|
+
# access level is +private+.
|
54
56
|
#
|
55
57
|
# class Foo
|
56
58
|
# include Metable
|
57
|
-
#
|
59
|
+
# lazy_attr(:time_of_evaluation, :public) { Time.now }
|
58
60
|
# end
|
59
61
|
#
|
60
62
|
# foo = Foo.new
|
61
63
|
# sleep 7
|
62
|
-
# Time.now
|
63
|
-
# foo.
|
64
|
+
# Time.now -> Fri Sep 05 16:20:00 -0700 2008
|
65
|
+
# foo.time_of_evaluation -> Fri Sep 05 16:20:00 -0700 2008
|
64
66
|
# sleep 86407
|
65
|
-
# foo.
|
66
|
-
# foo.instance_variables
|
67
|
+
# foo.time_of_evaluation -> Fri Sep 05 16:20:00 -0700 2008
|
68
|
+
# foo.instance_variables -> []
|
67
69
|
#
|
68
|
-
def
|
70
|
+
def lazy_attr(name, access = :private, &thunk) # :doc:
|
69
71
|
define_method name do
|
70
|
-
|
71
|
-
eigen_eval
|
72
|
-
|
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
|
73
78
|
end
|
74
79
|
__send__ access, name
|
75
80
|
end
|
76
81
|
|
77
|
-
|
78
|
-
# Equivalent to calling "<tt>enclosed_attr _name_, :public, <em>&block</em></tt>"
|
79
|
-
# on each _name_ in turn.
|
80
|
-
def public_enclosure(*names, &block) # :doc:
|
81
|
-
names.each { |name| enclosed_attr name, :public, &block }
|
82
|
-
end
|
83
|
-
|
84
|
-
# Equivalent to calling "<tt>enclosed_attr _name_, :private, <em>&block</em></tt>"
|
85
|
-
# on each _name_ in turn.
|
86
|
-
def private_enclosure(*names, &block) # :doc:
|
87
|
-
names.each { |name| enclosed_attr name, :private, &block }
|
88
|
-
end
|
89
|
-
|
90
|
-
# Equivalent to calling "<tt>enclosed_attr _name_, :protected, <em>&block</em></tt>"
|
91
|
-
# on each _name_ in turn.
|
92
|
-
def protected_enclosure(*names, &block) # :doc:
|
93
|
-
names.each { |name| enclosed_attr name, :protected, &block }
|
94
|
-
end
|
95
|
-
|
96
82
|
end
|
97
83
|
end
|
data/lib/mixin.rb
CHANGED
@@ -23,8 +23,8 @@ module Mixin
|
|
23
23
|
end
|
24
24
|
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
lazy_attr(:__class_mixin__) { SingletonMixin.new }
|
27
|
+
lazy_attr(:__module_mixin__) { ModuleMixin.new(self) }
|
28
28
|
|
29
29
|
|
30
30
|
# Extends the given modules to the classes that subsequently include the
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hersch Stevenson (xian)
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-10-11 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|