Module: Tzispa::Helpers::Text
- Defined in:
- lib/tzispa/helpers/text.rb
Instance Method Summary collapse
- #amount(number, options = {}) ⇒ Object
- #html_unscape(str) ⇒ Object
- #join_to_nil(ary, separator) ⇒ Object
- #mime_formatter(text, mime) ⇒ Object
- #money_amount(number, options = {}) ⇒ Object
- #price_amount(number, options = {}) ⇒ Object
- #remove_parenthesized_text(text) ⇒ Object
- #remove_phrases(text, removable) ⇒ Object
- #remove_words(sentence, removable) ⇒ Object
- #split_to_array(str, separator = ';') ⇒ Object
- #starinizer(rating, star_value, max_stars) ⇒ Object
- #str_to_amount(str, options = {}) ⇒ Object
- #str_to_bool(str, strue = nil) ⇒ Object
- #str_to_date(str, format = nil) ⇒ Object
- #str_to_datetime(str, format = nil) ⇒ Object
- #strip_to_nil(str, transform = nil) ⇒ Object
- #synonymize(sentence, synonyms) ⇒ Object
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, = {}) if number.nil? && [:nil_as_dash] != false '–' elsif number.zero? && [:zero_as_dash] '–' else precision = [:precision] if precision [:minimum_precision] = precision unless [:minimum_precision] [:maximum_precision] = precision unless [:maximum_precision] end number = number.round if [:round] separator = .fetch(:separator, I18n.t('number.currency.format.separator')) delimiter = .fetch(:delimiter, I18n.t('number.currency.format.delimiter')).to_s minimum_precision = [: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 [:maximum_precision] fraction = fraction[0, [:maximum_precision]] if [: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 [:unit] unless [:unit_separator] == false str << .fetch(:unit_separator, ' ') end str << [: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, = {}) amount(number, .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, = {}) amount(number, .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(, star_value, max_stars) Hash.new.tap { |stars| stars[:full] = / star_value stars[:half] = % 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, = {}) if str && !str.strip.empty? separator = .fetch(:separator, I18n.t('number.currency.format.separator')) precision = .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 |