carllerche (owner)

Forks

Revisions

gist: 222792 Download_button fork
public
Public Clone URL: git://gist.github.com/222792.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# This is pretty evil, yeah... I know
class Module
  alias old_append_features append_features
  def append_features(base)
    return false if base < self
    (@_needed || []).each { |mod| base.send(:include, mod) }
    (@_class_methods || []).each do |blk|
      (class << base ; self ; end).class_eval(&blk)
    end
    old_append_features(base)
  end
 
  alias old_included included
  def included(base = nil, &blk)
    if block_given?
      @_on_include = blk
    else
      base.class_eval(&@_on_include) if @_on_include
      old_included(base)
    end
  end
 
  def needs(*mods)
    mods.each do |mod|
      next if self < mod
      @_needed ||= []
      @_needed << mod
    end
  end
 
  def class_methods(&blk)
    @_class_methods ||= []
    @_class_methods << blk
  end
end