Sha256: 4865c5a85639964be769ac6c20de8f6859306ee067ed39a4e41488acd35a1b2b

Contents?: true

Size: 798 Bytes

Versions: 7

Compression:

Stored size: 798 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
    self.split( CAP_LIST_REGEX ).reject{ |match| match == "" || match =~ WHITESPACE_REGEX }
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rcap-2.3.0 lib/rcap/extensions/string.rb
rcap-2.2.1 lib/rcap/extensions/string.rb
rcap-2.2.0 lib/rcap/extensions/string.rb
rcap-2.1.0 lib/rcap/extensions/string.rb
rcap-2.0.2 lib/rcap/extensions/string.rb
rcap-2.0.1 lib/rcap/extensions/string.rb
rcap-2.0.0 lib/rcap/extensions/string.rb