Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ActiveSupport Object#blank? and #present? #4902

Merged
merged 2 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@
# Unignore vendored gems
!**/vendor/bundle-standalone/ruby/*/gems/*/lib

# Ignore backports gem (we don't need all files)
# Ignore partially included gems where we don't need all files
**/vendor/bundle-standalone/ruby/*/gems/activesupport-*/lib
**/vendor/bundle-standalone/ruby/*/gems/concurrent-ruby-*/lib
**/vendor/bundle-standalone/ruby/*/gems/backports-*/lib
**/vendor/bundle-standalone/ruby/*/gems/i18n-*/lib
**/vendor/bundle-standalone/ruby/*/gems/minitest-*/lib
**/vendor/bundle-standalone/ruby/*/gems/thread_safe-*/lib
**/vendor/bundle-standalone/ruby/*/gems/tzinfo-*/lib

# Ignore `bin` contents (again).
/bin
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/cmd/doctor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def check_taps

ohai "Homebrew Cask Taps:"
[default_tap, *alt_taps].each do |tap|
if tap.path.nil? || tap.path.to_s.empty?
if tap.path.blank?
puts none_string
else
puts "#{tap.path} (#{cask_count_for_tap(tap)})"
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ def audit
def audit_version
if version.nil?
problem "missing version"
elsif version.to_s.empty?
elsif version.blank?
problem "version is set to an empty string"
elsif !version.detected_from_url?
version_text = version
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def merge
tag = tag.to_s.delete ":"

unless tag.empty?
if !bottle_hash["bottle"]["tags"][tag].to_s.empty?
if bottle_hash["bottle"]["tags"][tag].present?
mismatches << "#{key} => #{tag}"
else
bottle.send(key, old_value => tag.to_sym)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/download_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ def initialize(url, name, version, **meta)
def source_modified_time
out, = system_command("bzr", args: ["log", "-l", "1", "--timezone=utc", cached_location])
timestamp = out.chomp
raise "Could not get any timestamps from bzr!" if timestamp.to_s.empty?
raise "Could not get any timestamps from bzr!" if timestamp.blank?
Time.parse(timestamp)
end

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/extend/os/linux/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def check_tmpdir_executable
end

def check_xdg_data_dirs
return if ENV["XDG_DATA_DIRS"].to_s.empty?
return if ENV["XDG_DATA_DIRS"].blank?
return if ENV["XDG_DATA_DIRS"].split("/").include?(HOMEBREW_PREFIX/"share")
<<~EOS
Homebrew's share was not found in your XDG_DATA_DIRS but you have
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def auditing?
require "extend/module"
require "extend/predicable"
require "extend/string"
require "active_support/core_ext/object/blank"

require "constants"
require "exceptions"
Expand Down
3 changes: 1 addition & 2 deletions Library/Homebrew/missing_formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ def deleted_reason(name, silent: false)
hash, short_hash, *commit_message, relative_path =
Utils.popen_read(log_command).gsub("\\n", "\n").lines.map(&:chomp)

if hash.to_s.empty? || short_hash.to_s.empty? ||
relative_path.to_s.empty?
if hash.blank? || short_hash.blank? || relative_path.blank?
ofail "No previously deleted formula found." unless silent
return
end
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/vendor/Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source "https://rubygems.org"

gem "activesupport"
gem "concurrent-ruby"
gem "backports"
gem "plist"
gem "ruby-macho"
14 changes: 14 additions & 0 deletions Library/Homebrew/vendor/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
backports (3.11.4)
concurrent-ruby (1.0.5)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
minitest (5.11.3)
plist (3.4.0)
ruby-macho (2.0.0)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
activesupport
backports
concurrent-ruby
plist
ruby-macho

Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/vendor/bundle-standalone/bundler/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
ruby_version = RbConfig::CONFIG["ruby_version"]
path = File.expand_path('..', __FILE__)
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/concurrent-ruby-1.0.5/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/i18n-1.1.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/minitest-5.11.3/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/thread_safe-0.3.6/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/tzinfo-1.2.5/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/activesupport-5.2.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/backports-3.11.4/lib"
$:.unshift "#{path}/"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/plist-3.4.0/lib"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# frozen_string_literal: true

