diff options
Diffstat (limited to 'activesupport/lib')
16 files changed, 135 insertions, 62 deletions
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index f498a8e1f5..7bd74101f5 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -23,13 +23,12 @@ $:.unshift(File.dirname(__FILE__)) -require 'active_support/class_attribute_accessors' -require 'active_support/class_inheritable_attributes' require 'active_support/inflector' require 'active_support/core_ext' require 'active_support/clean_logger' require 'active_support/dependencies' +require 'active_support/reloadable' require 'active_support/ordered_options' require 'active_support/option_merger' diff --git a/activesupport/lib/active_support/clean_logger.rb b/activesupport/lib/active_support/clean_logger.rb index 2730ef9008..376896cb3c 100644 --- a/activesupport/lib/active_support/clean_logger.rb +++ b/activesupport/lib/active_support/clean_logger.rb @@ -1,5 +1,5 @@ require 'logger' -require File.dirname(__FILE__) + '/class_attribute_accessors' +require File.dirname(__FILE__) + '/core_ext/class/attribute_accessors' class Logger #:nodoc: cattr_accessor :silencer diff --git a/activesupport/lib/active_support/core_ext/blank.rb b/activesupport/lib/active_support/core_ext/blank.rb index 791cdc5705..f7fbea3e89 100644 --- a/activesupport/lib/active_support/core_ext/blank.rb +++ b/activesupport/lib/active_support/core_ext/blank.rb @@ -1,4 +1,16 @@ -# The methods here are provided to speed up function blank? in class Object +class Object #:nodoc: + # "", " ", nil, [], and {} are blank + def blank? + if respond_to?(:empty?) && respond_to?(:strip) + empty? or strip.empty? + elsif respond_to?(:empty?) + empty? + else + !self + end + end +end + class NilClass #:nodoc: def blank? true @@ -35,4 +47,4 @@ class Numeric #:nodoc: def blank? false end -end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/class.rb b/activesupport/lib/active_support/core_ext/class.rb new file mode 100644 index 0000000000..7bacdb3903 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/class.rb @@ -0,0 +1,3 @@ +require File.dirname(__FILE__) + '/class/attribute_accessors' +require File.dirname(__FILE__) + '/class/inheritable_attributes' +require File.dirname(__FILE__) + '/class/removal'
\ No newline at end of file diff --git a/activesupport/lib/active_support/class_attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index 93a7d48f69..93a7d48f69 100644 --- a/activesupport/lib/active_support/class_attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb diff --git a/activesupport/lib/active_support/class_inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index 2e70a64436..2e70a64436 100644 --- a/activesupport/lib/active_support/class_inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb diff --git a/activesupport/lib/active_support/core_ext/class/removal.rb b/activesupport/lib/active_support/core_ext/class/removal.rb new file mode 100644 index 0000000000..8fc4d728b3 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/class/removal.rb @@ -0,0 +1,21 @@ +class Class #:nodoc: + def remove_subclasses + Object.remove_subclasses_of(self) + end + + def subclasses + Object.subclasses_of(self).map { |o| o.to_s } + end + + def remove_class(klass) + if klass.to_s.include? "::" + modules = klass.to_s.split("::") + final_klass = modules.pop + + final_module = modules.inject(Object) { |final_type, part| final_type.const_get(part) } + final_module.send(:remove_const, final_klass) rescue nil + else + Object.send(:remove_const, klass.to_s) rescue nil + end + end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/exception.rb b/activesupport/lib/active_support/core_ext/exception.rb index 2541f37495..0b65af7bf8 100644 --- a/activesupport/lib/active_support/core_ext/exception.rb +++ b/activesupport/lib/active_support/core_ext/exception.rb @@ -1,5 +1,4 @@ class Exception - alias :clean_message :message TraceSubstitutions = [] diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb new file mode 100644 index 0000000000..0f46631e65 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -0,0 +1,2 @@ +require File.dirname(__FILE__) + '/module/inclusion' +require File.dirname(__FILE__) + '/module/attribute_accessors'
\ No newline at end of file diff --git a/activesupport/lib/active_support/module_attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb index fe4f8a4fc0..fe4f8a4fc0 100644 --- a/activesupport/lib/active_support/module_attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb diff --git a/activesupport/lib/active_support/core_ext/module/inclusion.rb b/activesupport/lib/active_support/core_ext/module/inclusion.rb new file mode 100644 index 0000000000..124c4551bc --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/inclusion.rb @@ -0,0 +1,15 @@ +class Module + def remove_classes_including + included_in_classes.each { |klass| Class.remove_class(klass) } + end + + def included_in_classes + classes = [] + ObjectSpace.each_object(Class) { |k| classes << k if k.included_modules.include?(self) } + + classes.reverse.inject([]) do |unique_classes, klass| + unique_classes << klass unless unique_classes.collect { |k| k.to_s }.include?(klass.to_s) + unique_classes + end + end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb new file mode 100644 index 0000000000..3dd6deaba1 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -0,0 +1,2 @@ +require File.dirname(__FILE__) + '/object/extending' +require File.dirname(__FILE__) + '/object/misc'
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/object_and_class.rb b/activesupport/lib/active_support/core_ext/object/extending.rb index d44afbd839..8c638f83c2 100644 --- a/activesupport/lib/active_support/core_ext/object_and_class.rb +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -4,7 +4,7 @@ class Object #:nodoc: Object.send(:remove_const, subclass.to_s) rescue nil end end - + def subclasses_of(*superclasses) subclasses = [] ObjectSpace.each_object(Class) do |k| @@ -20,41 +20,16 @@ class Object #:nodoc: end def copy_instance_variables_from(object, exclude = []) - exclude += object.protected_instance_variables if - object.respond_to? :protected_instance_variables + exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables instance_variables = object.instance_variables - exclude.map { |name| name.to_s } - instance_variables.each do |name| - instance_variable_set name, object.instance_variable_get(name) - end + instance_variables.each { |name| instance_variable_set(name, object.instance_variable_get(name)) } end def extend_with_included_modules_from(object) object.extended_by.each { |mod| extend mod } end - - # "", " ", nil, [], and {} are blank - def blank? - if respond_to?(:empty?) && respond_to?(:strip) - empty? or strip.empty? - elsif respond_to?(:empty?) - empty? - else - !self - end - end - def suppress(*exception_classes) - begin yield - rescue Exception => e - raise unless exception_classes.any? {|cls| e.kind_of? cls} - end - end - - def with_options(options) - yield ActiveSupport::OptionMerger.new(self, options) - end - def instance_values instance_variables.inject({}) do |values, name| values[name[1..-1]] = instance_variable_get(name) @@ -62,23 +37,9 @@ class Object #:nodoc: end end - def to_json - ActiveSupport::JSON.encode(self) - end - unless defined? instance_exec # 1.9 def instance_exec(*arguments, &block) block.bind(self)[*arguments] end end -end - -class Class #:nodoc: - def remove_subclasses - Object.remove_subclasses_of(self) - end - - def subclasses - Object.subclasses_of(self).map { |o| o.to_s } - end -end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb new file mode 100644 index 0000000000..f599eee6f0 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -0,0 +1,16 @@ +class Object #:nodoc: + def with_options(options) + yield ActiveSupport::OptionMerger.new(self, options) + end + + def to_json + ActiveSupport::JSON.encode(self) + end + + def suppress(*exception_classes) + begin yield + rescue Exception => e + raise unless exception_classes.any? { |cls| e.kind_of?(cls) } + end + end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 8d1288cd96..37a3f4acb3 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -1,5 +1,5 @@ require 'set' -require File.dirname(__FILE__) + '/module_attribute_accessors' +require File.dirname(__FILE__) + '/core_ext/module/attribute_accessors' require File.dirname(__FILE__) + '/core_ext/load_error' require File.dirname(__FILE__) + '/core_ext/kernel' @@ -69,10 +69,6 @@ module Dependencies #:nodoc: history << file_name end - def remove_subclasses_for(*classes) - Object.remove_subclasses_of(*classes) - end - # LoadingModules implement namespace-safe dynamic loading. # They support automatic loading via const_missing, allowing contained items to be automatically # loaded when required. No extra syntax is required, as expressions such as Controller::Admin::UserController @@ -211,27 +207,70 @@ Object.send(:define_method, :require_association) { |file_name| Dependencies.ass class Module #:nodoc: # Rename the original handler so we can chain it to the new one alias :rails_original_const_missing :const_missing - + + def parent + parent_name = name.split('::')[0..-2] * '::' + parent_name.empty? ? Object : parent_name.constantize + end + + def as_load_path + if self == Object || self == Kernel + '' + elsif is_a? Class + parent == self ? '' : parent.as_load_path + else + name.split('::').collect do |word| + word.underscore + end * '/' + end + end + # Use const_missing to autoload associations so we don't have to # require_association when using single-table inheritance. def const_missing(class_id) - if Object.const_defined?(:Controllers) and Object::Controllers.const_available?(class_id) + if Object.const_defined?(:Controllers) && Object::Controllers.const_available?(class_id) return Object::Controllers.const_get(class_id) end file_name = class_id.to_s.demodulize.underscore + file_path = as_load_path.empty? ? file_name : "#{as_load_path}/#{file_name}" begin - require_dependency(file_name) - raise NameError.new("uninitialized constant #{class_id}") unless Object.const_defined?(class_id) - return Object.const_get(class_id) + require_dependency(file_path) + brief_name = self == Object ? '' : "#{name}::" + raise NameError.new("uninitialized constant #{brief_name}#{class_id}") unless const_defined?(class_id) + return const_get(class_id) rescue MissingSourceFile => e - # Convert the exception to a NameError only if the file we are looking for is the missing one. - raise unless e.is_missing? file_name + # Re-raise the error if it does not concern the file we were trying to load. + raise unless e.is_missing? file_path + + # Look for a directory in the load path that we ought to load. + if $LOAD_PATH.any? { |base| File.directory? "#{base}/#{file_path}" } + mod = Module.new + const_set class_id, mod # Create the new module + return mod + end + + if parent && parent != self + suppress(NameError) do + return parent.send(:const_missing, class_id) + end + end + raise NameError.new("uninitialized constant #{class_id}").copy_blame!(e) end end end +class Class + def const_missing(class_id) + if [Object, Kernel].include?(self) || parent == self + super + else + parent.send :const_missing, class_id + end + end +end + class Object #:nodoc: def load(file, *extras) super(file, *extras) @@ -267,4 +306,4 @@ class Exception #:nodoc: @blamed_files = exc.blamed_files.clone self end -end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/reloadable.rb b/activesupport/lib/active_support/reloadable.rb new file mode 100644 index 0000000000..fca397afeb --- /dev/null +++ b/activesupport/lib/active_support/reloadable.rb @@ -0,0 +1,4 @@ +# Classes that include this module will automatically be reloaded +# by the Rails dispatcher when Dependencies.mechanism = :load. +module Reloadable +end
\ No newline at end of file |