Module: Cautious

Includes:
SlightlyCautious
Defined in:
lib/cautious.rb

Overview

Have you ever overloaded :included or :extended ? Well you code might look like this:

module A

class << self
  def included(base)
    if base.kind_of?(Class)
      base.extend(FancyClassMethods)
    end
  end
end

module FancyClassMethods
  ... bla ...
end

end

Have we forgotten something? Yep - we don’t call super in included. But apart from that it seems reasonable, however think about the following:

module B

include A

end

When you now include B A.included is not called again thought this might be wanted. However: if you extend A with Cautious this will be done automatically!

module A2

extend Cautious

when_included do |base|
    if base.kind_of?(Class)
      base.extend(FancyClassMethods)
    end
end

module FancyClassMethods
  ... bla ...
end

end

module B2

include A2

end

You can now include B2 in any class and this class will be extended with FancyClassMethods!

Furthermore this module automatically search for a constant named ClassMethods in and includes this. So the example above could also be written as: module A3

extend Cautious

module ClassMethods
  ... bla ...
end

end

module B3

include A3

end

Perfect! Less repetetive code.

Defined Under Namespace

Modules: SlightlyCautious

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



157
158
159
# File 'lib/cautious.rb', line 157

def self.extended(base)
  base.extend(SlightlyCautious)
end

Instance Method Details

#extended(base) ⇒ Object



150
151
152
153
154
155
# File 'lib/cautious.rb', line 150

def extended(base)
  super
  if @cautious_extended_block
    @cautious_extended_block.call(base)
  end
end

#included(base) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cautious.rb', line 127

def included(base)
  if base.kind_of? Class
    if self.const_defined?('ClassMethods')
      base.extend(self::ClassMethods)
    end
  else
    if self.const_defined?('ModuleMethods')
      base.extend(self::ModuleMethods)
    end
  end
  if instance_variable_defined? :@cautious_included_block and @cautious_included_block
    @cautious_included_block.call(base)
  end
  super
end

#inherited(base) ⇒ Object



143
144
145
146
147
148
# File 'lib/cautious.rb', line 143

def inherited(base)
  super
  if @cautious_inherited_block
    @cautious_inherited_block.call(base)
  end
end

#when_extended(&block) ⇒ Object

Create a callback, which is fired whenever this module is extend.



172
173
174
# File 'lib/cautious.rb', line 172

def when_extended(&block)
  @cautious_extended_block = block
end

#when_included(&block) ⇒ Object

Create a callback, which is fired whenever this module is included.



162
163
164
# File 'lib/cautious.rb', line 162

def when_included(&block)
  @cautious_included_block = block
end

#when_inherited(&block) ⇒ Object

Create a callback, which is fired whenever this class is inherited.



167
168
169
# File 'lib/cautious.rb', line 167

def when_inherited(&block)
  @cautious_inherited_block = block
end