Sha256: 968221c8abeab9b35894f537d72900b7d92d8ea9e8c790dc04df76b2a8977c42
Contents?: true
Size: 1.25 KB
Versions: 5
Compression:
Stored size: 1.25 KB
Contents
class String class << self # Build a url/human-friendly random string. Defaults to 25 characters long. def friendly(length = 25) (1..length).map{ friendly_characters.sample }.join end # Build a completely unfriendly random string. Defaults to 25 characters long. def crazy(length = 25) (1..length).map{ crazy_characters.sample }.join end private def friendly_characters @friendly_characters ||= ['0'..'9', 'a'..'z', 'A'..'Z'].map(&:to_a).flatten! end def crazy_characters @crazy_characters ||= friendly_characters + ["\\", '(', ')', ' ', '`', '#', '"', "'", '?'] + %w(~ ! @ $ % ^ & * _ - + = [ ] { } | ; : , < . > /) end end # Translate a human-readable representation of an ObjectID instance back to an ObjectID. # Requires the all-your-base gem. def to_oid string = ::AllYourBase::Are.convert_to_base_10(self, :radix => 62).to_s(16) # replace leading zeroes that were lost during the hex conversion until string.length == 24 string = '0' + string end BSON::ObjectID.from_string(string.reverse) end # For when you want to check for a string in a case-insensitive fashion. Mostly used in Mongo queries. def to_regex /^#{Regexp.escape(self)}$/i end end
Version data entries
5 entries across 5 versions & 1 rubygems