Module: Doodle::Inherited

Defined in:
lib/doodle/inherit.rb

Overview

inherit

the intent of inherit is to provide a way to create directives that affect all members of a class ‘family’ without having to modify Module, Class or Object - in some ways, it’s similar to Ara Howard’s mixable though not as tidy :S

this works down to third level class << self - in practice, this is perfectly good - it would be great to have a completely general solution but I doubt whether the payoff is worth the effort

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



14
15
16
17
# File 'lib/doodle/inherit.rb', line 14

def self.included(other)
  other.extend(Inherited)
  other.send(:include, Factory)
end

Instance Method Details

#inherit(other, &block) ⇒ Object

fake module inheritance chain



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/doodle/inherit.rb', line 20

def inherit(other, &block)
  # include in instance method chain
  include other
  include Inherited

  sc = class << self; self; end
  sc.module_eval {
    # class method chain
    include other
    # singleton method chain
    extend other
    # ensure that subclasses also inherit this module
    define_method :inherited do |klass|
      #p [:inherit, :inherited, klass]
      klass.__send__(:inherit, other)       # n.b. closure
    end
  }
end