Module: Tzispa::Helpers::Text

Included in:
MacroField
Defined in:
lib/tzispa/helpers/text.rb

Instance Method Summary collapse

Instance Method Details

#amount(number, options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/tzispa/helpers/text.rb', line 95

def amount(number, options = {})
  if number.nil? && options[:nil_as_dash] != false
    '–'
  elsif number.zero? && options[:zero_as_dash]
    '–'
  else
    precision = options[:precision]
    if precision
      options[:minimum_precision] = precision unless options[:minimum_precision]
      options[:maximum_precision] = precision unless options[:maximum_precision]
    end

    number = number.round if options[:round]
    separator = options.fetch(:separator, I18n.t('number.currency.format.separator'))
    delimiter = options.fetch(:delimiter, I18n.t('number.currency.format.delimiter')).to_s
    minimum_precision = options[:minimum_precision] || 0
    str = if number.is_a?(BigDecimal)
            number.to_s('F').sub(/\.0+\z/, '')
          else
            number.to_s.sub(/\.0+\z/, '')
          end
    smatch = str.match(/\A(\-?)(\d+)(?:\.(\d+))?\z/)
    raise "Could not parse number: #{number}" unless smatch
    sign = smatch[1]
    integer = smatch[2]
    fraction = (smatch[3] || '')

    if options[:maximum_precision]
      fraction = fraction[0, options[:maximum_precision]] if options[:maximum_precision]
    end

    if fraction.positive? && minimum_precision.positive?
      fraction = "#{fraction}#{'0' * [0, minimum_precision - fraction.length].max}"
    elsif minimum_precision.zero? && !fraction.empty? && fraction.to_i.zero?
      fraction = ''
    end

    # add a delimiter to every thousands place in the number
    integer_size = integer.size
    (1..((integer_size - 1) / 3)).each { |x| integer[integer_size - x * 3, 0] = delimiter }
    str = integer.chomp(delimiter)

    # add fraction
    str << "#{separator}#{fraction}" unless fraction.empty?

    # restore sign
    str = "#{sign}#{str}"
    # add unit if given
    if options[:unit]
      unless options[:unit_separator] == false
        str << options.fetch(:unit_separator, ' ')
      end
      str << options[:unit]
    end
    str
  end
end

#html_unscape(str) ⇒ Object



47
48
49
# File 'lib/tzispa/helpers/text.rb', line 47

def html_unscape(str)
  CGI.unescapeHTML(str.strip) if str && !str.strip.empty?
end

#join_to_nil(ary, separator) ⇒ Object



55
56
57
# File 'lib/tzispa/helpers/text.rb', line 55

def join_to_nil(ary, separator)
  ary.join(separator) if ary && !ary.empty?
end

#money_amount(number, options = {}) ⇒ Object



153
154
155
156
157
158
# File 'lib/tzispa/helpers/text.rb', line 153

def money_amount(number, options = {})
  amount(number, options.merge(unit: I18n.t('number.currency.format.unit'),
                               nil_as_dash: false,
                               precision: I18n.t('number.currency.format.precision'),
                               minimum_precision: 0))
end

#price_amount(number, options = {}) ⇒ Object



160
161
162
163
164
# File 'lib/tzispa/helpers/text.rb', line 160

def price_amount(number, options = {})
  amount(number, options.merge(nil_as_dash: false,
                               precision: I18n.t('number.currency.format.precision'),
                               minimum_precision: 0))
end

#remove_parenthesized_text(text) ⇒ Object



22
23
24
# File 'lib/tzispa/helpers/text.rb', line 22

def remove_parenthesized_text(text)
  text.gsub(/\([^\)]*\)/, '')
end

#remove_phrases(text, removable) ⇒ Object



17
18
19
20
# File 'lib/tzispa/helpers/text.rb', line 17

def remove_phrases(text, removable)
  removable.each { |phrase| text.slice! phrase }
  text
end

#remove_words(sentence, removable) ⇒ Object



13
14
15
# File 'lib/tzispa/helpers/text.rb', line 13

def remove_words(sentence, removable)
  sentence.split.delete_if { |x| removable.include? UnicodeUtils.downcase(x) }.join(' ')
end

#split_to_array(str, separator = ';') ⇒ Object



51
52
53
# File 'lib/tzispa/helpers/text.rb', line 51

def split_to_array(str, separator = ';')
  str&.split(separator)
end

#starinizer(rating, star_value, max_stars) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/tzispa/helpers/text.rb', line 166

def starinizer(rating, star_value, max_stars)
  {}.tap do |stars|
    stars[:full] = rating / star_value
    stars[:half] = rating % star_value
    stars[:o] = max_stars - stars[:full] - stars[:half]
  end
end

#str_time_ellapsed(t_start, t_end = nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/tzispa/helpers/text.rb', line 78

def str_time_ellapsed(t_start, t_end = nil)
  elapsed = (t_end || Time.now) - t_start
  seconds = elapsed % 60
  minutes = (elapsed / 60) % 60
  hours = elapsed / (60 * 60)
  format('%02d:%02d:%02d', hours, minutes, seconds)
end

#str_to_amount(str, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/tzispa/helpers/text.rb', line 86

def str_to_amount(str, options = {})
  return unless !str || str.strip.empty?
  separator = options.fetch(:separator, I18n.t('number.currency.format.separator'))
  precision = options.fetch(:precision, I18n.t('number.currency.format.precision'))
  str = str.gsub(Regexp.new("[^\\d\\#{separator}\\-]"), '')
  str = str.gsub(separator, '.') if separator != '.'
  BigDecimal.new(str).round(precision).to_s('F')
end

#str_to_bool(str, strue = nil) ⇒ Object



59
60
61
# File 'lib/tzispa/helpers/text.rb', line 59

def str_to_bool(str, strue = nil)
  strue ? (strue == str) : (str == 'yes' || str == 'true')
end

#str_to_date(str, format = nil) ⇒ Object



68
69
70
71
# File 'lib/tzispa/helpers/text.rb', line 68

def str_to_date(str, format = nil)
  str = strip_to_nil str
  Date.strptime(str, format || I18n.t('date.formats.default')) if str
end

#str_to_datetime(str, format = nil) ⇒ Object



73
74
75
76
# File 'lib/tzispa/helpers/text.rb', line 73

def str_to_datetime(str, format = nil)
  str = strip_to_nil str
  DateTime.strptime(str, format || I18n.t('datetime.formats.default'))
end

#str_to_integer(str) ⇒ Object



63
64
65
66
# File 'lib/tzispa/helpers/text.rb', line 63

def str_to_integer(str)
  return unless (sstr = strip_to_nil(str))
  Integer sstr
end

#strip_to_nil(str, transform = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tzispa/helpers/text.rb', line 33

def strip_to_nil(str, transform = nil)
  sstr = str.strip if str && !str.strip.empty?
  case transform
  when :upcase then
    UnicodeUtils.upcase(sstr)
  when :downcase then
    UnicodeUtils.downcase(sstr)
  when :titlecase then
    UnicodeUtils.titlecase(sstr)
  else
    sstr
  end
end

#synonymize(sentence, synonyms) ⇒ Object



26
27
28
29
30
31
# File 'lib/tzispa/helpers/text.rb', line 26

def synonymize(sentence, synonyms)
  sentence.gsub(/[[:alnum:]]+/) do |word|
    dwword = UnicodeUtils.downcase word
    synonyms.key?(dwword) ? synonyms[dwword] : word
  end
end