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
- #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_time_ellapsed(t_start, t_end = nil) ⇒ 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
90 91 92 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 146 |
# File 'lib/tzispa/helpers/text.rb', line 90 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 = 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 [:maximum_precision] fraction = fraction[0, [:maximum_precision]] if [: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 [:unit] unless [:unit_separator] == false str << .fetch(:unit_separator, ' ') end str << [: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
148 149 150 151 152 153 |
# File 'lib/tzispa/helpers/text.rb', line 148 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
155 156 157 158 159 |
# File 'lib/tzispa/helpers/text.rb', line 155 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
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
161 162 163 164 165 166 167 |
# File 'lib/tzispa/helpers/text.rb', line 161 def starinizer(, star_value, max_stars) {}.tap do |stars| stars[:full] = / star_value stars[:half] = % star_value stars[:o] = max_stars - stars[:full] - stars[:half] end end |
#str_time_ellapsed(t_start, t_end = nil) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/tzispa/helpers/text.rb', line 73 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
81 82 83 84 85 86 87 88 |
# File 'lib/tzispa/helpers/text.rb', line 81 def str_to_amount(str, = {}) return unless !str || str.strip.empty? separator = .fetch(:separator, I18n.t('number.currency.format.separator')) precision = .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
63 64 65 66 |
# File 'lib/tzispa/helpers/text.rb', line 63 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
68 69 70 71 |
# File 'lib/tzispa/helpers/text.rb', line 68 def str_to_datetime(str, format = nil) str = strip_to_nil str DateTime.strptime(str, format || I18n.t('datetime.formats.default')) 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 |