module-cluster 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,17 @@ Adds methods for defining module clusters using #included and #extended.
8
8
 
9
9
  # Summary #
10
10
 
11
- Provides :define_module_included, :define_module_extended, and :define_module_cluster.
11
+ Provides:
12
+
13
+ * :define_module_included
14
+ * :define_module_extended
15
+ * :define_module_cluster
16
+ * :define_module_included_cascades
17
+ * :define_module_extended_cascades
18
+ * :define_module_cluster_cascades
19
+ * :define_module_included_cascades_to_class
20
+ * :define_module_extended_cascades_to_class
21
+ * :define_module_cluster_cascades_to_class
12
22
 
13
23
  # Install #
14
24
 
@@ -24,6 +34,16 @@ module SomeModule
24
34
  end
25
35
  ```
26
36
 
37
+ ## The Basic Idea ##
38
+
39
+ We use blocks to define cascading module includes/extends. This automates calls to super (so we don't forget) and puts self.included/self.extended support into a module so that local overriding is still possible.
40
+
41
+ The block provides arrays to hold includes/extends. If two arrays [ includes, extends ] are returned by the block, they will be used. Otherwise the arrays passed in by parameters will be used. This means, due to how Ruby handles references, that *if you set one of the parameter arrays to a new array you have to return both arrays* or your changes won't be added.
42
+
43
+ Calls to define_module_... functions are cumulative, permitting multiple calls (whether subsequently or otherwise). This is particularly useful for configuring including/extending modules from the included/extended module (and so on - nested blocks permit this sort of determination indefinitely).
44
+
45
+ ## define_module_included ##
46
+
27
47
  To cause modules to be included when this module is included:
28
48
 
29
49
  ```ruby
@@ -44,7 +64,9 @@ module SomeModule
44
64
  end
45
65
  ```
46
66
 
47
- or when this module is extended
67
+ ## define_module_extended ##
68
+
69
+ To cause modules to be included when this module is extended:
48
70
 
49
71
  ```ruby
50
72
  module SomeModule
@@ -54,7 +76,7 @@ module SomeModule
54
76
  end
55
77
  ```
56
78
 
57
- or
79
+ or extended when this module is extended:
58
80
 
59
81
  ```ruby
60
82
  module SomeModule
@@ -64,7 +86,9 @@ module SomeModule
64
86
  end
65
87
  ```
66
88
 
67
- or when this module is either included or extended:
89
+ ## define_module_cluster ##
90
+
91
+ To cause modules to be included when this module is included or extended:
68
92
 
69
93
  ```ruby
70
94
  module SomeModule
@@ -74,7 +98,7 @@ module SomeModule
74
98
  end
75
99
  ```
76
100
 
77
- or
101
+ or extended when this module is included or extended:
78
102
 
79
103
  ```ruby
80
104
  module SomeModule
@@ -84,7 +108,83 @@ module SomeModule
84
108
  end
