# encoding: utf-8 class Object # An object is blank if it's false, empty, or a whitespace string. # For example, '', ' ', +nil+, [], and {} are all blank. # # This simplifies # # address.nil? || 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+. # object.presence 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/ # 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? BLANK_RE === self end end class Numeric #:nodoc: # No number is blank: # # 1.blank? # => false # 0.blank? # => false # # @return [false] def blank? false end end class Hash # Returns a new hash with all keys converted using the block operation. # # hash = { name: 'Rob', age: '28' } # # hash.transform_keys{ |key| key.to_s.upcase } # # => {"NAME"=>"Rob", "AGE"=>"28"} def transform_keys return enum_for(:transform_keys) unless block_given? result = self.class.new each_key do |key| result[yield(key)] = self[key] end result end # Returns a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. # # hash = { 'name' => 'Rob', 'age' => '28' } # # hash.symbolize_keys # # => {:name=>"Rob", :age=>"28"} def symbolize_keys transform_keys { |key| key.to_sym rescue key } end # Returns a new hash with all keys converted by the block operation. # This includes the keys from the root hash and from all # nested hashes and arrays. # # hash = { person: { name: 'Rob', age: '28' } } # # hash.deep_transform_keys{ |key| key.to_s.upcase } # # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}} def deep_transform_keys(&block) _deep_transform_keys_in_object(self, &block) end # Returns a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. This includes the keys from the root hash # and from all nested hashes and arrays. # # hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } } # # hash.deep_symbolize_keys # # => {:person=>{:name=>"Rob", :age=>"28"}} def deep_symbolize_keys deep_transform_keys { |key| key.to_sym rescue key } end private # support methods for deep transforming nested hashes and arrays def _deep_transform_keys_in_object(object, &block) case object when Hash object.each_with_object({}) do |(key, value), result| result[yield(key)] = _deep_transform_keys_in_object(value, &block) end when Array object.map { |e| _deep_transform_keys_in_object(e, &block) } else object end end end class File class << self # A binary file is Mach-O dSYM # # @return [true, false] def dsym?(file_path) !(`file -b #{file_path}` =~ /dSYM/).nil? end # A file is ASCII text # # @return [true, false] def text?(file_path) !(`file -b #{file_path}` =~ /text/).nil? end end end class String # Convert String encoding to UTF-8 # # @return string def to_utf8 encode(Encoding.find('UTF-8'), invalid: :replace, undef: :replace, replace: '') end end