Permalink
Browse files

Deprecate string and hash instance methods (#275)

  • Loading branch information...
marionschleifer authored and jodosha committed Jul 2, 2018
1 parent 3841ae1 commit b96e2d4806ff331c25fd1309c7fa02e5a55d37a4
@@ -81,7 +81,7 @@ jobs:
./script/ci
"jruby-9.1":
docker:
- image: circleci/jruby:9.1.17.0
- image: circleci/jruby:9.1-jdk
working_directory: ~/hanami-utils
steps:
- checkout
@@ -8,14 +8,14 @@ rvm:
- 2.4.2
- 2.3.5
- 2.5.0
- jruby-9.1.13.0
- ruby-head
- jruby-head

matrix:
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
- rvm: jruby-9.1.1.0

notifications:
webhooks:
@@ -1,4 +1,3 @@
require 'hanami/utils/string'
require 'hanami/utils/deprecation'

module Hanami
@@ -113,7 +112,7 @@ def self.load(name, namespace = Object)
def self.load_from_pattern!(pattern, namespace = Object)
Deprecation.new('Hanami::Utils::Class.load_from_pattern! is deprecated, please use Hanami::Utils::Class.load! instead')

String.new(pattern).tokenize do |token|
tokenize(pattern) do |token|
begin
return namespace.const_get(token, false)
rescue NameError # rubocop:disable Lint/HandleExceptions
@@ -123,6 +122,35 @@ def self.load_from_pattern!(pattern, namespace = Object)
full_name = [(namespace == Object ? nil : namespace), pattern].compact.join('::')
raise NameError.new("uninitialized constant #{full_name}")
end

# rubocop:disable Metrics/MethodLength
def self.tokenize(pattern)
if match = TOKENIZE_REGEXP.match(pattern) # rubocop:disable Lint/AssignmentInCondition
pre = match.pre_match
post = match.post_match
tokens = match[1].split(TOKENIZE_SEPARATOR)
tokens.each do |token|
yield("#{pre}#{token}#{post}")
end
else
yield(pattern)
end

nil
end
# rubocop:enable Metrics/MethodLength

# Regexp for .tokenize
#
# @since x.x.x
# @api private
TOKENIZE_REGEXP = /\((.*)\)/

# Separator for .tokenize
#
# @since x.x.x
# @api private
TOKENIZE_SEPARATOR = '|'.freeze
end
end
end
@@ -1,6 +1,7 @@
require 'hanami/utils/inflector'
require 'transproc'
require 'concurrent/map'
require 'hanami/utils/deprecation'

module Hanami
module Utils
@@ -129,8 +130,8 @@ class String # rubocop:disable Metrics/ClassLength
# Hanami::Utils::String.transform("Cherry", -> { "blossom" }))
# # => ArgumentError: wrong number of arguments (given 1, expected 0)
#
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def self.transform(input, *transformations)
fn = @__transformations__.fetch_or_store(transformations.hash) do
compose do |fns|
@@ -150,8 +151,8 @@ def self.transform(input, *transformations)

fn.call(input)
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

# Extracted from `transproc` source code
#
@@ -329,6 +330,7 @@ def self.namespace(input)
#
# Hanami::Utils::String.pluralize('book') # => 'books'
def self.pluralize(input)
Hanami::Utils::Deprecation.new("#{name}.pluralize is deprecated")
string = ::String.new(input.to_s)
Inflector.pluralize(string)
end
@@ -348,6 +350,7 @@ def self.pluralize(input)
#
# Hanami::Utils::String.singularize('books') # => 'book'
def self.singularize(input)
Hanami::Utils::Deprecation.new("#{name}.singularize is deprecated")
string = ::String.new(input.to_s)
Inflector.singularize(string)
end
@@ -405,6 +408,7 @@ def initialize(string)
# string = Hanami::Utils::String.new 'hanami utils'
# string.titleize # => "Hanami Utils"
def titleize
Hanami::Utils::Deprecation.new("#{self.class.name}#titleize is deprecated, please use #{self.class.name}.titleize")
self.class.new underscore.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
end

@@ -432,6 +436,7 @@ def titleize
# string = Hanami::Utils::String.new 'hanami-utils'
# string.capitalize # => "Hanami utils"
def capitalize
Hanami::Utils::Deprecation.new("#{self.class.name}#capitalize is deprecated, please use #{self.class.name}.capitalize")
head, *tail = underscore.split(CLASSIFY_SEPARATOR)

self.class.new(
@@ -450,7 +455,8 @@ def capitalize
#
# string = Hanami::Utils::String.new 'hanami_utils'
# string.classify # => 'HanamiUtils'
def classify
def classify # rubocop:disable Metrics/AbcSize
Hanami::Utils::Deprecation.new("#{self.class.name}#classify is deprecated, please use #{self.class.name}.classify")
words = underscore.split(CLASSIFY_WORD_SEPARATOR).map!(&:capitalize)
delimiters = underscore.scan(CLASSIFY_WORD_SEPARATOR)

@@ -476,6 +482,7 @@ def classify
# string = Hanami::Utils::String.new 'HanamiUtils'
# string.underscore # => 'hanami_utils'
def underscore
Hanami::Utils::Deprecation.new("#{self.class.name}#underscore is deprecated, please use #{self.class.name}.underscore")
new_string = gsub(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
new_string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
new_string.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
@@ -502,6 +509,7 @@ def underscore
# string = Hanami::Utils::String.new 'HanamiUtils'
# string.dasherize # => "hanami-utils"
def dasherize
Hanami::Utils::Deprecation.new("#{self.class.name}#dasherize is deprecated, please use #{self.class.name}.dasherize")
self.class.new underscore.split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR)
end

@@ -520,6 +528,7 @@ def dasherize
# string = Hanami::Utils::String.new 'String'
# string.demodulize # => 'String'
def demodulize
Hanami::Utils::Deprecation.new("#{self.class.name}#demodulize is deprecated, please use #{self.class.name}.demodulize")
self.class.new split(NAMESPACE_SEPARATOR).last
end

@@ -538,6 +547,7 @@ def demodulize
# string = Hanami::Utils::String.new 'String'
# string.namespace # => 'String'
def namespace
Hanami::Utils::Deprecation.new("#{self.class.name}#namespace is deprecated, please use #{self.class.name}.namespace")
self.class.new split(NAMESPACE_SEPARATOR).first
end

@@ -561,7 +571,10 @@ def namespace
# # =>
# 'Hanami::Utils'
# 'Hanami::App'
def tokenize # rubocop:disable Metrics/MethodLength
#
# rubocop:disable Metrics/MethodLength
def tokenize
Hanami::Utils::Deprecation.new("#{self.class.name}#tokenize is deprecated")
if match = TOKENIZE_REGEXP.match(@string) # rubocop:disable Lint/AssignmentInCondition
pre = match.pre_match
post = match.post_match
@@ -575,6 +588,7 @@ def tokenize # rubocop:disable Metrics/MethodLength

nil
end
# rubocop:enable Metrics/MethodLength

# Return a pluralized version of self.
#
@@ -585,6 +599,7 @@ def tokenize # rubocop:disable Metrics/MethodLength
#
# @see Hanami::Utils::Inflector
def pluralize
Hanami::Utils::Deprecation.new("#{self.class.name}#pluralize is deprecated")
self.class.new Inflector.pluralize(self)
end

@@ -597,6 +612,7 @@ def pluralize
#
# @see Hanami::Utils::Inflector
def singularize
Hanami::Utils::Deprecation.new("#{self.class.name}#singularize is deprecated")
self.class.new Inflector.singularize(self)
end

@@ -639,6 +655,7 @@ def ==(other)
#
# @since 0.3.0
def split(pattern, limit = 0)
Hanami::Utils::Deprecation.new("#{self.class.name}#split is deprecated")
@string.split(pattern, limit)
end

@@ -650,6 +667,7 @@ def split(pattern, limit = 0)
#
# @since 0.3.0
def gsub(pattern, replacement = nil, &blk)
Hanami::Utils::Deprecation.new("#{self.class}#gsub is deprecated")
if block_given?
@string.gsub(pattern, &blk)
else
@@ -666,6 +684,7 @@ def gsub(pattern, replacement = nil, &blk)
#
# @since 0.6.0
def scan(pattern, &blk)
Hanami::Utils::Deprecation.new("#{self.class}#scan is deprecated")
@string.scan(pattern, &blk)
end

@@ -694,6 +713,7 @@ def scan(pattern, &blk)
# puts result
# # => #<Hanami::Utils::String:0x007fdb41232ed0 @string="authors/books#index">
def rsub(pattern, replacement)
Hanami::Utils::Deprecation.new("#{self.class.name}#rsub is deprecated, please use #{self.class.name}.rsub")
if i = rindex(pattern) # rubocop:disable Lint/AssignmentInCondition
s = @string.dup
s[i] = replacement
@@ -710,6 +730,7 @@ def rsub(pattern, replacement)
#
# @raise [NoMethodError] If doesn't respond to the given method
def method_missing(method_name, *args, &blk)
Hanami::Utils::Deprecation.new("#{self.class.name}##{method_name} is deprecated")
raise NoMethodError.new(%(undefined method `#{method_name}' for "#{@string}":#{self.class})) unless respond_to?(method_name)

s = @string.__send__(method_name, *args, &blk)
@@ -722,6 +743,7 @@ def method_missing(method_name, *args, &blk)
# @api private
# @since 0.3.0
def respond_to_missing?(method_name, include_private = false)
Hanami::Utils::Deprecation.new("#{self.class.name}##{method_name} is deprecated")
@string.respond_to?(method_name, include_private)
end
end
@@ -0,0 +1,10 @@
require "rspec"
require "hanami/utils/io"

RSpec.configure do |config|
config.around(:example, silence_deprecations: true) do |example|
Hanami::Utils::IO.silence_warnings do
example.run
end
end
end
@@ -8,15 +8,15 @@
[nil, false, '', ' ', " \n\t \r ", ' ', "\u00a0", [], {}, Set.new,
Hanami::Utils::Kernel.Boolean(0), Hanami::Utils::String.new(''),
Hanami::Utils::Hash.new({})].each do |v|
it 'returns true' do
it 'returns true', silence_deprecations: true do
expect(Hanami::Utils::Blank.blank?(v)).to eq(true)
end
end

[Object.new, true, 0, 1, 'a', :book, DateTime.now, Time.now, Date.new, [nil], { nil => 0 }, Set.new([1]),
Hanami::Utils::Kernel.Symbol(:hello), Hanami::Utils::String.new('foo'),
Hanami::Utils::Hash.new(foo: :bar)].each do |v|
it 'returns false' do
it 'returns false', silence_deprecations: true do
expect(Hanami::Utils::Blank.blank?(v)).to eq(false)
end
end
@@ -26,15 +26,15 @@
[nil, false, '', ' ', " \n\t \r ", ' ', "\u00a0", [], {}, Set.new,
Hanami::Utils::Kernel.Boolean(0), Hanami::Utils::String.new(''),
Hanami::Utils::Hash.new({})].each do |v|
it 'returns false' do
it 'returns false', silence_deprecations: true do
expect(Hanami::Utils::Blank.filled?(v)).to eq(false)
end
end

[Object.new, true, 0, 1, 'a', :book, DateTime.now, Time.now, Date.new, [nil], { nil => 0 }, Set.new([1]),
Hanami::Utils::Kernel.Symbol(:hello), Hanami::Utils::String.new('foo'),
Hanami::Utils::Hash.new(foo: :bar)].each do |v|
it 'returns true' do
it 'returns true', silence_deprecations: true do
expect(Hanami::Utils::Blank.filled?(v)).to eq(true)
end
end
@@ -1,4 +1,5 @@
require 'set'
require 'date'
require 'bigdecimal'
require 'hanami/utils/duplicable'

@@ -1,5 +1,6 @@
require 'hanami/utils'
require 'hanami/utils/escape'
require 'date'

RSpec.describe Hanami::Utils::Escape do
let(:mod) { Hanami::Utils::Escape }
@@ -69,7 +69,7 @@
expect(actual).to eq plural
end

it %(pluralizes titleized "#{Hanami::Utils::String.new(singular).titleize}" to "#{plural}") do
it %(pluralizes titleized "#{Hanami::Utils::String.titleize(singular)}" to "#{plural}"), silence_deprecations: true do
actual = Hanami::Utils::Inflector.pluralize(Hanami::Utils::String.new(singular).titleize)
expect(actual).to eq Hanami::Utils::String.new(plural).titleize
end
@@ -121,7 +121,7 @@
expect(actual).to eq singular
end

it %(singularizes titleized "#{Hanami::Utils::String.new(plural).titleize}" to "#{singular}") do
it %(singularizes titleized "#{Hanami::Utils::String.titleize(plural)}" to "#{singular}"), silence_deprecations: true do
actual = Hanami::Utils::Inflector.singularize(Hanami::Utils::String.new(plural).titleize)
expect(actual).to eq Hanami::Utils::String.new(singular).titleize
end
@@ -862,21 +862,6 @@ def to_d
end
end

describe 'when a string without numbers is given' do
let(:input) { 'home' }

require "rbconfig"
if RUBY_VERSION.start_with?('2.4') && RbConfig::CONFIG["host_os"] =~ /darwin/
it 'raises error' do
expect { Hanami::Utils::Kernel.BigDecimal(input) }.to raise_error(ArgumentError, "invalid value for BigDecimal(): #{input.inspect}")
end
else
it 'returns an BigDecimal' do
expect(Hanami::Utils::Kernel.BigDecimal(input)).to eq BigDecimal(0)
end
end
end

describe 'when a string which starts with a big decimal is given' do
let(:input) { '23.0 street' }

@@ -1646,23 +1631,23 @@ def to_bool
end
end

describe 'when "0" is given (Hanami::Utils::String)' do
describe 'when "0" is given (Hanami::Utils::String)', silence_deprecations: true do
let(:input) { Hanami::Utils::String.new('0') }

it 'returns false' do
expect(@result).to eq false
end
end

describe 'when "1" is given (Hanami::Utils::String)' do
describe 'when "1" is given (Hanami::Utils::String)', silence_deprecations: true do
let(:input) { Hanami::Utils::String.new('1') }

it 'returns true' do
expect(@result).to eq true
end
end

describe 'when "foo" is given (Hanami::Utils::String)' do
describe 'when "foo" is given (Hanami::Utils::String)', silence_deprecations: true do
let(:input) { Hanami::Utils::String.new('foo') }

it 'returns false' do
Oops, something went wrong.

0 comments on commit b96e2d4

Please sign in to comment.