cautious 0.0.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/cautious.rb ADDED
@@ -0,0 +1,176 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # This program is free software: you can redistribute it and/or modify
3
+ # it under the terms of the Affero GNU General Public License as published by
4
+ # the Free Software Foundation, either version 3 of the License, or
5
+ # (at your option) any later version.
6
+ #
7
+ # This program is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ # GNU General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+ #
15
+ # (c) 2010 by Hannes Georg
16
+ #
17
+
18
+ #
19
+ # Have you ever overloaded :included or :extended ?
20
+ # Well you code might look like this:
21
+ #
22
+ # module A
23
+ # class << self
24
+ # def included(base)
25
+ # if base.kind_of?(Class)
26
+ # base.extend(FancyClassMethods)
27
+ # end
28
+ # end
29
+ # end
30
+ #
31
+ # module FancyClassMethods
32
+ # ... bla ...
33
+ # end
34
+ #
35
+ # end
36
+ #
37
+ # Have we forgotten something? Yep - we don't call super in included.
38
+ # But apart from that it seems reasonable, however think about the following:
39
+ #
40
+ # module B
41
+ # include A
42
+ # end
43
+ #
44
+ # When you now include B A.included is not called again thought this might be wanted.
45
+ # However: if you extend A with Cautious this will be done automatically!
46
+ #
47
+ # module A2
48
+ # extend Cautious
49
+ #
50
+ # when_included do |base|
51
+ # if base.kind_of?(Class)
52
+ # base.extend(FancyClassMethods)
53
+ # end
54
+ # end
55
+ #
56
+ # module FancyClassMethods
57
+ # ... bla ...
58
+ # end
59
+ #
60
+ # end
61
+ #
62
+ # module B2
63
+ # include A2
64
+ # end
65
+ #
66
+ # You can now include B2 in any class and this class will be extended with FancyClassMethods!
67
+ #
68
+ # Furthermore this module automatically search for a constant named ClassMethods in and includes this.
69
+ # So the example above could also be written as:
70
+ # module A3
71
+ # extend Cautious
72
+ #
73
+ # module ClassMethods
74
+ # ... bla ...
75
+ # end
76
+ #
77
+ # end
78
+ #
79
+ # module B3
80
+ # include A3
81
+ # end
82
+ #
83
+ # Perfect! Less repetetive code.
84
+ #
85
+ module Cautious
86
+
87
+ module SlightlyCautious
88
+
89
+ def included(base)
90
+ self.included_modules.reverse_each do |mod|
91
+ #next unless mod.kind_of? SlightlyCautious
92
+ begin
93
+ mod.included(base)
94
+ rescue NoMethodError
95
+ end
96
+ end
97
+ super
98
+ if base.method(:included).owner == Module
99
+ base.extend(SlightlyCautious)
100
+ end
101
+ end
102
+
103
+ def inherited(base)
104
+ if base.method(:inherited).owner == Class
105
+ base.extend(SlightlyCautious)
106
+ end
107
+ super
108
+ end
109
+
110
+ def extended(base)
111
+ if !base.respond_to? :extended
112
+ if self.const_defined?('ClassMethods')
113
+ base.extend(self::ClassMethods)
114
+ end
115
+ return
116
+ end
117
+ if base.method(:extended).owner == Module
118
+ base.extend(SlightlyCautious)
119
+ end
120
+ super
121
+ end
122
+
123
+ end
124
+
125
+ include SlightlyCautious
126
+
127
+ def included(base)
128
+ if base.kind_of? Class
129
+ if self.const_defined?('ClassMethods')
130
+ base.extend(self::ClassMethods)
131
+ end
132
+ else
133
+ if self.const_defined?('ModuleMethods')
134
+ base.extend(self::ModuleMethods)
135
+ end
136
+ end
137
+ if instance_variable_defined? :@cautious_included_block and @cautious_included_block
138
+ @cautious_included_block.call(base)
139
+ end
140
+ super
141
+ end
142
+
143
+ def inherited(base)
144
+ super
145
+ if @cautious_inherited_block
146
+ @cautious_inherited_block.call(base)
147
+ end
148
+ end
149
+
150
+ def extended(base)
151
+ super
152
+ if @cautious_extended_block
153
+ @cautious_extended_block.call(base)
154
+ end
155
+ end
156
+
157
+ def self.extended(base)
158
+ base.extend(SlightlyCautious)
159
+ end
160
+
161
+ # Create a callback, which is fired whenever this module is included.
162
+ def when_included(&block)
163
+ @cautious_included_block = block
164
+ end
165
+
166
+ # Create a callback, which is fired whenever this class is inherited.
167
+ def when_inherited(&block)
168
+ @cautious_inherited_block = block
169
+ end
170
+
171
+ # Create a callback, which is fired whenever this module is extend.
172
+ def when_extended(&block)
173
+ @cautious_extended_block = block
174
+ end
175
+
176
+ end
data/lib/cautious.rb~ ADDED
@@ -0,0 +1,173 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # This program is free software: you can redistribute it and/or modify
3
+ # it under the terms of the Affero GNU General Public License as published by
4
+ # the Free Software Foundation, either version 3 of the License, or
5
+ # (at your option) any later version.
6
+ #
7
+ # This program is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ # GNU General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+ #
15
+ # (c) 2010 by Hannes Georg
16
+ #
17
+
18
+ #
19
+ # Have you ever overloaded :included or :extended ?
20
+ # Well you code might look like this:
21
+ #
22
+ # module A
23
+ # class << self
24
+ # def included(base)
25
+ # if base.kind_of?(Class)
26
+ # base.extend(FancyClassMethods)
27
+ # end
28
+ # end
29
+ # end
30
+ #
31
+ # module FancyClassMethods
32
+ # ... bla ...
33
+ # end
34
+ #
35
+ # end
36
+ #
37
+ # Have we forgotten something? Yep - we don't call super in included.
38
+ # But apart from that it seems reasonable, however think about the following:
39
+ #
40
+ # module B
41
+ # include A
42
+ # end
43
+ #
44
+ # When you now include B A.included is not called again thought this might be wanted.
45
+ # However: if you extend A with Cautious this will be done automatically!
46
+ #
47
+ # module A2
48
+ # extend Cautious
49
+ #
50
+ # when_included do |base|
51
+ # if base.kind_of?(Class)
52
+ # base.extend(FancyClassMethods)
53
+ # end
54
+ # end
55
+ #
56
+ # module FancyClassMethods
57
+ # ... bla ...
58
+ # end
59
+ #
60
+ # end
61
+ #
62
+ # module B2
63
+ # include A2
64
+ # end
65
+ #
66
+ # You can now include B2 in any class and this class will be extended with FancyClassMethods!
67
+ #
68
+ # Furthermore this module automatically search for a constant named ClassMethods in and includes this.
69
+ # So the example above could also be written as:
70
+ # module A2
71
+ # extend Cautious
72
+ #
73
+ # module ClassMethods
74
+ # ... bla ...
75
+ # end
76
+ #
77
+ # end
78
+ #
79
+ # module B2
80
+ # include A2
81
+ # end
82
+ module Cautious
83
+
84
+ module SlightlyCautious
85
+
86
+ def included(base)
87
+ self.included_modules.reverse_each do |mod|
88
+ #next unless mod.kind_of? SlightlyCautious
89
+ begin
90
+ mod.included(base)
91
+ rescue NoMethodError
92
+ end
93
+ end
94
+ super
95
+ if base.method(:included).owner == Module
96
+ base.extend(SlightlyCautious)
97
+ end
98
+ end
99
+
100
+ def inherited(base)
101
+ if base.method(:inherited).owner == Class
102
+ base.extend(SlightlyCautious)
103
+ end
104
+ super
105
+ end
106
+
107
+ def extended(base)
108
+ if !base.respond_to? :extended
109
+ if self.const_defined?('ClassMethods')
110
+ base.extend(self::ClassMethods)
111
+ end
112
+ return
113
+ end
114
+ if base.method(:extended).owner == Module
115
+ base.extend(SlightlyCautious)
116
+ end
117
+ super
118
+ end
119
+
120
+ end
121
+
122
+ include SlightlyCautious
123
+
124
+ def included(base)
125
+ if base.kind_of? Class
126
+ if self.const_defined?('ClassMethods')
127
+ base.extend(self::ClassMethods)
128
+ end
129
+ else
130
+ if self.const_defined?('ModuleMethods')
131
+ base.extend(self::ModuleMethods)
132
+ end
133
+ end
134
+ if instance_variable_defined? :@cautious_included_block and @cautious_included_block
135
+ @cautious_included_block.call(base)
136
+ end
137
+ super
138
+ end
139
+
140
+ def inherited(base)
141
+ super
142
+ if @cautious_inherited_block
143
+ @cautious_inherited_block.call(base)
144
+ end
145
+ end
146
+
147
+ def extended(base)
148
+ super
149
+ if @cautious_extended_block
150
+ @cautious_extended_block.call(base)
151
+ end
152
+ end
153
+
154
+ def self.extended(base)
155
+ base.extend(SlightlyCautious)
156
+ end
157
+
158
+ # Create a callback, which is fired whenever this module is included.
159
+ def when_included(&block)
160
+ @cautious_included_block = block
161
+ end
162
+
163
+ # Create a callback, which is fired whenever this class is inherited.
164
+ def when_inherited(&block)
165
+ @cautious_inherited_block = block
166
+ end
167
+
168
+ # Create a callback, which is fired whenever this module is extend.
169
+ def when_extended(&block)
170
+ @cautious_extended_block = block
171
+ end
172
+
173
+ end
@@ -0,0 +1,86 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # This program is free software: you can redistribute it and/or modify
3
+ # it under the terms of the Affero GNU General Public License as published by
4
+ # the Free Software Foundation, either version 3 of the License, or
5
+ # (at your option) any later version.
6
+ #
7
+ # This program is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ # GNU General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+ #
15
+ # (c) 2010 by Hannes Georg
16
+ #
17
+ module Combineable
18
+
19
+ Combination = Struct.new(:modules,:block)
20
+
21
+ def combined_with(*modules,&block)
22
+ @module_combinations ||= []
23
+ @module_combinations << Combination.new(modules,block)
24
+ end
25
+
26
+ def included(base,*)
27
+ super
28
+ self.__combined(base)
29
+ end
30
+
31
+ protected
32
+ def __combined(base)
33
+ if instance_variable_defined?(:@module_combinations)
34
+ given = base.included_modules
35
+ @module_combinations.each do |combination|
36
+ # a bit expensive
37
+ if combination.modules.all?{|mod| base <= mod }#{|mod| given.any?{|mod2| mod2 <= mod } }
38
+ # Hit!
39
+ combination.block.call(base)
40
+ else
41
+ __delay_combination(base,combination)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ def __delay_combination(base,combo)
48
+ if !base.kind_of? IncludeInvader
49
+ base.extend(IncludeInvader)
50
+ end
51
+ base.wait_on(combo)
52
+ end
53
+
54
+ module IncludeInvader
55
+
56
+ def include(mod)
57
+ super
58
+ __recheck_combos(self)
59
+ end
60
+
61
+ def wait_on(combination)
62
+ @module_combinations ||= []
63
+ @module_combinations << combination
64
+ end
65
+
66
+ protected
67
+ def __recheck_combos(base)
68
+ if self.kind_of? Class
69
+ self.superclass.__recheck_combos(base) if self.superclass.respond_to? :__recheck_combos
70
+ end
71
+ if instance_variable_defined?(:@module_combinations)
72
+ given = base.included_modules
73
+ @module_combinations.each do |combination|
74
+ # a bit expensive
75
+ if combination.modules.all?{|mod| base <= mod } #{|mod| given.any?{|mod2| mod2 <= mod } }
76
+ # Hit!
77
+ combination.block.call(base)
78
+ @module_combinations.delete(combination) if self == base
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ end
File without changes
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cautious
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - HannesG
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-04 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &19956620 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *19956620
25
+ - !ruby/object:Gem::Dependency
26
+ name: simplecov
27
+ requirement: &19954780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *19954780
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &19953540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *19953540
47
+ description: ! 'This package contains two modules: Cautious and Combineable'
48
+ email: hannes.georg@googlemail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/combineable.rb~
54
+ - lib/cautious.rb~
55
+ - lib/combineable.rb
56
+ - lib/cautious.rb
57
+ homepage: http://github.com/hannesg/cautious
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.10
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A simple yet nifty module which helps you combining modules.
81
+ test_files: []