Sha256: 38a7d8fb36376ecb88e4385ef6cc027260f18696863ca663343b09200ce240e6

Contents?: true

Size: 825 Bytes

Versions: 4

Compression:

Stored size: 825 Bytes

Contents

class String
  CAP_LIST_REGEX = Regexp.new('"([\w\s]+)"|(\S+)')
  WHITESPACE_REGEX = Regexp.new('^\s+$')

  # Reformats string for a CAP list. If the string contains whitespace it will
  # enclose the contents in quotation marks.
  #
  # @return [String]
  # @example
  #   "one".for_cap_list       # => "one"
  #   "two words".for_cap_list # => "\"two words\""
  def for_cap_list
    if self =~ /\s/
      '"' + self + '"'
    else
      self
    end
  end

  # Will unpack a string generated by {Array#to_s_for_cap}
  #
  # @return [Array<String>]
  # @example
  #   "one \"two words\" three".unpack_cap_list # => [ "one", "two words", "three" ]
  # @see Array#to_s_for_cap
  def unpack_cap_list
    split(CAP_LIST_REGEX).reject { |match| match == '' || match =~ WHITESPACE_REGEX }
  end

  def blank?
    empty?
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rcap-2.7.0 lib/rcap/extensions/string.rb
rcap-2.6.0 lib/rcap/extensions/string.rb
rcap-2.5.1 lib/rcap/extensions/string.rb
rcap-2.5.0 lib/rcap/extensions/string.rb