module StringAndMore
Constants
- CHARS_TO_REMOVE
List with internation chars and their ASCII replacement
- STRING_BASE
Array with all strings which can be encrypted
- STRING_CHARSETS
String
charsets used for numeric encryption
Public Class Methods
email_message_id(mail_domain)
click to toggle source
# File lib/string_and_more.rb, line 47 def self.email_message_id(mail_domain) "<#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@#{mail_domain}>" end
random_string(l=12, mode = :all)
click to toggle source
Returns random String
, for Passwords, Captachs etc.. could create :upcase, :downcase, :numbers or :all
# File lib/string_and_more.rb, line 31 def self.random_string(l=12, mode = :all) case mode when :all base = String::RANDOM_CHARS.values.join("").split("") else base = String::RANDOM_CHARS[mode].split("") end str = "" l.times do str << base.shuffle[rand(base.size-1)] end return str end
Public Instance Methods
add_brs()
click to toggle source
# File lib/string_and_more.rb, line 184 def add_brs return self.gsub("\n", "<br />") end
bold()
click to toggle source
clear_html(options = {})
click to toggle source
# File lib/string_and_more.rb, line 138 def clear_html(options = {}) ActionView::Base.full_sanitizer.sanitize(self, :tags => options[:tags] ) end
colorize(color_code)
click to toggle source
de_2_int()
click to toggle source
# File lib/string_and_more.rb, line 104 def de_2_int str = self CHARS_TO_REMOVE.each do |de, int| str = str.gsub(de, int) end str = str.gsub(" ", " ") return str end
denumberize(options = {})
click to toggle source
# File lib/string_and_more.rb, line 238 def denumberize(options = {}) string_array = STRING_BASE.split("") if options[:token] string_array = string_array.sort_by {|x| x.hash*options[:token].inspect.bytes.join("").to_i} end real_string = "" (options[:base_64] == true ? Base64.decode64(self) : self ).scan(/[0-9]{2}.{1}/).each do |s| real_string << string_array[s.first(2).to_i] end return real_string end
green()
click to toggle source
# File lib/string_and_more.rb, line 198 def green colorize(32) end
gsub(old, repl)
click to toggle source
# File lib/string_and_more.rb, line 261 def gsub(old, repl) self.to_s.gsub(old, repl) end
ital()
click to toggle source
# File lib/string_and_more.rb, line 160 def ital "<i>#{self}</i>".html_safe end
limit(l=20)
click to toggle source
# File lib/string_and_more.rb, line 113 def limit(l=20) if self.size > l s = self.size s2 = (((self.size-1)/2).round) first = self.slice(0..s2) last = self.slice(s2+1..-1) l1 = (l/2).round "#{first.first(l1)}...#{last.last(l1)}" else return self end end
nbsp()
click to toggle source
# File lib/string_and_more.rb, line 172 def nbsp self.gsub(" ", " ").html_safe end
numberize(options = {})
click to toggle source
Numerische encription¶ ↑
cool thing for simple encrypt and decrypt strings
# File lib/string_and_more.rb, line 214 def numberize(options = {}) # Basisarray das alle zeichen enthält die verschlüsselt werden können string_array = STRING_BASE.split("") if options[:token] string_array = string_array.sort_by {|x| x.hash*options[:token].inspect.bytes.join("").to_i} end # Nur Zahlen und buchstaben für die verschlüsselung/mix nehmen wg. URLs string_array_filtered = string_array.select {|s| !s.match(/[a-zA-Z0-9\-_]/).nil? } splitted = self.split("") numbered_string = "" splitted.each do |s| position = string_array.index(s) if !position.nil? numbered_string << (position.to_s.rjust(2, "0")+string_array_filtered[rand(string_array_filtered.size-1)]) end end return options[:base_64] == true ? Base64.encode64(numbered_string) : numbered_string end
pink()
click to toggle source
# File lib/string_and_more.rb, line 206 def pink colorize(35) end
red()
click to toggle source
# File lib/string_and_more.rb, line 194 def red colorize(31) end
replace_entities(mode = :html, options = {})
click to toggle source
# File lib/string_and_more.rb, line 176 def replace_entities(mode = :html, options = {}) str = self Chars2Remove::ENTITIES.each do |orig, rep| str = str.gsub(orig, rep[mode]) end return str.html_safe end
replace_html(from, to)
click to toggle source
# File lib/string_and_more.rb, line 142 def replace_html(from, to) new_text = self new_text = new_text.gsub("<#{from}>", "<#{to}>") new_text = new_text.gsub("</#{from}>", "</#{to}>") return new_text end
shuffle()
click to toggle source
# File lib/string_and_more.rb, line 81 def shuffle self.split("").sort_by {rand}.join.humanize end
span()
click to toggle source
# File lib/string_and_more.rb, line 164 def span "<span>#{self}</span>".html_safe end
to_boolean()
click to toggle source
Extract boolean status from string
# File lib/string_and_more.rb, line 53 def to_boolean if ['true', 'True', 'TRUE', '1'].include?(self) return true else return false end end
to_date()
click to toggle source
# File lib/string_and_more.rb, line 61 def to_date Date.parse(self) end
to_datetime()
click to toggle source
# File lib/string_and_more.rb, line 131 def to_datetime date = self.split(" ").first time = self.split(" ").last.split(":") DateTime.new(date.split(".")[2].to_i, date.split(".")[1].to_i, date.split(".")[0].to_i, time.first.to_i, time.last.to_i) end
to_foldername(sep = "_")
click to toggle source
# File lib/string_and_more.rb, line 86 def to_foldername(sep = "_") # deutsch umlaute ersetzen parameterized_string = self.strip.gsub(/\ {2,}/ , " ").de_2_int # Turn unwanted chars into the separator parameterized_string.gsub!(/[^a-zA-Z0-9ÄÖÜöäüß\-_]+/, sep) unless sep.nil? || sep.empty? re_sep = Regexp.escape(sep) # No more than one of the separator in a row. parameterized_string.gsub!(/#{re_sep}{2,}/, sep) # Remove leading/trailing separator. parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/, '') end return parameterized_string end
to_label(options = {:show_tooltip => false})
click to toggle source
# File lib/string_and_more.rb, line 77 def to_label(options = {:show_tooltip => false}) I18n.t("friendly_labels.#{options[:show_tooltip] == true ? 'label_tooltips' : 'labels'}.#{self.downcase}") end
to_model()
click to toggle source
# File lib/string_and_more.rb, line 149 def to_model self.singularize.camelize.constantize end
to_number(type = :float)
click to toggle source
# File lib/string_and_more.rb, line 66 def to_number(type = :float) extract = self.match(/[0-9\.,]{1,}/).to_s final = extract.gsub(".", "").gsub(",", ".") case type when :float return final.to_f else return final.to_i end end
uline()
click to toggle source
# File lib/string_and_more.rb, line 168 def uline "<u>#{self}</u>".html_safe end
yellow()
click to toggle source
# File lib/string_and_more.rb, line 202 def yellow colorize(33) end