Convert String conversions, filters, starts/ends_with, and multibyte extension modules to class reopen
This commit is contained in:
parent
fce68161a4
commit
83a89b8e85
@ -1,22 +1,19 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'active_support/core_ext/string/inflections'
|
||||
require 'active_support/core_ext/string/conversions'
|
||||
require 'active_support/core_ext/string/access'
|
||||
require 'active_support/core_ext/string/starts_ends_with'
|
||||
require 'active_support/core_ext/string/iterators'
|
||||
require 'active_support/core_ext/string/multibyte'
|
||||
require 'active_support/core_ext/string/xchar'
|
||||
require 'active_support/core_ext/string/filters'
|
||||
require 'active_support/core_ext/string/multibyte'
|
||||
require 'active_support/core_ext/string/starts_ends_with'
|
||||
|
||||
require 'active_support/core_ext/string/inflections'
|
||||
require 'active_support/core_ext/string/access'
|
||||
require 'active_support/core_ext/string/iterators'
|
||||
require 'active_support/core_ext/string/xchar'
|
||||
require 'active_support/core_ext/string/behavior'
|
||||
|
||||
class String #:nodoc:
|
||||
include ActiveSupport::CoreExtensions::String::Access
|
||||
include ActiveSupport::CoreExtensions::String::Conversions
|
||||
include ActiveSupport::CoreExtensions::String::Filters
|
||||
include ActiveSupport::CoreExtensions::String::Inflections
|
||||
include ActiveSupport::CoreExtensions::String::StartsEndsWith
|
||||
include ActiveSupport::CoreExtensions::String::Iterators
|
||||
include ActiveSupport::CoreExtensions::String::Behavior
|
||||
include ActiveSupport::CoreExtensions::String::Multibyte
|
||||
end
|
||||
|
@ -1,28 +1,21 @@
|
||||
require 'date'
|
||||
|
||||
module ActiveSupport #:nodoc:
|
||||
module CoreExtensions #:nodoc:
|
||||
module String #:nodoc:
|
||||
# Converting strings to other objects
|
||||
module Conversions
|
||||
# 'a'.ord == 'a'[0] for Ruby 1.9 forward compatibility.
|
||||
def ord
|
||||
self[0]
|
||||
end if RUBY_VERSION < '1.9'
|
||||
class String
|
||||
# 'a'.ord == 'a'[0] for Ruby 1.9 forward compatibility.
|
||||
def ord
|
||||
self[0]
|
||||
end if RUBY_VERSION < '1.9'
|
||||
|
||||
# Form can be either :utc (default) or :local.
|
||||
def to_time(form = :utc)
|
||||
::Time.send("#{form}_time", *::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
|
||||
end
|
||||
# Form can be either :utc (default) or :local.
|
||||
def to_time(form = :utc)
|
||||
::Time.send("#{form}_time", *::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
|
||||
end
|
||||
|
||||
def to_date
|
||||
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
|
||||
end
|
||||
def to_date
|
||||
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
|
||||
end
|
||||
|
||||
def to_datetime
|
||||
::DateTime.civil(*::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
|
||||
end
|
||||
end
|
||||
end
|
||||
def to_datetime
|
||||
::DateTime.civil(*::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
|
||||
end
|
||||
end
|
||||
|
@ -1,26 +1,20 @@
|
||||
module ActiveSupport #:nodoc:
|
||||
module CoreExtensions #:nodoc:
|
||||
module String #:nodoc:
|
||||
module Filters
|
||||
# Returns the string, first removing all whitespace on both ends of
|
||||
# the string, and then changing remaining consecutive whitespace
|
||||
# groups into one space each.
|
||||
#
|
||||
# Examples:
|
||||
# %{ Multi-line
|
||||
# string }.squish # => "Multi-line string"
|
||||
# " foo bar \n \t boo".squish # => "foo bar boo"
|
||||
def squish
|
||||
dup.squish!
|
||||
end
|
||||
class String
|
||||
# Returns the string, first removing all whitespace on both ends of
|
||||
# the string, and then changing remaining consecutive whitespace
|
||||
# groups into one space each.
|
||||
#
|
||||
# Examples:
|
||||
# %{ Multi-line
|
||||
# string }.squish # => "Multi-line string"
|
||||
# " foo bar \n \t boo".squish # => "foo bar boo"
|
||||
def squish
|
||||
dup.squish!
|
||||
end
|
||||
|
||||
# Performs a destructive squish. See String#squish.
|
||||
def squish!
|
||||
strip!
|
||||
gsub!(/\s+/, ' ')
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
# Performs a destructive squish. See String#squish.
|
||||
def squish!
|
||||
strip!
|
||||
gsub!(/\s+/, ' ')
|
||||
self
|
||||
end
|
||||
end
|
||||
|
@ -1,80 +1,73 @@
|
||||
# encoding: utf-8
|
||||
|
||||
module ActiveSupport #:nodoc:
|
||||
module CoreExtensions #:nodoc:
|
||||
module String #:nodoc:
|
||||
# Implements multibyte methods for easier access to multibyte characters in a String instance.
|
||||
module Multibyte
|
||||
unless '1.9'.respond_to?(:force_encoding)
|
||||
# == Multibyte proxy
|
||||
#
|
||||
# +mb_chars+ is a multibyte safe proxy for string methods.
|
||||
#
|
||||
# In Ruby 1.8 and older it creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
|
||||
# encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
|
||||
# class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsuled string.
|
||||
#
|
||||
# name = 'Claus Müller'
|
||||
# name.reverse #=> "rell??M sualC"
|
||||
# name.length #=> 13
|
||||
#
|
||||
# name.mb_chars.reverse.to_s #=> "rellüM sualC"
|
||||
# name.mb_chars.length #=> 12
|
||||
#
|
||||
# In Ruby 1.9 and newer +mb_chars+ returns +self+ because String is (mostly) encoding aware. This means that
|
||||
# it becomes easy to run one version of your code on multiple Ruby versions.
|
||||
#
|
||||
# == Method chaining
|
||||
#
|
||||
# All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
|
||||
# method chaining on the result of any of these methods.
|
||||
#
|
||||
# name.mb_chars.reverse.length #=> 12
|
||||
#
|
||||
# == Interoperability and configuration
|
||||
#
|
||||
# The Chars object tries to be as interchangeable with String objects as possible: sorting and comparing between
|
||||
# String and Char work like expected. The bang! methods change the internal string representation in the Chars
|
||||
# object. Interoperability problems can be resolved easily with a +to_s+ call.
|
||||
#
|
||||
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
|
||||
# information about how to change the default Multibyte behaviour see ActiveSupport::Multibyte.
|
||||
def mb_chars
|
||||
if ActiveSupport::Multibyte.proxy_class.wants?(self)
|
||||
ActiveSupport::Multibyte.proxy_class.new(self)
|
||||
else
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if the string has UTF-8 semantics (a String used for purely byte resources is unlikely to have
|
||||
# them), returns false otherwise.
|
||||
def is_utf8?
|
||||
ActiveSupport::Multibyte::Chars.consumes?(self)
|
||||
end
|
||||
class String
|
||||
unless '1.9'.respond_to?(:force_encoding)
|
||||
# == Multibyte proxy
|
||||
#
|
||||
# +mb_chars+ is a multibyte safe proxy for string methods.
|
||||
#
|
||||
# In Ruby 1.8 and older it creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
|
||||
# encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
|
||||
# class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsuled string.
|
||||
#
|
||||
# name = 'Claus Müller'
|
||||
# name.reverse #=> "rell??M sualC"
|
||||
# name.length #=> 13
|
||||
#
|
||||
# name.mb_chars.reverse.to_s #=> "rellüM sualC"
|
||||
# name.mb_chars.length #=> 12
|
||||
#
|
||||
# In Ruby 1.9 and newer +mb_chars+ returns +self+ because String is (mostly) encoding aware. This means that
|
||||
# it becomes easy to run one version of your code on multiple Ruby versions.
|
||||
#
|
||||
# == Method chaining
|
||||
#
|
||||
# All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
|
||||
# method chaining on the result of any of these methods.
|
||||
#
|
||||
# name.mb_chars.reverse.length #=> 12
|
||||
#
|
||||
# == Interoperability and configuration
|
||||
#
|
||||
# The Chars object tries to be as interchangeable with String objects as possible: sorting and comparing between
|
||||
# String and Char work like expected. The bang! methods change the internal string representation in the Chars
|
||||
# object. Interoperability problems can be resolved easily with a +to_s+ call.
|
||||
#
|
||||
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
|
||||
# information about how to change the default Multibyte behaviour see ActiveSupport::Multibyte.
|
||||
def mb_chars
|
||||
if ActiveSupport::Multibyte.proxy_class.wants?(self)
|
||||
ActiveSupport::Multibyte.proxy_class.new(self)
|
||||
else
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if the string has UTF-8 semantics (a String used for purely byte resources is unlikely to have
|
||||
# them), returns false otherwise.
|
||||
def is_utf8?
|
||||
ActiveSupport::Multibyte::Chars.consumes?(self)
|
||||
end
|
||||
|
||||
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)
|
||||
mb_chars
|
||||
end
|
||||
end
|
||||
else
|
||||
def mb_chars #:nodoc
|
||||
self
|
||||
end
|
||||
|
||||
def is_utf8? #:nodoc
|
||||
case encoding
|
||||
when Encoding::UTF_8
|
||||
valid_encoding?
|
||||
when Encoding::ASCII_8BIT, Encoding::US_ASCII
|
||||
dup.force_encoding(Encoding::UTF_8).valid_encoding?
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
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)
|
||||
mb_chars
|
||||
end
|
||||
end
|
||||
else
|
||||
def mb_chars #:nodoc
|
||||
self
|
||||
end
|
||||
|
||||
def is_utf8? #:nodoc
|
||||
case encoding
|
||||
when Encoding::UTF_8
|
||||
valid_encoding?
|
||||
when Encoding::ASCII_8BIT, Encoding::US_ASCII
|
||||
dup.force_encoding(Encoding::UTF_8).valid_encoding?
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,35 +1,18 @@
|
||||
module ActiveSupport #:nodoc:
|
||||
module CoreExtensions #:nodoc:
|
||||
module String #:nodoc:
|
||||
# Additional string tests.
|
||||
module StartsEndsWith
|
||||
def self.append_features(base)
|
||||
if '1.8.7 and up'.respond_to?(:start_with?)
|
||||
base.class_eval do
|
||||
alias_method :starts_with?, :start_with?
|
||||
alias_method :ends_with?, :end_with?
|
||||
end
|
||||
else
|
||||
super
|
||||
base.class_eval do
|
||||
alias_method :start_with?, :starts_with?
|
||||
alias_method :end_with?, :ends_with?
|
||||
end
|
||||
end
|
||||
end
|
||||
class String
|
||||
unless '1.8.7 and up'.respond_to?(:start_with?)
|
||||
# Does the string start with the specified +prefix+?
|
||||
def start_with?(prefix)
|
||||
prefix = prefix.to_s
|
||||
self[0, prefix.length] == prefix
|
||||
end
|
||||
|
||||
# Does the string start with the specified +prefix+?
|
||||
def starts_with?(prefix)
|
||||
prefix = prefix.to_s
|
||||
self[0, prefix.length] == prefix
|
||||
end
|
||||
|
||||
# Does the string end with the specified +suffix+?
|
||||
def ends_with?(suffix)
|
||||
suffix = suffix.to_s
|
||||
self[-suffix.length, suffix.length] == suffix
|
||||
end
|
||||
end
|
||||
# Does the string end with the specified +suffix+?
|
||||
def end_with?(suffix)
|
||||
suffix = suffix.to_s
|
||||
self[-suffix.length, suffix.length] == suffix
|
||||
end
|
||||
end
|
||||
|
||||
alias_method :starts_with?, :start_with?
|
||||
alias_method :ends_with?, :end_with?
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user