Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce ActiveSupport.core_ext Integer, %w(conversions time etc)
* Convert some extension modules to simply reopening the class * Remove deprecated Float time extensions * Fold Base64 extension into ActiveSupport::Base64 since stdlib Base64 is gone
- Loading branch information
Showing
33 changed files
with
163 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,4 @@ | |||
Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].sort.each do |path| | require 'active_support/core_ext/util' | ||
filename = File.basename(path, '.rb') | Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.each do |path| | ||
require "active_support/core_ext/#{filename}" | require "active_support/core_ext/#{File.basename(path, '.rb')}" | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,15 +1,3 @@ | |||
require 'active_support/core_ext/array/access' | require 'active_support/core_ext/util' | ||
require 'active_support/core_ext/array/conversions' | require 'active_support/core_ext/array/wrap' | ||
require 'active_support/core_ext/array/extract_options' | ActiveSupport.core_ext Array, %w(access conversions extract_options grouping random_access) | ||
require 'active_support/core_ext/array/grouping' | |||
require 'active_support/core_ext/array/random_access' | |||
require 'active_support/core_ext/array/wrapper' | |||
|
|||
class Array #:nodoc: | |||
include ActiveSupport::CoreExtensions::Array::Access | |||
include ActiveSupport::CoreExtensions::Array::Conversions | |||
include ActiveSupport::CoreExtensions::Array::ExtractOptions | |||
include ActiveSupport::CoreExtensions::Array::Grouping | |||
include ActiveSupport::CoreExtensions::Array::RandomAccess | |||
extend ActiveSupport::CoreExtensions::Array::Wrapper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
class Array | |||
# Wraps the object in an Array unless it's an Array. Converts the | |||
# object to an Array using #to_ary if it implements that. | |||
def self.wrap(object) | |||
case object | |||
when nil | |||
[] | |||
when self | |||
object | |||
else | |||
if object.respond_to?(:to_ary) | |||
object.to_ary | |||
else | |||
[object] | |||
end | |||
end | |||
end | |||
end |
24 changes: 0 additions & 24 deletions
24
activesupport/lib/active_support/core_ext/array/wrapper.rb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
activesupport/lib/active_support/core_ext/base64/encoding.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
require 'active_support/core_ext/util' | |||
require 'bigdecimal' | |||
ActiveSupport.core_ext BigDecimal, %w(conversions) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1 @@ | |||
require 'active_support/core_ext/cgi/escape_skipping_slashes' | require 'active_support/core_ext/cgi/escape_skipping_slashes' | ||
|
|||
class CGI #:nodoc: | |||
extend ActiveSupport::CoreExtensions::CGI::EscapeSkippingSlashes | |||
end |
34 changes: 14 additions & 20 deletions
34
activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,23 +1,17 @@ | |||
module ActiveSupport #:nodoc: | class CGI #:nodoc: | ||
module CoreExtensions #:nodoc: | if RUBY_VERSION >= '1.9' | ||
module CGI #:nodoc: | def self.escape_skipping_slashes(str) | ||
module EscapeSkippingSlashes #:nodoc: | str = str.join('/') if str.respond_to? :join | ||
if RUBY_VERSION >= '1.9' | str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do | ||
def escape_skipping_slashes(str) | "%#{$1.unpack('H2' * $1.bytesize).join('%').upcase}" | ||
str = str.join('/') if str.respond_to? :join | end.tr(' ', '+') | ||
str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do | end | ||
"%#{$1.unpack('H2' * $1.bytesize).join('%').upcase}" | else | ||
end.tr(' ', '+') | def self.escape_skipping_slashes(str) | ||
end | str = str.join('/') if str.respond_to? :join | ||
else | str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do | ||
def escape_skipping_slashes(str) | "%#{$1.unpack('H2').first.upcase}" | ||
str = str.join('/') if str.respond_to? :join | end.tr(' ', '+') | ||
str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do | |||
"%#{$1.unpack('H2').first.upcase}" | |||
end.tr(' ', '+') | |||
end | |||
end | |||
end | |||
end | end | ||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,10 +1,3 @@ | |||
require 'active_support/core_ext/util' | |||
require 'date' | require 'date' | ||
require 'active_support/core_ext/date/behavior' | ActiveSupport.core_ext Date, %w(behavior calculations conversions) | ||
require 'active_support/core_ext/date/calculations' | |||
require 'active_support/core_ext/date/conversions' | |||
|
|||
class Date#:nodoc: | |||
include ActiveSupport::CoreExtensions::Date::Behavior | |||
include ActiveSupport::CoreExtensions::Date::Calculations | |||
include ActiveSupport::CoreExtensions::Date::Conversions | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,12 +1,11 @@ | |||
require 'active_support/core_ext/util' | |||
require 'date' | require 'date' | ||
require 'active_support/core_ext/time/behavior' | require 'active_support/core_ext/time/behavior' | ||
require 'active_support/core_ext/time/zones' | require 'active_support/core_ext/time/zones' | ||
require 'active_support/core_ext/date_time/calculations' | |||
require 'active_support/core_ext/date_time/conversions' | |||
|
|
||
class DateTime | class DateTime | ||
include ActiveSupport::CoreExtensions::Time::Behavior | include ActiveSupport::CoreExtensions::Time::Behavior | ||
include ActiveSupport::CoreExtensions::Time::Zones | include ActiveSupport::CoreExtensions::Time::Zones | ||
include ActiveSupport::CoreExtensions::DateTime::Calculations | |||
include ActiveSupport::CoreExtensions::DateTime::Conversions | |||
end | end | ||
|
|||
ActiveSupport.core_ext DateTime, %w(calculations conversions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1 @@ | |||
require 'active_support/core_ext/file/atomic' | require 'active_support/core_ext/file/atomic' | ||
|
|||
class File #:nodoc: | |||
extend ActiveSupport::CoreExtensions::File::Atomic | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,46 +1,40 @@ | |||
module ActiveSupport #:nodoc: | class File | ||
module CoreExtensions #:nodoc: | # Write to a file atomically. Useful for situations where you don't | ||
module File #:nodoc: | # want other processes or threads to see half-written files. | ||
module Atomic | # | ||
# Write to a file atomically. Useful for situations where you don't | # File.atomic_write("important.file") do |file| | ||
# want other processes or threads to see half-written files. | # file.write("hello") | ||
# | # end | ||
# File.atomic_write("important.file") do |file| | # | ||
# file.write("hello") | # If your temp directory is not on the same filesystem as the file you're | ||
# end | # trying to write, you can provide a different temporary directory. | ||
# | # | ||
# If your temp directory is not on the same filesystem as the file you're | # File.atomic_write("/data/something.important", "/data/tmp") do |f| | ||
# trying to write, you can provide a different temporary directory. | # file.write("hello") | ||
# | # end | ||
# File.atomic_write("/data/something.important", "/data/tmp") do |f| | def self.atomic_write(file_name, temp_dir = Dir.tmpdir) | ||
# file.write("hello") | require 'tempfile' unless defined?(Tempfile) | ||
# end | |||
def atomic_write(file_name, temp_dir = Dir.tmpdir) | |||
require 'tempfile' unless defined?(Tempfile) | |||
|
|
||
temp_file = Tempfile.new(basename(file_name), temp_dir) | temp_file = Tempfile.new(basename(file_name), temp_dir) | ||
yield temp_file | yield temp_file | ||
temp_file.close | temp_file.close | ||
|
|
||
begin | begin | ||
# Get original file permissions | # Get original file permissions | ||
old_stat = stat(file_name) | old_stat = stat(file_name) | ||
rescue Errno::ENOENT | rescue Errno::ENOENT | ||
# No old permissions, write a temp file to determine the defaults | # No old permissions, write a temp file to determine the defaults | ||
check_name = join(dirname(file_name), ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}") | check_name = join(dirname(file_name), ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}") | ||
open(check_name, "w") { } | open(check_name, "w") { } | ||
old_stat = stat(check_name) | old_stat = stat(check_name) | ||
unlink(check_name) | unlink(check_name) | ||
end | end | ||
|
|
||
# Overwrite original file with temp file | # Overwrite original file with temp file | ||
rename(temp_file.path, file_name) | rename(temp_file.path, file_name) | ||
|
|
||
# Set correct permissions on new file | # Set correct permissions on new file | ||
chown(old_stat.uid, old_stat.gid, file_name) | chown(old_stat.uid, old_stat.gid, file_name) | ||
chmod(old_stat.mode, file_name) | chmod(old_stat.mode, file_name) | ||
end | |||
end | |||
end | |||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,7 +1 @@ | |||
require 'active_support/core_ext/float/rounding' | require 'active_support/core_ext/float/rounding' | ||
require 'active_support/core_ext/float/time' | |||
|
|||
class Float #:nodoc: | |||
include ActiveSupport::CoreExtensions::Float::Rounding | |||
include ActiveSupport::CoreExtensions::Float::Time | |||
end |
34 changes: 14 additions & 20 deletions
34
activesupport/lib/active_support/core_ext/float/rounding.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,24 +1,18 @@ | |||
module ActiveSupport #:nodoc: | class Float | ||
module CoreExtensions #:nodoc: | remove_method :round | ||
module Float #:nodoc: | |||
module Rounding | |||
def self.included(base) #:nodoc: | |||
base.class_eval do | |||
alias_method :round_without_precision, :round | |||
alias_method :round, :round_with_precision | |||
end | |||
end | |||
|
|
||
# Rounds the float with the specified precision. | # Rounds the float with the specified precision. | ||
# | # | ||
# x = 1.337 | # x = 1.337 | ||
# x.round # => 1 | # x.round # => 1 | ||
# x.round(1) # => 1.3 | # x.round(1) # => 1.3 | ||
# x.round(2) # => 1.34 | # x.round(2) # => 1.34 | ||
def round_with_precision(precision = nil) | def round(precision = nil) | ||
precision.nil? ? round_without_precision : (self * (10 ** precision)).round / (10 ** precision).to_f | if precision | ||
end | magnitude = 10.0 ** precision | ||
end | (self * magnitude).round / magnitude | ||
else | |||
super() | |||
end | end | ||
end | end | ||
end | end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,14 +1,2 @@ | |||
%w(keys indifferent_access deep_merge reverse_merge conversions diff slice except).each do |ext| | require 'active_support/core_ext/util' | ||
require "active_support/core_ext/hash/#{ext}" | ActiveSupport.core_ext Hash, %w(keys indifferent_access deep_merge reverse_merge conversions diff slice except) | ||
end | |||
|
|||
class Hash #:nodoc: | |||
include ActiveSupport::CoreExtensions::Hash::Keys | |||
include ActiveSupport::CoreExtensions::Hash::IndifferentAccess | |||
include ActiveSupport::CoreExtensions::Hash::DeepMerge | |||
include ActiveSupport::CoreExtensions::Hash::ReverseMerge | |||
include ActiveSupport::CoreExtensions::Hash::Conversions | |||
include ActiveSupport::CoreExtensions::Hash::Diff | |||
include ActiveSupport::CoreExtensions::Hash::Slice | |||
include ActiveSupport::CoreExtensions::Hash::Except | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,9 +1,2 @@ | |||
require 'active_support/core_ext/integer/even_odd' | require 'active_support/core_ext/util' | ||
require 'active_support/core_ext/integer/inflections' | ActiveSupport.core_ext Integer, %w(even_odd inflections time) | ||
require 'active_support/core_ext/integer/time' | |||
|
|||
class Integer #:nodoc: | |||
include ActiveSupport::CoreExtensions::Integer::EvenOdd | |||
include ActiveSupport::CoreExtensions::Integer::Inflections | |||
include ActiveSupport::CoreExtensions::Integer::Time | |||
end |
Oops, something went wrong.