aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute_accessors.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/class/delegating_attributes.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb101
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb3
-rw-r--r--activesupport/lib/active_support/core_ext/module.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/module/attribute_accessors.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/module/setup.rb26
-rw-r--r--activesupport/lib/active_support/core_ext/proc.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/string/access.rb34
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb14
10 files changed, 162 insertions, 41 deletions
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 c795871474..75e481fc54 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -10,7 +10,7 @@ class Class
def cattr_reader(*syms)
syms.flatten.each do |sym|
next if sym.is_a?(Hash)
- class_eval(<<-EOS, __FILE__, __LINE__)
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@hair_colors
@@#{sym} = nil # @@hair_colors = nil
end # end
@@ -29,7 +29,7 @@ class Class
def cattr_writer(*syms)
options = syms.extract_options!
syms.flatten.each do |sym|
- class_eval(<<-EOS, __FILE__, __LINE__)
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@hair_colors
@@#{sym} = nil # @@hair_colors = nil
end # 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 c81af68034..da798c67e7 100644
--- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
+++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
@@ -2,7 +2,7 @@ class Class
def superclass_delegating_reader(*names)
class_name_to_stop_searching_on = superclass.name.blank? ? "Object" : superclass.name
names.each do |name|
- class_eval(<<-EOS, __FILE__, __LINE__)
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def self.#{name} # def self.only_reader
if defined?(@#{name}) # if defined?(@only_reader)
@#{name} # @only_reader
@@ -26,10 +26,10 @@ class Class
def superclass_delegating_writer(*names)
names.each do |name|
- class_eval <<-EOS
- def self.#{name}=(value) # def self.only_writer=(value)
- @#{name} = value # @only_writer = value
- end # 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 1794afe77c..2f18666ab9 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} # def self.before_add_for_comments
- read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:before_add_for_comments)
- end # end
- #
- def #{sym} # def before_add_for_comments
- self.class.#{sym} # self.class.before_add_for_comments
- end # 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,7 +25,7 @@ class Class # :nodoc:
def class_inheritable_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval <<-EOS
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def self.#{sym}=(obj) # def self.color=(obj)
write_inheritable_attribute(:#{sym}, obj) # write_inheritable_attribute(:color, obj)
end # end
@@ -42,7 +42,7 @@ class Class # :nodoc:
def class_inheritable_array_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval <<-EOS
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def self.#{sym}=(obj) # def self.levels=(obj)
write_inheritable_array(:#{sym}, obj) # write_inheritable_array(:levels, obj)
end # end
@@ -59,7 +59,7 @@ class Class # :nodoc:
def class_inheritable_hash_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval <<-EOS
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def self.#{sym}=(obj) # def self.nicknames=(obj)
write_inheritable_hash(:#{sym}, obj) # write_inheritable_hash(:nicknames, obj)
end # end
@@ -138,3 +138,82 @@ class Class # :nodoc:
alias inherited_without_inheritable_attributes inherited
alias inherited inherited_with_inheritable_attributes
end
+
+class Class
+ # Defines class-level inheritable attribute reader. Attributes are available to subclasses,
+ # each subclass has a copy of parent's attribute.
+ #
+ # @param *syms<Array[#to_s]> Array of attributes to define inheritable reader for.
+ # @return <Array[#to_s]> Array of attributes converted into inheritable_readers.
+ #
+ # @api public
+ #
+ # @todo Do we want to block instance_reader via :instance_reader => false
+ # @todo It would be preferable that we do something with a Hash passed in
+ # (error out or do the same as other methods above) instead of silently
+ # moving on). In particular, this makes the return value of this function
+ # less useful.
+ def extlib_inheritable_reader(*ivars)
+ instance_reader = ivars.pop[:reader] if ivars.last.is_a?(Hash)
+
+ ivars.each do |ivar|
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def self.#{ivar}
+ return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
+ ivar = superclass.#{ivar}
+ return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
+ @#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) ? ivar.dup : ivar
+ end
+ RUBY
+ unless instance_reader == false
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{ivar}
+ self.class.#{ivar}
+ end
+ RUBY
+ end
+ end
+ end
+
+ # Defines class-level inheritable attribute writer. Attributes are available to subclasses,
+ # each subclass has a copy of parent's attribute.
+ #
+ # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
+ # define inheritable writer for.
+ # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
+ # @return <Array[#to_s]> An Array of the attributes that were made into inheritable writers.
+ #
+ # @api public
+ #
+ # @todo We need a style for class_eval <<-HEREDOC. I'd like to make it
+ # class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere.
+ def extlib_inheritable_writer(*ivars)
+ instance_writer = ivars.pop[:writer] if ivars.last.is_a?(Hash)
+ ivars.each do |ivar|
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def self.#{ivar}=(obj)
+ @#{ivar} = obj
+ end
+ RUBY
+ unless instance_writer == false
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{ivar}=(obj) self.class.#{ivar} = obj end
+ RUBY
+ end
+ end
+ end
+
+ # Defines class-level inheritable attribute accessor. Attributes are available to subclasses,
+ # each subclass has a copy of parent's attribute.
+ #
+ # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
+ # define inheritable accessor for.
+ # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
+ # @return <Array[#to_s]> An Array of attributes turned into inheritable accessors.
+ #
+ # @api public
+ def extlib_inheritable_accessor(*syms)
+ extlib_inheritable_reader(*syms)
+ extlib_inheritable_writer(*syms)
+ end
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index 45e56381a9..fa171720f9 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -27,8 +27,7 @@ class Hash
"FalseClass" => "boolean",
"Date" => "date",
"DateTime" => "datetime",
- "Time" => "datetime",
- "ActiveSupport::TimeWithZone" => "datetime"
+ "Time" => "datetime"
} unless defined?(XML_TYPE_NAMES)
XML_FORMATTING = {
diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb
index f35a554364..c37c9badca 100644
--- a/activesupport/lib/active_support/core_ext/module.rb
+++ b/activesupport/lib/active_support/core_ext/module.rb
@@ -9,6 +9,7 @@ require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/loading'
require 'active_support/core_ext/module/model_naming'
require 'active_support/core_ext/module/synchronization'
+require 'active_support/core_ext/module/setup'
module ActiveSupport
module CoreExtensions
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 62bc64d137..131b512944 100644
--- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
@@ -4,7 +4,7 @@ class Module
def mattr_reader(*syms)
syms.extract_options!
syms.each do |sym|
- class_eval(<<-EOS, __FILE__, __LINE__)
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@pagination_options
@@#{sym} = nil # @@pagination_options = nil
end # end
@@ -23,7 +23,7 @@ class Module
def mattr_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval(<<-EOS, __FILE__, __LINE__)
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@pagination_options
@@#{sym} = nil # @@pagination_options = nil
end # end
diff --git a/activesupport/lib/active_support/core_ext/module/setup.rb b/activesupport/lib/active_support/core_ext/module/setup.rb
new file mode 100644
index 0000000000..e6dfd0cf56
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/module/setup.rb
@@ -0,0 +1,26 @@
+class Module
+ attr_accessor :_setup_block
+ attr_accessor :_dependencies
+
+ def setup(&blk)
+ @_setup_block = blk
+ end
+
+ def use(mod)
+ return if self < mod
+
+ (mod._dependencies || []).each do |dep|
+ use dep
+ end
+ # raise "Circular dependencies" if self < mod
+ include mod
+ extend mod.const_get("ClassMethods") if mod.const_defined?("ClassMethods")
+ class_eval(&mod._setup_block) if mod._setup_block
+ end
+
+ def depends_on(mod)
+ return if self < mod
+ @_dependencies ||= []
+ @_dependencies << mod
+ end
+end \ No newline at end of file
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/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb
index 7fb21fa4dd..e806b321f1 100644
--- a/activesupport/lib/active_support/core_ext/string/access.rb
+++ b/activesupport/lib/active_support/core_ext/string/access.rb
@@ -41,9 +41,15 @@ module ActiveSupport #:nodoc:
# "hello".first(2) # => "he"
# "hello".first(10) # => "hello"
def first(limit = 1)
- mb_chars[0..(limit - 1)].to_s
+ if limit == 0
+ ''
+ elsif limit >= size
+ self
+ else
+ mb_chars[0...limit].to_s
+ end
end
-
+
# Returns the last character of the string or the last +limit+ characters.
#
# Examples:
@@ -51,7 +57,13 @@ module ActiveSupport #:nodoc:
# "hello".last(2) # => "lo"
# "hello".last(10) # => "hello"
def last(limit = 1)
- (mb_chars[(-limit)..-1] || self).to_s
+ if limit == 0
+ ''
+ elsif limit >= size
+ self
+ else
+ mb_chars[(-limit)..-1].to_s
+ end
end
end
else
@@ -69,11 +81,23 @@ module ActiveSupport #:nodoc:
end
def first(limit = 1)
- self[0..(limit - 1)]
+ if limit == 0
+ ''
+ elsif limit >= size
+ self
+ else
+ to(limit - 1)
+ end
end
def last(limit = 1)
- from(-limit) || self
+ if limit == 0
+ ''
+ elsif limit >= size
+ self
+ else
+ from(-limit)
+ end
end
end
end
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 608e3ad29c..b73c3b2c9b 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -101,19 +101,11 @@ class Time
since(-seconds)
end
- # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
- # the Numeric extension.
+ # Returns a new Time representing the time a number of seconds since the instance time
def since(seconds)
- f = seconds.since(self)
- if ActiveSupport::Duration === seconds
- f
- else
- initial_dst = dst? ? 1 : 0
- final_dst = f.dst? ? 1 : 0
- (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
- end
+ self + seconds
rescue
- self.to_datetime.since(seconds)
+ to_datetime.since(seconds)
end
alias :in :since