diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-29 21:03:21 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-29 21:03:21 +0000 |
commit | bd323b3c99437cf1a50d7b4c23a64f296859c56a (patch) | |
tree | 943347b52f12f8df492c0864e7ba61318e459f63 /activesupport/lib | |
parent | 1b0da48fe98abc67b56bc76f2af59a90198b21cc (diff) | |
download | rails-bd323b3c99437cf1a50d7b4c23a64f296859c56a.tar.gz rails-bd323b3c99437cf1a50d7b4c23a64f296859c56a.tar.bz2 rails-bd323b3c99437cf1a50d7b4c23a64f296859c56a.zip |
Moved support from both Action Pack and Active Record into a separate module called Active Support that can be included using svn:externals in both
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@273 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/class_attribute_accessors.rb | 57 | ||||
-rw-r--r-- | activesupport/lib/class_inheritable_attributes.rb | 41 | ||||
-rw-r--r-- | activesupport/lib/clean_logger.rb | 10 | ||||
-rw-r--r-- | activesupport/lib/dependencies.rb | 65 | ||||
-rw-r--r-- | activesupport/lib/inflector.rb | 80 | ||||
-rw-r--r-- | activesupport/lib/misc.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/module_attribute_accessors.rb | 57 |
7 files changed, 316 insertions, 0 deletions
diff --git a/activesupport/lib/class_attribute_accessors.rb b/activesupport/lib/class_attribute_accessors.rb new file mode 100644 index 0000000000..786dcf98cb --- /dev/null +++ b/activesupport/lib/class_attribute_accessors.rb @@ -0,0 +1,57 @@ +# Extends the class object with class and instance accessors for class attributes, +# just like the native attr* accessors for instance attributes. +class Class # :nodoc: + def cattr_reader(*syms) + syms.each do |sym| + class_eval <<-EOS + if ! defined? @@#{sym.id2name} + @@#{sym.id2name} = nil + end + + def self.#{sym.id2name} + @@#{sym} + end + + def #{sym.id2name} + @@#{sym} + end + + def call_#{sym.id2name} + case @@#{sym.id2name} + when Symbol then send(@@#{sym}) + when Proc then @@#{sym}.call(self) + when String then @@#{sym} + else nil + end + end + EOS + end + end + + def cattr_writer(*syms) + syms.each do |sym| + class_eval <<-EOS + if ! defined? @@#{sym.id2name} + @@#{sym.id2name} = nil + end + + def self.#{sym.id2name}=(obj) + @@#{sym.id2name} = obj + end + + def self.set_#{sym.id2name}(obj) + @@#{sym.id2name} = obj + end + + def #{sym.id2name}=(obj) + @@#{sym} = obj + end + EOS + end + end + + def cattr_accessor(*syms) + cattr_reader(*syms) + cattr_writer(*syms) + end +end
\ No newline at end of file diff --git a/activesupport/lib/class_inheritable_attributes.rb b/activesupport/lib/class_inheritable_attributes.rb new file mode 100644 index 0000000000..39312e8d5b --- /dev/null +++ b/activesupport/lib/class_inheritable_attributes.rb @@ -0,0 +1,41 @@ +# Allows attributes to be shared within an inheritance hierarchy, but where each descendant gets a copy of +# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements +# to, for example, an array without those additions being shared with either their parent, siblings, or +# children, which is unlike the regular class-level attributes that are shared across the entire hierarchy. +module ClassInheritableAttributes # :nodoc: + def self.append_features(base) + super + base.extend(ClassMethods) + end + + module ClassMethods # :nodoc: + @@classes ||= {} + + def inheritable_attributes + @@classes[self] ||= {} + end + + def write_inheritable_attribute(key, value) + inheritable_attributes[key] = value + end + + def write_inheritable_array(key, elements) + write_inheritable_attribute(key, []) if read_inheritable_attribute(key).nil? + write_inheritable_attribute(key, read_inheritable_attribute(key) + elements) + end + + def read_inheritable_attribute(key) + inheritable_attributes[key] + end + + def reset_inheritable_attributes + inheritable_attributes.clear + end + + private + def inherited(child) + @@classes[child] = inheritable_attributes.dup + end + + end +end diff --git a/activesupport/lib/clean_logger.rb b/activesupport/lib/clean_logger.rb new file mode 100644 index 0000000000..1a36562892 --- /dev/null +++ b/activesupport/lib/clean_logger.rb @@ -0,0 +1,10 @@ +require 'logger' + +class Logger #:nodoc: + private + remove_const "Format" + Format = "%s\n" + def format_message(severity, timestamp, msg, progname) + Format % [msg] + end +end
\ No newline at end of file diff --git a/activesupport/lib/dependencies.rb b/activesupport/lib/dependencies.rb new file mode 100644 index 0000000000..e3a50151bd --- /dev/null +++ b/activesupport/lib/dependencies.rb @@ -0,0 +1,65 @@ +require 'action_controller/support/module_attribute_accessors' + +module Dependencies + extend self + + @@loaded = [ ] + mattr_accessor :loaded + + @@mechanism = :load + mattr_accessor :mechanism + + def depend_on(file_name, swallow_load_errors = false) + begin + loaded << require_or_load(file_name) if !loaded.include?(file_name) + rescue LoadError + raise unless swallow_load_errors + end + end + + def associate_with(file_name) + depend_on(file_name, true) + end + + def reload + loaded.each do |file_name| + begin + silence_warnings { load("#{file_name}.rb") } + rescue LoadError + # The association didn't reside in its own file, so we assume it was required by other means + end + end + end + + def clear + self.loaded = [ ] + end + + private + def require_or_load(file_name) + mechanism == :load ? silence_warnings { load("#{file_name}.rb") } : require(file_name) + return file_name + end +end + +Object.send(:define_method, :require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency) +Object.send(:define_method, :require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association) + +class Object + class << self + # Use const_missing to autoload associations so we don't have to + # require_association when using single-table inheritance. + unless respond_to?(:pre_dependency_const_missing) + alias_method :pre_dependency_const_missing, :const_missing + + def const_missing(class_id) + begin + require_dependency(Inflector.underscore(Inflector.demodulize(class_id.to_s))) + return Object.const_get(class_id) if Object.const_defined?(class_id) + rescue LoadError + pre_dependency_const_missing(class_id) + end + end + end + end +end diff --git a/activesupport/lib/inflector.rb b/activesupport/lib/inflector.rb new file mode 100644 index 0000000000..5fddf9d09c --- /dev/null +++ b/activesupport/lib/inflector.rb @@ -0,0 +1,80 @@ +# The Inflector transforms words from singular to plural, class names to table names, modulized class names to ones without, +# and class names to foreign keys. +module Inflector + extend self + + def pluralize(word) + result = word.dup + plural_rules.each do |(rule, replacement)| + break if result.gsub!(rule, replacement) + end + return result + end + + def singularize(word) + result = word.dup + singular_rules.each do |(rule, replacement)| + break if result.gsub!(rule, replacement) + end + return result + end + + def camelize(lower_case_and_underscored_word) + lower_case_and_underscored_word.gsub(/(^|_)(.)/){$2.upcase} + end + + def underscore(camel_cased_word) + camel_cased_word.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase + end + + def demodulize(class_name_in_module) + class_name_in_module.gsub(/^.*::/, '') + end + + def tableize(class_name) + pluralize(underscore(class_name)) + end + + def classify(table_name) + camelize(singularize(table_name)) + end + + def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) + Inflector.underscore(Inflector.demodulize(class_name)) + + (separate_class_name_and_id_with_underscore ? "_id" : "id") + end + + private + def plural_rules #:doc: + [ + [/(x|ch|ss)$/, '\1es'], # search, switch, fix, box, process, address + [/([^aeiouy]|qu)ies$/, '\1y'], + [/([^aeiouy]|qu)y$/, '\1ies'], # query, ability, agency + [/(?:([^f])fe|([lr])f)$/, '\1\2ves'], # half, safe, wife + [/sis$/, 'ses'], # basis, diagnosis + [/([ti])um$/, '\1a'], # datum, medium + [/person$/, 'people'], # person, salesperson + [/man$/, 'men'], # man, woman, spokesman + [/child$/, 'children'], # child + [/s$/, 's'], # no change (compatibility) + [/$/, 's'] + ] + end + + def singular_rules #:doc: + [ + [/(x|ch|ss)es$/, '\1'], + [/movies$/, 'movie'], + [/([^aeiouy]|qu)ies$/, '\1y'], + [/([lr])ves$/, '\1f'], + [/([^f])ves$/, '\1fe'], + [/(analy|ba|diagno|parenthe|progno|synop|the)ses$/, '\1sis'], + [/([ti])a$/, '\1um'], + [/people$/, 'person'], + [/men$/, 'man'], + [/status$/, 'status'], + [/children$/, 'child'], + [/s$/, ''] + ] + end +end
\ No newline at end of file diff --git a/activesupport/lib/misc.rb b/activesupport/lib/misc.rb new file mode 100644 index 0000000000..db842f6061 --- /dev/null +++ b/activesupport/lib/misc.rb @@ -0,0 +1,6 @@ +def silence_warnings + old_verbose, $VERBOSE = $VERBOSE, nil + result = yield + $VERBOSE = old_verbose + return result +end diff --git a/activesupport/lib/module_attribute_accessors.rb b/activesupport/lib/module_attribute_accessors.rb new file mode 100644 index 0000000000..3d466e4493 --- /dev/null +++ b/activesupport/lib/module_attribute_accessors.rb @@ -0,0 +1,57 @@ +# Extends the module object with module and instance accessors for class attributes, +# just like the native attr* accessors for instance attributes. +class Module # :nodoc: + def mattr_reader(*syms) + syms.each do |sym| + class_eval <<-EOS + if ! defined? @@#{sym.id2name} + @@#{sym.id2name} = nil + end + + def self.#{sym.id2name} + @@#{sym} + end + + def #{sym.id2name} + @@#{sym} + end + + def call_#{sym.id2name} + case @@#{sym.id2name} + when Symbol then send(@@#{sym}) + when Proc then @@#{sym}.call(self) + when String then @@#{sym} + else nil + end + end + EOS + end + end + + def mattr_writer(*syms) + syms.each do |sym| + class_eval <<-EOS + if ! defined? @@#{sym.id2name} + @@#{sym.id2name} = nil + end + + def self.#{sym.id2name}=(obj) + @@#{sym.id2name} = obj + end + + def self.set_#{sym.id2name}(obj) + @@#{sym.id2name} = obj + end + + def #{sym.id2name}=(obj) + @@#{sym} = obj + end + EOS + end + end + + def mattr_accessor(*syms) + mattr_reader(*syms) + mattr_writer(*syms) + end +end
\ No newline at end of file |