From d77deb89d54b18c662ae3de103802e4d7a9d7d08 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 28 Dec 2008 13:21:10 +0300 Subject: Annotated metaprogramming code across ActiveSupport --- .../lib/active_support/buffered_logger.rb | 16 +++--- activesupport/lib/active_support/callbacks.rb | 28 +++++------ .../core_ext/class/attribute_accessors.rb | 46 ++++++++--------- .../core_ext/class/delegating_attributes.rb | 42 ++++++++-------- .../core_ext/class/inheritable_attributes.rb | 58 +++++++++++----------- .../core_ext/module/attribute_accessors.rb | 46 ++++++++--------- activesupport/lib/active_support/core_ext/proc.rb | 6 +-- .../active_support/core_ext/string/multibyte.rb | 6 ++- activesupport/lib/active_support/deprecation.rb | 12 +++-- activesupport/lib/active_support/memoizable.rb | 52 +++++++++---------- activesupport/lib/active_support/minimalistic.rb | 15 ++++++ .../active_support/multibyte/unicode_database.rb | 10 ++-- activesupport/lib/active_support/time_with_zone.rb | 8 +-- 13 files changed, 183 insertions(+), 162 deletions(-) create mode 100644 activesupport/lib/active_support/minimalistic.rb (limited to 'activesupport') diff --git a/activesupport/lib/active_support/buffered_logger.rb b/activesupport/lib/active_support/buffered_logger.rb index 445d8edf47..568c596f91 100644 --- a/activesupport/lib/active_support/buffered_logger.rb +++ b/activesupport/lib/active_support/buffered_logger.rb @@ -67,14 +67,14 @@ module ActiveSupport end for severity in Severity.constants - class_eval <<-EOT, __FILE__, __LINE__ - def #{severity.downcase}(message = nil, progname = nil, &block) - add(#{severity}, message, progname, &block) - end - - def #{severity.downcase}? - #{severity} >= @level - end + class_eval <<-EOT, __FILE__, __LINE__ + 1 + def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block) + add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block) + end # end + + def #{severity.downcase}? # def debug? + #{severity} >= @level # DEBUG >= @level + end # end EOT end diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 5cdcaf5ad1..aabf0a0895 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -209,21 +209,21 @@ module ActiveSupport module ClassMethods def define_callbacks(*callbacks) callbacks.each do |callback| - class_eval <<-"end_eval" - def self.#{callback}(*methods, &block) - callbacks = CallbackChain.build(:#{callback}, *methods, &block) - (@#{callback}_callbacks ||= CallbackChain.new).concat callbacks - end + class_eval <<-"end_eval", __FILE__, __LINE__ + 1 + def self.#{callback}(*methods, &block) # def self.validate_on_create(*methods, &block) + callbacks = CallbackChain.build(:#{callback}, *methods, &block) # callbacks = CallbackChain.build(:validate_on_create, *methods, &block) + (@#{callback}_callbacks ||= CallbackChain.new).concat callbacks # (@validate_on_create_callbacks ||= CallbackChain.new).concat callbacks + end # end - def self.#{callback}_callback_chain - @#{callback}_callbacks ||= CallbackChain.new - - if superclass.respond_to?(:#{callback}_callback_chain) - CallbackChain.new(superclass.#{callback}_callback_chain + @#{callback}_callbacks) - else - @#{callback}_callbacks - end - end + def self.#{callback}_callback_chain # def self.validate_on_create_callback_chain + @#{callback}_callbacks ||= CallbackChain.new # @validate_on_create_callbacks ||= CallbackChain.new + # + if superclass.respond_to?(:#{callback}_callback_chain) # if superclass.respond_to?(:validate_on_create_callback_chain) + CallbackChain.new(superclass.#{callback}_callback_chain + @#{callback}_callbacks) # CallbackChain.new(superclass.validate_on_create_callback_chain + @validate_on_create_callbacks) + else # else + @#{callback}_callbacks # @validate_on_create_callbacks + end # end + end # end end_eval end end diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index 186ca69c05..6b2ac8b38b 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -10,18 +10,18 @@ class Class def cattr_reader(*syms) syms.flatten.each do |sym| next if sym.is_a?(Hash) - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined @@property + @@#{sym} = nil # @@property = nil + end # end - def self.#{sym} - @@#{sym} - end + def self.#{sym} # def self.property + @@#{sym} # @@property + end # end - def #{sym} - @@#{sym} - end + def #{sym} # def property + @@#{sym} # @@property + end # end EOS end end @@ -29,19 +29,19 @@ class Class def cattr_writer(*syms) options = syms.extract_options! syms.flatten.each do |sym| - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end - - def self.#{sym}=(obj) - @@#{sym} = obj - end - - #{" - def #{sym}=(obj) - @@#{sym} = obj - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined? @@property + @@#{sym} = nil # @@property = nil + end # end + + def self.#{sym}=(obj) # def self.property=(obj) + @@#{sym} = obj # @@property + end # end + + #{" + def #{sym}=(obj) # def property=(obj) + @@#{sym} = obj # @@property = obj + end # end " unless options[:instance_writer] == false } EOS end diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb index 368317df9b..3b093a9a65 100644 --- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -8,33 +8,33 @@ class Class def superclass_delegating_reader(*names) class_name_to_stop_searching_on = self.superclass.name.blank? ? "Object" : self.superclass.name names.each do |name| - class_eval <<-EOS - def self.#{name} - if defined?(@#{name}) - @#{name} - elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name}) - superclass.#{name} - end - end - def #{name} - self.class.#{name} - end - def self.#{name}? - !!#{name} - end - def #{name}? - !!#{name} - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{name} # def self.property + if defined?(@#{name}) # if defined?(@property) + @#{name} # @property + elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name}) # elseif superclass < Object && superclass.respond_to?(:property) + superclass.#{name} # superclass.property + end # end + end # end + def #{name} # def property + self.class.#{name} # self.class.property + end # end + def self.#{name}? # def self.property? + !!#{name} # !!property + end # end + def #{name}? # def property? + !!#{name} # !!property + end # end EOS end end def superclass_delegating_writer(*names) names.each do |name| - class_eval <<-EOS - def self.#{name}=(value) - @#{name} = value - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{name}=(value) # def self.property=(value) + @#{name} = value # @property = value + end # end EOS end end diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index e6143a274b..8d0233ce9a 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -10,14 +10,14 @@ class Class # :nodoc: def class_inheritable_reader(*syms) syms.each do |sym| next if sym.is_a?(Hash) - class_eval <<-EOS - def self.#{sym} - read_inheritable_attribute(:#{sym}) - end - - def #{sym} - self.class.#{sym} - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym} # def self.after_add + read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:after_add) + end # end + + def #{sym} # def after_add + self.class.#{sym} # self.class.after_add + end # end EOS end end @@ -25,15 +25,15 @@ class Class # :nodoc: def class_inheritable_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval <<-EOS - def self.#{sym}=(obj) - write_inheritable_attribute(:#{sym}, obj) - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym}=(obj) # def self.property=(obj) + write_inheritable_attribute(:#{sym}, obj) # write_inheritable_attribute(:property, obj) + end # end #{" - def #{sym}=(obj) - self.class.#{sym} = obj - end + def #{sym}=(obj) # def property=(obj) + self.class.#{sym} = obj # self.class.property = obj + end # end " unless options[:instance_writer] == false } EOS end @@ -42,15 +42,15 @@ class Class # :nodoc: def class_inheritable_array_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval <<-EOS - def self.#{sym}=(obj) - write_inheritable_array(:#{sym}, obj) - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym}=(obj) # def self.property=(obj) + write_inheritable_array(:#{sym}, obj) # write_inheritable_array(:property, obj) + end # end #{" - def #{sym}=(obj) - self.class.#{sym} = obj - end + def #{sym}=(obj) # def property=(obj) + self.class.#{sym} = obj # self.class.property = obj + end # end " unless options[:instance_writer] == false } EOS end @@ -59,15 +59,15 @@ class Class # :nodoc: def class_inheritable_hash_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval <<-EOS - def self.#{sym}=(obj) - write_inheritable_hash(:#{sym}, obj) - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym}=(obj) # def self.property=(obj) + write_inheritable_hash(:#{sym}, obj) # write_inheritable_hash(:property, obj) + end # end #{" - def #{sym}=(obj) - self.class.#{sym} = obj - end + def #{sym}=(obj) # def property=(obj) + self.class.#{sym} = obj # self.class.property = obj + end # end " unless options[:instance_writer] == false } EOS end diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb index 51e1c9af90..9467eb71ac 100644 --- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb @@ -14,18 +14,18 @@ class Module def mattr_reader(*syms) syms.each do |sym| next if sym.is_a?(Hash) - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined? @@property + @@#{sym} = nil # @@ property = nil + end # end - def self.#{sym} - @@#{sym} - end + def self.#{sym} # def self.property + @@#{sym} # @@property + end # end - def #{sym} - @@#{sym} - end + def #{sym} # def property + @@#{sym} # @@property + end # end EOS end end @@ -33,19 +33,19 @@ class Module def mattr_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end - - def self.#{sym}=(obj) - @@#{sym} = obj - end - - #{" - def #{sym}=(obj) - @@#{sym} = obj - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined? @@property + @@#{sym} = nil # @@ property = nil + end # end + + def self.#{sym}=(obj) # def self.property=(obj) + @@#{sym} = obj # @@property = obj + end # end + + #{" + def #{sym}=(obj) # def property=(obj) + @@#{sym} = obj # @@property = obj + end # end " unless options[:instance_writer] == false } EOS end diff --git a/activesupport/lib/active_support/core_ext/proc.rb b/activesupport/lib/active_support/core_ext/proc.rb index 2ca23f62ef..5c29cc32a2 100644 --- a/activesupport/lib/active_support/core_ext/proc.rb +++ b/activesupport/lib/active_support/core_ext/proc.rb @@ -3,9 +3,9 @@ class Proc #:nodoc: block, time = self, Time.now (class << object; self end).class_eval do method_name = "__bind_#{time.to_i}_#{time.usec}" - define_method(method_name, &block) - method = instance_method(method_name) - remove_method(method_name) + define_method(method_name, &block) # define_method("__bind_1230458026_720454", &block) + method = instance_method(method_name) # method = instance_method("__bind_1230458026_720454") + remove_method(method_name) # remove_method("__bind_1230458026_720454") method end.bind(object) end diff --git a/activesupport/lib/active_support/core_ext/string/multibyte.rb b/activesupport/lib/active_support/core_ext/string/multibyte.rb index a4caa83b74..8f8f0968fd 100644 --- a/activesupport/lib/active_support/core_ext/string/multibyte.rb +++ b/activesupport/lib/active_support/core_ext/string/multibyte.rb @@ -55,7 +55,11 @@ module ActiveSupport #:nodoc: unless '1.8.7 and later'.respond_to?(:chars) def chars - ActiveSupport::Deprecation.warn('String#chars has been deprecated in favor of String#mb_chars.', caller) + # FIXME: + # ActiveSupport::Deprecation refers to RAILS_ENV + # and is a show stopper for 3rd party applications + # that only want ActiveSupport + ActiveSupport::Deprecation.warn('String#chars has been deprecated in favor of String#mb_chars.', caller) if defined?(ActiveSupport::Deprecation) mb_chars end end diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index 25b26e9c96..d9bd40199f 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -89,11 +89,13 @@ module ActiveSupport method_names = method_names + options.keys method_names.each do |method_name| alias_method_chain(method_name, :deprecation) do |target, punctuation| - class_eval(<<-EOS, __FILE__, __LINE__) - def #{target}_with_deprecation#{punctuation}(*args, &block) - ::ActiveSupport::Deprecation.warn(self.class.deprecated_method_warning(:#{method_name}, #{options[method_name].inspect}), caller) - #{target}_without_deprecation#{punctuation}(*args, &block) - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def #{target}_with_deprecation#{punctuation}(*args, &block) # def multi_with_reprecation(*args, &block) + ::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn( + self.class.deprecated_method_warning(:#{method_name}, #{options[method_name].inspect}), # self.class.deprecated_method_warning(:multi, "this method is deprecated, blah, blah, blah") + caller) # caller) + #{target}_without_deprecation#{punctuation}(*args, &block) # multi_without_deprecation(*args, &block) + end # end EOS end end diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index 9f2fd3a401..67ed10d58d 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -58,35 +58,35 @@ module ActiveSupport original_method = :"_unmemoized_#{symbol}" memoized_ivar = ActiveSupport::Memoizable.memoized_ivar_for(symbol) - class_eval <<-EOS, __FILE__, __LINE__ + class_eval <<-EOS, __FILE__, __LINE__ + 1 include InstanceMethods - raise "Already memoized #{symbol}" if method_defined?(:#{original_method}) - alias #{original_method} #{symbol} + raise "Already memoized #{symbol}" if method_defined?(:#{original_method}) # raise "Already memoized if_modified_since" if method_defined?(:__unmemoized_if_modified_since) + alias #{original_method} #{symbol} # alias __unmemoized_if_modified_since if_modified_since - if instance_method(:#{symbol}).arity == 0 - def #{symbol}(reload = false) - if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? - #{memoized_ivar} = [#{original_method}.freeze] - end - #{memoized_ivar}[0] - end - else - def #{symbol}(*args) - #{memoized_ivar} ||= {} unless frozen? - reload = args.pop if args.last == true || args.last == :reload - - if defined?(#{memoized_ivar}) && #{memoized_ivar} - if !reload && #{memoized_ivar}.has_key?(args) - #{memoized_ivar}[args] - elsif #{memoized_ivar} - #{memoized_ivar}[args] = #{original_method}(*args).freeze - end - else - #{original_method}(*args) - end - end - end + if instance_method(:#{symbol}).arity == 0 # if instance_method(:if_modified_since).arity == 0 + def #{symbol}(reload = false) # def if_modified_since(reload = false) + if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? # if reload || !defined?(@_memoized_if_modified_since) || @_memoized_if_modified_since.empty? + #{memoized_ivar} = [#{original_method}.freeze] # @_memoized_if_modified_since = [__unmemoized_if_modified_since.freeze] + end # end + #{memoized_ivar}[0] # @_memoized_if_modified_since[0] + end # end + else # else + def #{symbol}(*args) # def if_modified_since(*args) + #{memoized_ivar} ||= {} unless frozen? # @_memoized_if_modified_since ||= {} unless frozen? + reload = args.pop if args.last == true || args.last == :reload # reload = args.pop if args.last == true || args.last == :reload + # + if defined?(#{memoized_ivar}) && #{memoized_ivar} # if defined?(@_memoized_if_modified_since) && @_memoized_if_modified_since + if !reload && #{memoized_ivar}.has_key?(args) # if !reload && @_memoized_if_modified_since.has_key?(args) + #{memoized_ivar}[args] # @_memoized_if_modified_since[args] + elsif #{memoized_ivar} # elsif @_memoized_if_modified_since + #{memoized_ivar}[args] = #{original_method}(*args).freeze # @_memoized_if_modified_since[args] = __unmemoized_if_modified_since(*args).freeze + end # end + else # else + #{original_method}(*args) # __unmemoized_if_modified_since(*args) + end # end + end # end + end # end EOS end end diff --git a/activesupport/lib/active_support/minimalistic.rb b/activesupport/lib/active_support/minimalistic.rb new file mode 100644 index 0000000000..ecb5de9eaa --- /dev/null +++ b/activesupport/lib/active_support/minimalistic.rb @@ -0,0 +1,15 @@ +$LOAD_PATH.unshift File.dirname(__FILE__) + +require "core_ext/blank" +# whole object.rb pulls up rare used introspection extensions +require "core_ext/object/metaclass" +require 'core_ext/array' +require 'core_ext/hash' +require 'core_ext/module/attribute_accessors' +require 'multibyte' +require 'core_ext/string/multibyte' +require 'core_ext/string/inflections' + +class String + include ActiveSupport::CoreExtensions::String::Multibyte +end diff --git a/activesupport/lib/active_support/multibyte/unicode_database.rb b/activesupport/lib/active_support/multibyte/unicode_database.rb index 3b8cf8f9eb..074ad8613a 100644 --- a/activesupport/lib/active_support/multibyte/unicode_database.rb +++ b/activesupport/lib/active_support/multibyte/unicode_database.rb @@ -23,11 +23,11 @@ module ActiveSupport #:nodoc: # Lazy load the Unicode database so it's only loaded when it's actually used ATTRIBUTES.each do |attr_name| - class_eval(<<-EOS, __FILE__, __LINE__) - def #{attr_name} - load - @#{attr_name} - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def #{attr_name} # def codepoints + load # load + @#{attr_name} # @codepoints + end # end EOS end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 9a2d283b30..59f1e6163b 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -233,10 +233,10 @@ module ActiveSupport end %w(year mon month day mday wday yday hour min sec to_date).each do |method_name| - class_eval <<-EOV - def #{method_name} - time.#{method_name} - end + class_eval <<-EOV, __FILE__, __LINE__ + 1 + def #{method_name} # def month + time.#{method_name} # time.month + end # end EOV end -- cgit v1.2.3