Module: Doodle::Embrace

Defined in:
lib/doodle.rb

Overview

embrace

the intent of embrace 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’m doubt whether the payoff is worth the effort

Instance Method Summary collapse

Instance Method Details

#embrace(other, &block) ⇒ Object

fake module inheritance chain



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/doodle.rb', line 139

def embrace(other, &block)
  # include in instance method chain
  include other
  sc = class << self; self; end
  sc.module_eval {
    # class method chain
    include other
    # singleton method chain
    extend other
    # ensure that subclasses are also embraced
    define_method :inherited do |klass|
      #p [:embrace, :inherited, klass]
      klass.__send__(:embrace, other)       # n.b. closure
      klass.__send__(:include, Factory)     # is there another way to do this? i.e. not in embrace
      super(klass) if defined?(super)
    end
  }
  sc.module_eval(&block) if block_given?
end