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.
@@ -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
- # * enclosed_attr
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 becomes an enclosure around
48
- # a given object. The enclosed object is the result of the given block,
49
- # which is evaluated within the context of the invoking instance object
50
- # using +instance_eval+ at the time in which the enclosure method is first
51
- # invoked on that instance. The _access_ argument can be a symbol or string
52
- # representing the access level for the enclosure method (public, private,
53
- # or protected). The default access level is +private+.
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
- # enclosed_attr(:time_of_enclosure, :public) { Time.now }
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 -> Fri Sep 05 16:20:00 -0700 2008
63
- # foo.time_of_enclosure -> Fri Sep 05 16:20:00 -0700 2008
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.time_of_enclosure -> Fri Sep 05 16:20:00 -0700 2008
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 enclosed_attr(name, access = :private, &block) # :doc:
70
+ def lazy_attr(name, access = :private, &thunk) # :doc:
69
71
  define_method name do
70
- ea = instance_eval &block
71
- eigen_eval { define_method(name) { ea } && __send__(access, name) }
72
- ea
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
@@ -23,8 +23,8 @@ module Mixin
23
23
  end
24
24
 
25
25
 
26
- enclosed_attr(:__class_mixin__) { SingletonMixin.new }
27
- enclosed_attr(:__module_mixin__) { ModuleMixin.new(self) }
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.0
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-09-09 00:00:00 -07:00
12
+ date: 2008-10-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15