Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ext/string.rb,
lib/ext/translator.rb
Instance Method Summary collapse
-
#get_file_name ⇒ Object
from a string path, this function get the filename.
- #hex_to_binary ⇒ Object
- #include_bar?(uid) ⇒ Boolean
- #is_bool? ⇒ Boolean
- #is_email ⇒ Object
- #is_float? ⇒ Boolean
- #is_number? ⇒ Boolean
-
#parse_domain ⇒ Object
parse string into domain owem.tuzitio.com into owem.tuzitio.com.
-
#parseCamaClass ⇒ Object
return cleaned model class name remove decorate remove Cama prefix.
-
#slice_read_more(quantity = 100, start_from = 0) ⇒ Object
slice string respect to correct word for read more.
-
#slug ⇒ Object
parse string into slug format.
- #split_bar ⇒ Object
- #strip_tags ⇒ Object
- #to_bool ⇒ Object
- #to_var ⇒ Object
-
#translate(locale = nil) ⇒ Object
Usage The default value if translation is not provided is all text.
-
#translations ⇒ Object
return hash of translations for this string sample: “hola mundo”, en: “Hello World”.
-
#translations_array ⇒ Object
return aray of translations for this string sample: [“hola mundo”, “Hello World”].
-
#truncate_text(string, quantity = 100, quantity_before_text = 20) ⇒ Object
slice string respect to correct word for read more.
Instance Method Details
#get_file_name ⇒ Object
from a string path, this function get the filename
101 102 103 |
# File 'lib/ext/string.rb', line 101 def get_file_name self.split("/").last.split(".").delete_last.join(".") end |
#hex_to_binary ⇒ Object
105 106 107 108 109 110 |
# File 'lib/ext/string.rb', line 105 def hex_to_binary temp = gsub("\s", ""); ret = [] (0...temp.size()/2).each{|index| ret[index] = [temp[index*2, 2]].pack("H2")} return ret end |
#include_bar?(uid) ⇒ Boolean
45 46 47 |
# File 'lib/ext/string.rb', line 45 def (uid) self.include?("__#{uid}__") end |
#is_bool? ⇒ Boolean
25 26 27 |
# File 'lib/ext/string.rb', line 25 def is_bool? self == 'false' || self == 'true' end |
#is_email ⇒ Object
12 13 14 15 |
# File 'lib/ext/string.rb', line 12 def is_email return false if self.blank? return /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.match(self).present? end |
#is_float? ⇒ Boolean
17 18 19 |
# File 'lib/ext/string.rb', line 17 def is_float? self.to_f.to_s == self.to_s end |
#is_number? ⇒ Boolean
21 22 23 |
# File 'lib/ext/string.rb', line 21 def is_number? self.to_f.to_s == self.to_s || self.to_i.to_s == self.to_s end |
#parse_domain ⇒ Object
parse string into domain owem.tuzitio.com into owem.tuzitio.com
114 115 116 117 118 119 120 121 |
# File 'lib/ext/string.rb', line 114 def parse_domain url = self uri = URI.parse(url) uri = URI.parse("http://#{url}") if uri.scheme.nil? host = (uri.host || self).downcase h = host.start_with?('www.') ? host[4..-1] : host "#{h}#{":#{uri.port}" unless [80, 443].include?(uri.port)}" end |
#parseCamaClass ⇒ Object
return cleaned model class name remove decorate remove Cama prefix
126 127 128 |
# File 'lib/ext/string.rb', line 126 def parseCamaClass self.gsub("Decorator","").gsub("CamaleonCms::","") end |
#slice_read_more(quantity = 100, start_from = 0) ⇒ Object
slice string respect to correct word for read more
50 51 52 53 54 55 56 57 58 |
# File 'lib/ext/string.rb', line 50 def slice_read_more(quantity = 100, start_from = 0) return self if self.length <= quantity tmp = self.slice(start_from, self.length) if tmp.slice(quantity) == " " || tmp.index(" ").nil? return tmp.slice(0, quantity) end quantity += tmp.slice(quantity, tmp.length).index(" ").nil? ? tmp.length : tmp.slice(quantity, tmp.length).index(" ") tmp.slice(0, quantity) end |
#slug ⇒ Object
parse string into slug format
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ext/string.rb', line 78 def slug #strip the string ret = self.strip #blow away apostrophes ret.gsub! /['`]/,"" # @ --> at, and & --> and ret.gsub! /\s*@\s*/, " at " ret.gsub! /\s*&\s*/, " and " #replace all non alphanumeric, underscore or periods with underscore ret.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '_' #convert double underscores to single ret.gsub! /_+/,"_" #strip off leading/trailing underscore ret.gsub! /\A[_\.]+|[_\.]+\z/,"" ret end |
#split_bar ⇒ Object
41 42 43 |
# File 'lib/ext/string.rb', line 41 def self.split(',').map{|us_id| us_id.gsub('__','')}.uniq end |
#strip_tags ⇒ Object
8 9 10 |
# File 'lib/ext/string.rb', line 8 def ActionController::Base.helpers.(self) end |
#to_bool ⇒ Object
2 3 4 5 6 |
# File 'lib/ext/string.rb', line 2 def to_bool return true if self == true || self =~ (/(true|t|yes|y|1)$/i) return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i) raise ArgumentError.new("invalid value for Boolean: \"#{self}\"") end |
#to_var ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ext/string.rb', line 29 def to_var if is_float? self.to_f elsif is_number? self.to_i elsif is_bool? self.to_bool else self end end |
#translate(locale = nil) ⇒ Object
Usage The default value if translation is not provided is all text
$ WpPost.post_title
$ => "<!--:en-->And this is how the Universe ended.<!--:--><!--:fr-->Et c'est ainsi que l'univers connu cessa d'exister.<!--:-->"
$ I18n.locale = :en
$ WpPost.post_title.translate
$ => "And this is how the Universe ended."
$ WpPost.post_title.translate(:en)
$ => "And this is how the Universe ended."
Spits the same text out if no translation tags are applied
$ WpPost.post_title
$ => "And this is how the Universe ended."
$ WpPost.post_title.translate(:fr)
$ => "And this is how the Universe ended."
23 24 25 26 27 28 29 30 31 |
# File 'lib/ext/translator.rb', line 23 def translate(locale = nil) locale ||= I18n.locale locale = locale.to_sym return self if !self.squish.starts_with?("<!--") or self.blank? return translations[locale] if translations.has_key?(locale) return translations[I18n.default_locale] if translations.has_key?(I18n.default_locale) return '' if translations.keys.any? self end |
#translations ⇒ Object
return hash of translations for this string sample: “hola mundo”, en: “Hello World”
35 36 37 38 |
# File 'lib/ext/translator.rb', line 35 def translations @translations ||= split_locales @translations end |
#translations_array ⇒ Object
return aray of translations for this string sample: [“hola mundo”, “Hello World”]
42 43 44 45 |
# File 'lib/ext/translator.rb', line 42 def translations_array r = translations.map{|key, value| value} return r.present? ? r : [self] end |
#truncate_text(string, quantity = 100, quantity_before_text = 20) ⇒ Object
slice string respect to correct word for read more
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ext/string.rb', line 61 def truncate_text(string, quantity = 100, quantity_before_text = 20) string = string.gsub("+", "").gsub("*", "").gsub("-", "").downcase self. return self if self.length <= quantity start_from = self.downcase.index("#{string}") start_from = self.index(/#{string.split(" ").join("|")}/i) unless start_from.present? start_from -= quantity_before_text if start_from.present? && start_from > 0 start_from = 0 if start_from.nil? || start_from < 0 tmp = self.slice(start_from, self.length) if tmp.slice(quantity) == " " || tmp.index(" ").nil? return tmp.slice(0, quantity) end quantity += tmp.slice(quantity, tmp.length).to_s.index(" ").nil? ? tmp.length : tmp.slice(quantity, tmp.length).to_s.index(" ") tmp.slice(0, quantity) end |