From c1aa5b0e14cd4bd27a5d8bd85cf7c36fa5911830 Mon Sep 17 00:00:00 2001 From: Yehuda Katz and Carl Lerche Date: Tue, 7 Apr 2009 14:57:18 -0700 Subject: Add depends_on, use, and setup to abstract up ideas about module inheritance. --- .../lib/active_support/core_ext/module.rb | 1 + .../lib/active_support/core_ext/module/setup.rb | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/module/setup.rb (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index da8d28ec13..cb31437094 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -8,6 +8,7 @@ require 'active_support/core_ext/module/loading' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/model_naming' require 'active_support/core_ext/module/synchronization' +require 'active_support/core_ext/module/setup' module ActiveSupport module CoreExtensions diff --git a/activesupport/lib/active_support/core_ext/module/setup.rb b/activesupport/lib/active_support/core_ext/module/setup.rb new file mode 100644 index 0000000000..e6dfd0cf56 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/setup.rb @@ -0,0 +1,26 @@ +class Module + attr_accessor :_setup_block + attr_accessor :_dependencies + + def setup(&blk) + @_setup_block = blk + end + + def use(mod) + return if self < mod + + (mod._dependencies || []).each do |dep| + use dep + end + # raise "Circular dependencies" if self < mod + include mod + extend mod.const_get("ClassMethods") if mod.const_defined?("ClassMethods") + class_eval(&mod._setup_block) if mod._setup_block + end + + def depends_on(mod) + return if self < mod + @_dependencies ||= [] + @_dependencies << mod + end +end \ No newline at end of file -- cgit v1.2.3