Module: Tzispa::Helpers::Text

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

Instance Method Summary collapse

Instance Method Details

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



93
94
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
# File 'lib/tzispa/helpers/text.rb', line 93

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 = number.is_a?(BigDecimal) ? number.to_s('F').sub(/\.0+\z/, "") : number.to_s.sub(/\.0+\z/, "")
    str =~ /\A(\-?)(\d+)(?:\.(\d+))?\z/ or raise "Could not parse number: #{number}"
    sign = $1
    integer = $2
    fraction = ($3 || '')

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


    if fraction.length > 0 && minimum_precision > 0
      fraction = "#{fraction}#{'0' * [0, minimum_precision - fraction.length].max}"
    elsif minimum_precision == 0 && fraction.length > 0 && fraction.to_i == 0
      fraction = ''
    end

    # the following two lines appear to be the most performant way to 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}" if fraction.length > 0

    # 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



49
50
51
# File 'lib/tzispa/helpers/text.rb', line 49

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

#join_to_nil(ary, separator) ⇒ Object



57
58
59
# File 'lib/tzispa/helpers/text.rb', line 57

def join_to_nil(ary, separator)
  ary.join(separator) if ary and not ary.empty?
end

#mime_formatter(text, mime) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/tzispa/helpers/text.rb', line 155

def mime_formatter(text, mime)
  case mime
  when 'text/x-markdown'
    Redcarpet::Markdown.new(Redcarpet::Render::HTML.new).render(text) if text
  else
    text
  end
end

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



147
148
149
# File 'lib/tzispa/helpers/text.rb', line 147

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



151
152
153
# File 'lib/tzispa/helpers/text.rb', line 151

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



25
26
27
# File 'lib/tzispa/helpers/text.rb', line 25

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

#remove_phrases(text, removable) ⇒ Object



18
19
20
21
22
23
# File 'lib/tzispa/helpers/text.rb', line 18

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

#remove_words(sentence, removable) ⇒ Object



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

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

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



53
54
55
# File 'lib/tzispa/helpers/text.rb', line 53

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

#starinizer(rating, star_value, max_stars) ⇒ Object



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

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

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



82
83
84
85
86
87
88
89
90
91
# File 'lib/tzispa/helpers/text.rb', line 82

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

#str_to_bool(str, strue = nil) ⇒ Object



61
62
63
# File 'lib/tzispa/helpers/text.rb', line 61

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

#str_to_date(str, format = nil) ⇒ Object



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

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

#str_to_datetime(str, format = nil) ⇒ Object



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

def str_to_datetime(str, format=nil)
  begin
    result = DateTime.strptime(str, format || I18n.t('time.formats.default'))
  rescue
    result = nil
  end
  result
end

#strip_to_nil(str, transform = nil) ⇒ Object



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

def strip_to_nil(str, transform = nil)
  sstr = str.strip if str and not 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



29
30
31
32
33
34
# File 'lib/tzispa/helpers/text.rb', line 29

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