require "active_support/core_ext/regexp"
require "concurrent/map"

class Object
# An object is blank if it's false, empty, or a whitespace string.
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
#
# This simplifies
#
# !address || address.empty?
#
# to
#
# address.blank?
#
# @return [true, false]
def blank?
respond_to?(:empty?) ? !!empty? : !self
end

# An object is present if it's not blank.
#
# @return [true, false]
def present?
!blank?
end

# Returns the receiver if it's present otherwise returns +nil+.
# <tt>object.presence</tt> is equivalent to
#
# object.present? ? object : nil
#
# For example, something like
#
# state = params[:state] if params[:state].present?
# country = params[:country] if params[:country].present?
# region = state || country || 'US'
#
# becomes
#
# region = params[:state].presence || params[:country].presence || 'US'
#
# @return [Object]
def presence
self if present?
end
end

class NilClass
# +nil+ is blank:
#
# nil.blank? # => true
#
# @return [true]
def blank?
true
end
end

class FalseClass
# +false+ is blank:
#
# false.blank? # => true
#
# @return [true]
def blank?
true
end
end

class TrueClass
# +true+ is not blank:
#
# true.blank? # => false
#
# @return [false]
def blank?
false
end
end

class Array
# An array is blank if it's empty:
#
# [].blank? # => true
# [1,2,3].blank? # => false
#
# @return [true, false]
alias_method :blank?, :empty?
end

class Hash
# A hash is blank if it's empty:
#
# {}.blank? # => true
# { key: 'value' }.blank? # => false
#
# @return [true, false]
alias_method :blank?, :empty?
end

class String
BLANK_RE = /\A[[:space:]]*\z/
ENCODED_BLANKS = Concurrent::Map.new do |h, enc|
h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING)
end

# A string is blank if it's empty or contains whitespaces only:
#
# ''.blank? # => true
# ' '.blank? # => true
# "\t\n\r".blank? # => true
# ' blah '.blank? # => false
#
# Unicode whitespace is supported:
#
# "\u00a0".blank? # => true
#
# @return [true, false]
def blank?
# The regexp that matches blank strings is expensive. For the case of empty
# strings we can speed up this method (~3.5x) with an empty? call. The
# penalty for the rest of strings is marginal.
empty? ||
begin
BLANK_RE.match?(self)
rescue Encoding::CompatibilityError
ENCODED_BLANKS[self.encoding].match?(self)
end
end
end

class Numeric #:nodoc:
# No number is blank:
#
# 1.blank? # => false
# 0.blank? # => false
#
# @return [false]
def blank?
false
end
end

class Time #:nodoc:
# No Time is blank:
#
# Time.now.blank? # => false
#
# @return [false]
def blank?
false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Regexp #:nodoc:
def multiline?
options & MULTILINE == MULTILINE
end

def match?(string, pos = 0)
!!match(string, pos)
end unless //.respond_to?(:match?)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'thread'
require 'concurrent/collection/map/non_concurrent_map_backend'

module Concurrent

# @!visibility private
module Collection

# @!visibility private
class MriMapBackend < NonConcurrentMapBackend

def initialize(options = nil)
super(options)
@write_lock = Mutex.new
end

def []=(key, value)
@write_lock.synchronize { super }
end

def compute_if_absent(key)
if stored_value = _get(key) # fast non-blocking path for the most likely case
stored_value
else
@write_lock.synchronize { super }
end
end

def compute_if_present(key)
@write_lock.synchronize { super }
end

def compute(key)
@write_lock.synchronize { super }
end

def merge_pair(key, value)
@write_lock.synchronize { super }
end

def replace_pair(key, old_value, new_value)
@write_lock.synchronize { super }
end

def replace_if_exists(key, new_value)
@write_lock.synchronize { super }
end

def get_and_set(key, value)
@write_lock.synchronize { super }
end

def delete(key)
@write_lock.synchronize { super }
end

def delete_pair(key, value)
@write_lock.synchronize { super }
end

def clear
@write_lock.synchronize { super }
end
end
end
end