Sha256: be28b8d67d23a0f837bfb48443d2f584052b1c71e365523db77ba90565fd9e4d
Contents?: true
Size: 1.38 KB
Versions: 1
Compression:
Stored size: 1.38 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 # Convenience method, for whenever you need to pepper something. def peppered self + ENV['PASSWORD_PEPPER'] end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
junk_drawer-0.0.2 | lib/core_extensions/string.rb |