diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
9 files changed, 275 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash.rb b/activesupport/lib/active_support/core_ext/hash.rb new file mode 100644 index 0000000000..e899d3e1e2 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash.rb @@ -0,0 +1,7 @@ +require File.dirname(__FILE__) + '/hash/keys' +require File.dirname(__FILE__) + '/hash/indifferent_access' + +class Hash #:nodoc: + include ActiveSupport::CoreExtensions::Hash::Keys + include ActiveSupport::CoreExtensions::Hash::IndifferentAccess +end diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb new file mode 100644 index 0000000000..2353cfaf3b --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -0,0 +1,38 @@ +class HashWithIndifferentAccess < Hash + def initialize(constructor) + if constructor.is_a?(Hash) + super() + update(constructor.symbolize_keys) + else + super(constructor) + end + end + + alias_method :regular_reader, :[] unless method_defined?(:regular_reader) + + def [](key) + case key + when Symbol: regular_reader(key) || regular_reader(key.to_s) + when String: regular_reader(key) || regular_reader(key.to_sym) + else regular_reader(key) + end + end + + alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) + + def []=(key, value) + regular_writer(key.is_a?(String) ? key.to_sym : key, value) + end +end + +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + module IndifferentAccess + def with_indifferent_access + HashWithIndifferentAccess.new(self) + end + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb new file mode 100644 index 0000000000..8725138856 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -0,0 +1,53 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + module Keys + # Return a new hash with all keys converted to strings. + def stringify_keys + inject({}) do |options, (key, value)| + options[key.to_s] = value + options + end + end + + # Destructively convert all keys to strings. + def stringify_keys! + keys.each do |key| + unless key.class.to_s == "String" # weird hack to make the tests run when string_ext_test.rb is also running + self[key.to_s] = self[key] + delete(key) + end + end + self + end + + # Return a new hash with all keys converted to symbols. + def symbolize_keys + inject({}) do |options, (key, value)| + options[key.to_sym] = value + options + end + end + + # Destructively convert all keys to symbols. + def symbolize_keys! + keys.each do |key| + unless key.is_a?(Symbol) + self[key.to_sym] = self[key] + delete(key) + end + end + self + end + + alias_method :to_options, :symbolize_keys + alias_method :to_options!, :symbolize_keys! + + def assert_valid_keys(valid_keys) + unknown_keys = keys - valid_keys + raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty? + end + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/numeric.rb b/activesupport/lib/active_support/core_ext/numeric.rb new file mode 100644 index 0000000000..88fead7aa4 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/numeric.rb @@ -0,0 +1,7 @@ +require File.dirname(__FILE__) + '/numeric/time' +require File.dirname(__FILE__) + '/numeric/bytes' + +class Numeric #:nodoc: + include ActiveSupport::CoreExtensions::Numeric::Time + include ActiveSupport::CoreExtensions::Numeric::Bytes +end diff --git a/activesupport/lib/active_support/core_ext/numeric/bytes.rb b/activesupport/lib/active_support/core_ext/numeric/bytes.rb new file mode 100644 index 0000000000..98e5e13abb --- /dev/null +++ b/activesupport/lib/active_support/core_ext/numeric/bytes.rb @@ -0,0 +1,33 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Numeric #:nodoc: + # Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes + module Bytes + def bytes + self + end + alias :byte :bytes + + def kilobytes + self * 1024 + end + alias :kilobyte :kilobytes + + def megabytes + self * 1024.kilobytes + end + alias :megabyte :megabytes + + def gigabytes + self * 1024.megabytes + end + alias :gigabyte :gigabytes + + def terabytes + self * 1024.gigabytes + end + alias :terabyte :terabytes + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/numeric/time.rb b/activesupport/lib/active_support/core_ext/numeric/time.rb new file mode 100644 index 0000000000..43c0425b00 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/numeric/time.rb @@ -0,0 +1,59 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Numeric #:nodoc: + # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years + module Time + def minutes + self * 60 + end + alias :minute :minutes + + def hours + self * 60.minutes + end + alias :hour :hours + + def days + self * 24.hours + end + alias :day :days + + def weeks + self * 7.days + end + alias :week :weeks + + def fortnights + self * 2.weeks + end + alias :fortnight :fortnights + + def months + self * 30.days + end + alias :month :months + + def years + self * 365.days + end + alias :year :years + + # Reads best without arguments: 10.minutes.ago + def ago(time = ::Time.now) + time - self + end + + # Reads best with argument: 10.minutes.until(time) + alias :until :ago + + # Reads best with argument: 10.minutes.since(time) + def since(time = ::Time.now) + time + self + end + + # Reads best without arguments: 10.minutes.from_now + alias :from_now :since + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/object_and_class.rb b/activesupport/lib/active_support/core_ext/object_and_class.rb new file mode 100644 index 0000000000..59a463ae29 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object_and_class.rb @@ -0,0 +1,24 @@ +class Object #:nodoc: + def remove_subclasses_of(superclass) + subclasses_of(superclass).each { |subclass| Object.send(:remove_const, subclass) rescue nil } + end + + def subclasses_of(superclass) + subclasses = [] + ObjectSpace.each_object(Class) do |k| + next if !k.ancestors.include?(superclass) || superclass == k || k.to_s.include?("::") || subclasses.include?(k.to_s) + subclasses << k.to_s + end + subclasses + end +end + +class Class #:nodoc: + def remove_subclasses + Object.remove_subclasses_of(self) + end + + def subclasses + Object.subclasses_of(self) + end +end diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb new file mode 100644 index 0000000000..6d554b483c --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -0,0 +1,5 @@ +require File.dirname(__FILE__) + '/string/inflections' + +class String #:nodoc: + include ActiveSupport::CoreExtensions::String::Inflections +end diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb new file mode 100644 index 0000000000..aa4ff3a74d --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -0,0 +1,49 @@ +require File.dirname(__FILE__) + '/../../inflector' +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module String #:nodoc: + # Makes it possible to do "posts".singularize that returns "post" and "MegaCoolClass".underscore that returns "mega_cool_class". + module Inflections + def pluralize + Inflector.pluralize(self) + end + + def singularize + Inflector.singularize(self) + end + + def camelize + Inflector.camelize(self) + end + + def underscore + Inflector.underscore(self) + end + + def demodulize + Inflector.demodulize(self) + end + + def tableize + Inflector.tableize(self) + end + + def classify + Inflector.classify(self) + end + + def humanize + Inflector.humanize(self) + end + + def foreign_key(separate_class_name_and_id_with_underscore = true) + Inflector.foreign_key(self, separate_class_name_and_id_with_underscore) + end + + def constantize + Inflector.constantize(self) + end + end + end + end +end |