85
109
  ```
86
110
 
87
- If the block returns [ includes, extends ] as arrays, those values will be used; otherwise, the contents of the includes and extends parameters passed to the block will be used.
111
+ ## define_module_included_cascades ##
112
+
113
+ To cause modules to be included or extended when this module is used to include in a class or module.
114
+
115
+ ```ruby
116
+ module SomeModule
117
+ define_module_included_cascades do |includes, extends|
118
+ includes.concat [ SomeOtherModule ]
119
+ extends.concat [ SomeOtherModule ]
120
+ end
121
+ end
122
+ ```
123
+
124
+ ## define_module_extended_cascades ##
125
+
126
+ To cause modules to be included or extended when this module is used to extend in a class or module:
127
+
128
+ ```ruby
129
+ module SomeModule
130
+ define_module_extended_cascades do |includes, extends|
131
+ includes.concat [ SomeOtherModule ]
132
+ extends.concat [ SomeOtherModule ]
133
+ end
134
+ end
135
+ ```
136
+
137
+ ## define_module_cluster_cascades ##
138
+
139
+ To cause modules to be included or extended when this module is used to include or extend in a class or module:
140
+
141
+ ```ruby
142
+ module SomeModule
143
+ define_module_cluster_cascades do |includes, extends|
144
+ includes.concat [ SomeOtherModule ]
145
+ extends.concat [ SomeOtherModule ]
146
+ end
147
+ end
148
+ ```
149
+
150
+ ## define_module_included_cascades_to_class ##
151
+
152
+ To cause modules to be included or extended when this module is used to include in a class (and not a module):
153
+
154
+ ```ruby
155
+ module SomeModule
156
+ define_module_included_cascades_to_class do |includes, extends|
157
+ includes.concat [ SomeOtherModule ]
158
+ extends.concat [ SomeOtherModule ]
159
+ end
160
+ end
161
+ ```
162
+
163
+ ## define_module_extended_cascades_to_class ##
164
+
165
+ To cause modules to be included or extended when this module is used to extend in a class (and not a module):
166
+
167
+ ```ruby
168
+ module SomeModule
169
+ define_module_extended_cascades_to_class do |includes, extends|
170
+ includes.concat [ SomeOtherModule ]
171
+ extends.concat [ SomeOtherModule ]
172
+ end
173
+ end
174
+ ```
175
+
176
+ ## define_module_cluster_cascades_to_class ##
177
+
178
+ To cause modules to be included or extended when this module is used to include or extend in a class (and not a module):
179
+
180
+ ```ruby
181
+ module SomeModule
182
+ define_module_cluster_cascades_to_class do |includes, extends|
183
+ includes.concat [ SomeOtherModule ]
184
+ extends.concat [ SomeOtherModule ]
185
+ end
186
+ end
187
+ ```
88
188
 
89
189
  # License #
90
190
 
data/README.rdoc CHANGED
@@ -0,0 +1,182 @@
1
+ == Module Cluster
2
+
3
+ http://rubygems.org/gems/module-cluster
4
+
5
+ == Description
6
+
7
+ Adds methods for defining module clusters using #included and #extended.
8
+
9
+ == Summary
10
+
11
+ Provides:
12
+ * :define_module_included
13
+ * :define_module_extended
14
+ * :define_module_cluster
15
+ * :define_module_included_cascades_to_class
16
+ * :define_module_extended_cascades_to_class
17
+ * :define_module_cluster_cascades_to_class
18
+
19
+ == Install
20
+
21
+ * sudo gem install module-cluster
22
+
23
+ == Usage
24
+
25
+ Simply:
26
+
27
+ module SomeModule
28
+ include ModuleCluster
29
+ end
30
+
31
+ === The Basic Idea
32
+
33
+ We use blocks to define cascading module includes/extends. This automates calls to super (so we don't forget) and puts self.included/self.extended support into a module so that local overriding is still possible.
34
+
35
+ The block provides arrays with the current set of includes/extends to cascade for the module. If two arrays [ includes, extends ] are returned by the block, they will be used. Otherwise the arrays passed in by parameters will be used. This means, due to how Ruby handles references, that *if you set one of the parameter arrays to a new array you have to return both arrays* or your changes won't be added.
36
+
37
+ Calls to define_module_... functions are cumulative, permitting multiple calls (whether subsequently or otherwise). This is particularly useful for configuring including/extending modules from the included/extended module (and so on - nested blocks permit this sort of determination indefinitely).
38
+
39
+ === define_module_included
40
+
41
+ To cause modules to be included when this module is included:
42
+
43
+ module SomeModule
44
+ define_module_included do |includes, extends|
45
+ includes.concat [ SomeOtherModule ]
46
+ end
47
+ end
48
+
49
+ or extended when this module is included:
50
+
51
+ module SomeModule
52
+ define_module_included do |includes, extends|
53
+ extends.concat [ SomeOtherModule ]
54
+ end
55
+ end
56
+
57
+ === define_module_extended
58
+
59
+ To cause modules to be included when this module is extended:
60
+
61
+ module SomeModule
62
+ define_module_extended do |includes, extends|
63
+ includes.concat [ SomeOtherModule ]
64
+ end
65
+ end
66
+
67
+ or extended when this module is extended:
68
+
69
+ module SomeModule
70
+ define_module_extended do |includes, extends|
71
+ extends.concat [ SomeOtherModule ]
72
+ end
73
+ end
74
+
75
+ === define_module_cluster
76
+
77
+ To cause modules to be included when this module is included or extended:
78
+
79
+ module SomeModule
80
+ define_module_cluster do |includes, extends|
81
+ includes.concat [ SomeOtherModule ]
82
+ end
83
+ end
84
+
85
+ or extended when this module is included or extended:
86
+
87
+ module SomeModule
88
+ define_module_cluster do |includes, extends|
89
+ extends.concat [ SomeOtherModule ]
90
+ end
91
+ end
92
+
93
+ === define_module_included_cascades
94
+
95
+ To cause modules to be included or extended when this module is used to include in a class or module.
96
+
97
+ module SomeModule
98
+ define_module_included_cascades do |includes, extends|
99
+ includes.concat [ SomeOtherModule ]
100
+ extends.concat [ SomeOtherModule ]
101
+ end
102
+ end
103
+
104
+ === define_module_extended_cascades
105
+
106
+ To cause modules to be included or extended when this module is used to extend in a class or module:
107
+
108
+ module SomeModule
109
+ define_module_extended_cascades do |includes, extends|
110
+ includes.concat [ SomeOtherModule ]
111
+ extends.concat [ SomeOtherModule ]
112
+ end
113
+ end
114
+
115
+ === define_module_cluster_cascades
116
+
117
+ To cause modules to be included or extended when this module is used to include or extend in a class or module:
118
+
119
+ module SomeModule
120
+ define_module_cluster_cascades do |includes, extends|
121
+ includes.concat [ SomeOtherModule ]
122
+ extends.concat [ SomeOtherModule ]
123
+ end
124
+ end
125
+
126
+ === define_module_included_cascades_to_class
127
+
128
+ To cause modules to be included or extended when this module is used to include a class (and not a module):
129
+
130
+ module SomeModule
131
+ define_module_included_cascades_to_class do |includes, extends|
132
+ includes.concat [ SomeOtherModule ]
133
+ extends.concat [ SomeOtherModule ]
134
+ end
135
+ end
136
+
137
+ === define_module_extended_cascades_to_class
138
+
139
+ To cause modules to be included or extended when this module is used to extend a class (and not a module):
140
+
141
+ module SomeModule
142
+ define_module_extended_cascades_to_class do |includes, extends|
143
+ includes.concat [ SomeOtherModule ]
144
+ extends.concat [ SomeOtherModule ]
145
+ end
146
+ end
147
+
148
+ === define_module_cluster_cascades_to_class
149
+
150
+ To cause modules to be included or extended when this module is used to include or extend a class (and not a module):
151
+
152
+ module SomeModule
153
+ define_module_cluster_cascades_to_class do |includes, extends|
154
+ includes.concat [ SomeOtherModule ]
155
+ extends.concat [ SomeOtherModule ]
156
+ end
157
+ end
158
+
159
+ == License
160
+
161
+ (The MIT License)
162
+
163
+ Copyright (c) 2011 Asher
164
+
165
+ Permission is hereby granted, free of charge, to any person obtaining
166
+ a copy of this software and associated documentation files (the
167
+ 'Software'), to deal in the Software without restriction, including
168
+ without limitation the rights to use, copy, modify, merge, publish,
169
+ distribute, sublicense, and/or sell copies of the Software, and to
170
+ permit persons to whom the Software is furnished to do so, subject to
171
+ the following conditions:
172
+
173
+ The above copyright notice and this permission notice shall be
174
+ included in all copies or substantial portions of the Software.
175
+
176
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
177
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
178
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
179
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
180
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
181
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
182
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -6,8 +6,16 @@ module ModuleCluster
6
6
  end
7
7
  end
8
8
 
9
+ class Array::ClassOrModule < Array
10
+ end
11
+ class Array::CascadeToClass < Array
12
+ end
13
+ class Array::CascadeToClassOrModule < Array
14
+ end
15
+
9
16
  require_relative 'module-cluster/ModuleCluster.rb'
10
17
  require_relative 'module-cluster/_private_/ModuleCluster.rb'
11
18
  require_relative 'module-cluster/ModuleCluster/IncludeExtendSupport.rb'
19
+ require_relative 'module-cluster/ModuleCluster/_private_/IncludeExtendSupport.rb'
12
20
  require_relative 'module-cluster/ModuleCluster/IncludeSupport.rb'
13
21
  require_relative 'module-cluster/ModuleCluster/ExtendSupport.rb'
@@ -1,26 +1,26 @@
1
1
 
2
2
  module ModuleCluster
3
3
 
4
- #############################
4
+ ############################
5
5
  # define_module_included #
6
- #############################
6
+ ############################
7
7
 
8
- def define_module_included( & block )
9
- inclusion_array, extension_array = yield_for_includes_extends( block )
10
- ModuleCluster::IncludeSupport.set_includes_for_class_or_module( self, inclusion_array )
11
- ModuleCluster::IncludeSupport.set_extends_for_class_or_module( self, extension_array )
8
+ def define_module_included( & include_extend_block )
9
+ inclusion_array, extension_array = yield_for_includes_extends( Array::ClassOrModule.new, Array::ClassOrModule.new, include_extend_block )
10
+ ModuleCluster::IncludeSupport.add_includes_for_class_or_module( self, inclusion_array )
11
+ ModuleCluster::IncludeSupport.add_extends_for_class_or_module( self, extension_array )
12
12
  extend ModuleCluster::IncludeSupport
13
13
  return self
14
14
  end
15
15
 
16
- #############################
16
+ ############################
17
17
  # define_module_extended #
18
- #############################
18
+ ############################
19
19
 
20
- def define_module_extended( & block )
21
- inclusion_array, extension_array = yield_for_includes_extends( block )
22
- ModuleCluster::ExtendSupport.set_includes_for_class_or_module( self, inclusion_array )
23
- ModuleCluster::ExtendSupport.set_extends_for_class_or_module( self, extension_array )
20
+ def define_module_extended( & include_extend_block )
21
+ inclusion_array, extension_array = yield_for_includes_extends( Array::ClassOrModule.new, Array::ClassOrModule.new, include_extend_block )
22
+ ModuleCluster::ExtendSupport.add_includes_for_class_or_module( self, inclusion_array )
23
+ ModuleCluster::ExtendSupport.add_extends_for_class_or_module( self, extension_array )
24
24
  extend ModuleCluster::ExtendSupport
25
25
  return self
26
26
  end
@@ -35,4 +35,92 @@ module ModuleCluster
35
35
  return self
36
36
  end
37
37
 
38
+ #####################################
39
+ # define_module_included_cascades #
40
+ #####################################
41
+
42
+ def define_module_included_cascades( & include_extend_block )
43
+ # any time we are included or extended in a module we want to add cascading extends:
44
+ # * to the module cascading includes
45
+ # any time we are included in a class we want to add cascading extends
46
+ # * to the class
47
+ inclusion_array, extension_array = yield_for_includes_extends( Array::CascadeToClassOrModule.new, Array::CascadeToClassOrModule.new, include_extend_block )
48
+ ModuleCluster::IncludeSupport.add_cascading_includes_for_class_or_module( self, inclusion_array )
49
+ ModuleCluster::IncludeSupport.add_cascading_extends_for_class_or_module( self, extension_array )
50
+ extend ModuleCluster::IncludeSupport
51
+ return inclusion_array, extension_array
52
+ end
53
+
54
+ #####################################
55
+ # define_module_extended_cascades #
56
+ #####################################
57
+
58
+ def define_module_extended_cascades( & include_extend_block )
59
+ # any time we are included or extended in a module we want to add cascading extends:
60
+ # * to the module cascading extends
61
+ # any time we are extended in a class we want to add cascading extends
62
+ # * to the class
63
+ inclusion_array, extension_array = yield_for_includes_extends( Array::CascadeToClassOrModule.new, Array::CascadeToClassOrModule.new, include_extend_block )
64
+ ModuleCluster::ExtendSupport.add_cascading_includes_for_class_or_module( self, inclusion_array )
65
+ ModuleCluster::ExtendSupport.add_cascading_extends_for_class_or_module( self, extension_array )
66
+ extend ModuleCluster::ExtendSupport
67
+ return inclusion_array, extension_array
68
+ end
69
+
70
+ ####################################
71
+ # define_module_cluster_cascades #
72
+ ####################################
73
+
74
+ def define_module_cluster_cascades( & include_extend_block )
75
+ include_includes, include_extends = define_module_included_cascades( & include_extend_block )
76
+ extend_includes, extend_extends = define_module_extended_cascades( & include_extend_block )
77
+ ModuleCluster::IncludeSupport.set_as_cluster( self, include_includes, include_extends )
78
+ ModuleCluster::ExtendSupport.set_as_cluster( self, extend_includes, extend_extends )
79
+ return self
80
+ end
81
+
82
+ ##############################################
83
+ # define_module_included_cascades_to_class #
84
+ ##############################################
85
+
86
+ def define_module_included_cascades_to_class( & include_extend_block )
87
+ # any time we are included or extended in a module we want to add cascading extends:
88
+ # * to the module cascading includes
89
+ # any time we are included in a class we want to add cascading extends
90
+ # * to the class
91
+ inclusion_array, extension_array = yield_for_includes_extends( Array::CascadeToClass.new, Array::CascadeToClass.new, include_extend_block )
92
+ ModuleCluster::IncludeSupport.add_cascading_includes_for_class( self, inclusion_array )
93
+ ModuleCluster::IncludeSupport.add_cascading_extends_for_class( self, extension_array )
94
+ extend ModuleCluster::IncludeSupport
95
+ return inclusion_array, extension_array
96
+ end
97
+
98
+ ##############################################
99
+ # define_module_extended_cascades_to_class #
100
+ ##############################################
101
+
102
+ def define_module_extended_cascades_to_class( & include_extend_block )
103
+ # any time we are included or extended in a module we want to add cascading extends:
104
+ # * to the module cascading extends
105
+ # any time we are extended in a class we want to add cascading extends
106
+ # * to the class
107
+ inclusion_array, extension_array = yield_for_includes_extends( Array::CascadeToClass.new, Array::CascadeToClass.new, include_extend_block )
108
+ ModuleCluster::ExtendSupport.add_cascading_includes_for_class( self, inclusion_array )
109
+ ModuleCluster::ExtendSupport.add_cascading_extends_for_class( self, extension_array )
110
+ extend ModuleCluster::ExtendSupport
111
+ return inclusion_array, extension_array
112
+ end
113
+
114
+ #############################################
115
+ # define_module_cluster_cascades_to_class #
116
+ #############################################
117
+
118
+ def define_module_cluster_cascades_to_class( & include_extend_block )
119
+ include_includes, include_extends = define_module_included_cascades_to_class( & include_extend_block )
120
+ extend_includes, extend_extends = define_module_extended_cascades_to_class( & include_extend_block )
121
+ ModuleCluster::IncludeSupport.set_as_cluster( self, include_includes, include_extends )
122
+ ModuleCluster::ExtendSupport.set_as_cluster( self, extend_includes, extend_extends )
123
+ return self
124
+ end
125
+
38
126
  end