Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/string.rb,
lib/ext/translator.rb

Instance Method Summary collapse

Instance Method Details

#cama_add_postfix_file_name(postfix) ⇒ Object

Sample:

'/var/www/media/132/logo.png'.cama_add_postfix_file_name('_2') ==> /var/www/media/132/logo_2.png


158
159
160
# File 'lib/ext/string.rb', line 158

def cama_add_postfix_file_name(postfix)
  File.join(File.dirname(self), "#{File.basename(self, File.extname(self))}#{postfix}#{File.extname(self)}")
end

#cama_add_postfix_url(postfix) ⇒ Object

convert url into custom url with postfix, sample: “”.cama_add_postfix_url(‘thumbs/’) into



152
153
154
# File 'lib/ext/string.rb', line 152

def cama_add_postfix_url(postfix)
  File.join(File.dirname(self), "#{postfix}#{File.basename(self)}")
end

#cama_parse_image_version(version_name: '') ⇒ Object

Parse the url to get the image version

version_name: (String) version name,
  if this is empty, this will return the image version for thumb of the image, sample: 'http://localhost/my_image.png'.cama_parse_image_version('') => http://localhost/thumb/my_image.png
  if this is present, this will return the image version generated, sample: , sample: 'http://localhost/my_image.png'.cama_parse_image_version('200x200') => http://localhost/thumb/my_image_200x200.png
default: default image if post image does not exist


167
168
169
170
171
# File 'lib/ext/string.rb', line 167

def cama_parse_image_version(version_name: '')
  res = File.join(File.dirname(self), 'thumb', "#{File.basename(self).parameterize}#{File.extname(self)}")
  res = res.cama_add_postfix_file_name("_#{version_name}") if version_name.present?
  res
end

#cama_replace_codes(values, format_code = '[') ⇒ Object

parse all codes in current text to replace with values sample: “Hello [c1]”.cama_replace_codes(‘World’) ==> Hello World



133
134
135
136
137
138
139
140
141
# File 'lib/ext/string.rb', line 133

def cama_replace_codes(values, format_code = '[')
  res = self
  values.each do |k, v|
    v = v.join(',') if v.is_a?(Array)
    res = res.gsub("[#{k}]", v) if format_code == '['
    res = res.gsub("{#{k}}", v) if format_code == '{'
  end
  res
end

#cama_true?Boolean

check if current string is true or false cases for true: ‘1’ | ‘true’ cases for false: ‘0’ | ‘false’ | ” return boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ext/string.rb', line 33

def cama_true?
  self == 'true' || self == '1'
end

#get_file_nameObject

from a string path, this function get the filename



109
110
111
# File 'lib/ext/string.rb', line 109

def get_file_name
  self.split("/").last.split(".").delete_last.join(".")
end

#hex_to_binaryObject



113
114
115
116
117
118
# File 'lib/ext/string.rb', line 113

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/ext/string.rb', line 53

def include_bar?(uid)
  self.include?("__#{uid}__")
end

#is_bool?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ext/string.rb', line 25

def is_bool?
  self == 'false' || self == 'true'
end

#is_emailObject



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

Returns:

  • (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

Returns:

  • (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_domainObject

parse string into domain owem.tuzitio.com into owem.tuzitio.com



122
123
124
125
126
127
128
129
# File 'lib/ext/string.rb', line 122

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

#parseCamaClassObject

return cleaned model class name remove decorate remove Cama prefix



146
147
148
# File 'lib/ext/string.rb', line 146

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



58
59
60
61
62
63
64
65
66
# File 'lib/ext/string.rb', line 58

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

#slugObject

parse string into slug format



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ext/string.rb', line 86

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_barObject



49
50
51
# File 'lib/ext/string.rb', line 49

def split_bar
  self.split(',').map{|us_id| us_id.gsub('__','')}.uniq
end

#strip_tagsObject



8
9
10
# File 'lib/ext/string.rb', line 8

def strip_tags
  ActionController::Base.helpers.strip_tags(self)
end

#to_boolObject

Raises:

  • (ArgumentError)


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_varObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ext/string.rb', line 37

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

#translationsObject

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_arrayObject

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ext/string.rb', line 69

def truncate_text(string, quantity = 100, quantity_before_text = 20)
  string = string.gsub("+", "").gsub("*", "").gsub("-", "").downcase
  self.strip_tags
  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