From 918dc27345319fbabf25a43bd65b613878b3a66e Mon Sep 17 00:00:00 2001 From: thedarkone Date: Mon, 27 Sep 2010 14:50:39 +0200 Subject: Compile ActionController::Base.config's methods to avoid method_missing overhead. --- activesupport/lib/active_support/configurable.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/configurable.rb') diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb index 5b85f9394a..36634bd7f3 100644 --- a/activesupport/lib/active_support/configurable.rb +++ b/activesupport/lib/active_support/configurable.rb @@ -9,9 +9,29 @@ module ActiveSupport module Configurable extend ActiveSupport::Concern + class Configuration < ActiveSupport::InheritableOptions + def crystalize! + self.class.crystalize!(keys.reject {|key| respond_to?(key)}) + end + + # compiles reader methods so we don't have to go through method_missing + def self.crystalize!(keys) + keys.each do |key| + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{key}; self[#{key.inspect}]; end + RUBY + end + end + end + module ClassMethods def config - @_config ||= ActiveSupport::InheritableOptions.new(superclass.respond_to?(:config) ? superclass.config : {}) + @_config ||= if superclass.respond_to?(:config) + superclass.config.inheritable_copy + else + # create a new "anonymous" class that will host the compiled reader methods + Class.new(Configuration).new({}) + end end def configure @@ -48,7 +68,7 @@ module ActiveSupport # user.config.level # => 1 # def config - @_config ||= ActiveSupport::InheritableOptions.new(self.class.config) + @_config ||= self.class.config.inheritable_copy end end end -- cgit v1.2.3