Class: String
- Includes:
- ActiveSupport::NumberHelper, StringToNewDate, StringToNewTime
- Defined in:
- lib/ruby-rails-extensions/extensions/cutoff.rb,
lib/ruby-rails-extensions/extensions/to_dec.rb,
lib/ruby-rails-extensions/extensions/to_bool.rb,
lib/ruby-rails-extensions/extensions/to_money.rb,
lib/ruby-rails-extensions/extensions/usd_to_f.rb,
lib/ruby-rails-extensions/extensions/usd_to_i.rb,
lib/ruby-rails-extensions/extensions/word_wrap.rb,
lib/ruby-rails-extensions/extensions/to_new_date.rb,
lib/ruby-rails-extensions/extensions/to_new_time.rb,
lib/ruby-rails-extensions/extensions/utf8_encode.rb,
lib/ruby-rails-extensions/extensions/to_delimited.rb,
lib/ruby-rails-extensions/extensions/to_nonzero_i.rb,
lib/ruby-rails-extensions/extensions/quarter_dates.rb,
lib/ruby-rails-extensions/extensions/to_negative_i.rb,
lib/ruby-rails-extensions/extensions/to_positive_i.rb
Constant Summary
Constants included from StringToNewTime
StringToNewTime::NEW_TIME_REGEXES
Constants included from StringToNewDate
StringToNewDate::NEW_DATE_REGEXES
Instance Method Summary collapse
-
#cutoff(max = 60, options = {}) ⇒ String
Cuts off a long string and will add an ellipsis or what has been entered via
:end_moreoption. -
#quarter_end ⇒ Date
Return the quarter end date.
-
#quarter_start ⇒ Date
Return the quarter start date.
-
#safe_utf8_encode(**kwargs) ⇒ String?
Safely calls #utf8_encode with a rescue that will catch all errors.
-
#to_bool ⇒ Object
:nodoc:.
-
#to_dec(p = 4) ⇒ Object
:nodoc:.
-
#to_delimited(options = {}) ⇒ String
(also: #with_delimiter, #with_sep)
Converts a number to a delimited number string.
- #to_money ⇒ Object
-
#to_negative_i ⇒ Integer, Nil
Calls
to_iand returns the value if it is negative. -
#to_nonzero_i ⇒ Integer, Nil
Calls
to_iand returns the value if it is nonzero. -
#to_positive_i ⇒ Integer, Nil
Calls
to_iand returns the value if it is positive. -
#usd_to_f ⇒ Object
:nodoc:.
-
#usd_to_i ⇒ Object
:nodoc:.
- #utf8_encode(**kwargs) ⇒ String
-
#utf8_encode!(binary: true, **kwargs) ⇒ void
Remove all non-UTF-8 chars from a string.
-
#word_wrap(line_width: 80, break_sequence: "\n", return_type: 'string') ⇒ String+
Wraps a string by inserting line breaks at a specified line width.
Methods included from StringToNewTime
Methods included from StringToNewDate
#to_new_date, #to_new_date_mm_yy, #to_new_date_safe
Instance Method Details
#cutoff(max = 60, options = {}) ⇒ String
Cuts off a long string and will add an ellipsis or what has been entered via :end_more option.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby-rails-extensions/extensions/cutoff.rb', line 26 def cutoff(max = 60, = {}) this = self max_len = (max || 60).to_i opts = { ending: false, end_more: nil }.merge( || {}) # Return the string if it isn't over the max return this if this.size < max_len end_chars = opts[:end_more].present? ? opts[:end_more] : '...' unless opts[:ending] return this.truncate(max_len, { omission: end_chars }).html_safe end start_val = ((max_len - 3) / 2).round end_val = max_len - start_val "#{this[0, start_val]}...#{this[-end_val, this.size]}".html_safe end |
#quarter_end ⇒ Date
Return the quarter end date.
16 17 18 |
# File 'lib/ruby-rails-extensions/extensions/quarter_dates.rb', line 16 def quarter_end quarter_date.end_of_quarter end |
#quarter_start ⇒ Date
Return the quarter start date.
8 9 10 |
# File 'lib/ruby-rails-extensions/extensions/quarter_dates.rb', line 8 def quarter_start quarter_date.beginning_of_quarter end |
#safe_utf8_encode(**kwargs) ⇒ String?
Safely calls #utf8_encode with a rescue that will catch all errors.
41 42 43 44 45 |
# File 'lib/ruby-rails-extensions/extensions/utf8_encode.rb', line 41 def safe_utf8_encode(**kwargs) utf8_encode(**kwargs) rescue nil end |
#to_bool ⇒ Object
:nodoc:
28 29 30 |
# File 'lib/ruby-rails-extensions/extensions/to_bool.rb', line 28 def to_bool match?(/^(true|1|t|y|yes)$/i) end |
#to_dec(p = 4) ⇒ Object
:nodoc:
19 20 21 |
# File 'lib/ruby-rails-extensions/extensions/to_dec.rb', line 19 def to_dec(p = 4) (to_f / 100.0).round(p) end |
#to_delimited(options = {}) ⇒ String Also known as: with_delimiter, with_sep
Converts a number to a delimited number string
19 20 21 |
# File 'lib/ruby-rails-extensions/extensions/to_delimited.rb', line 19 def to_delimited( = {}) number_to_delimited(self, ) end |
#to_money ⇒ Object
24 25 26 |
# File 'lib/ruby-rails-extensions/extensions/to_money.rb', line 24 def to_money(...) to_d.to_money(...) end |
#to_negative_i ⇒ Integer, Nil
Calls to_i and returns the value if it is negative
8 9 10 11 12 13 14 |
# File 'lib/ruby-rails-extensions/extensions/to_negative_i.rb', line 8 def to_negative_i val = to_i return unless val.negative? val end |
#to_nonzero_i ⇒ Integer, Nil
Calls to_i and returns the value if it is nonzero
8 9 10 11 12 13 14 |
# File 'lib/ruby-rails-extensions/extensions/to_nonzero_i.rb', line 8 def to_nonzero_i val = to_i return if val.zero? val end |
#to_positive_i ⇒ Integer, Nil
Calls to_i and returns the value if it is positive
8 9 10 11 12 13 14 |
# File 'lib/ruby-rails-extensions/extensions/to_positive_i.rb', line 8 def to_positive_i val = to_i return unless val.positive? val end |
#usd_to_f ⇒ Object
:nodoc:
19 20 21 |
# File 'lib/ruby-rails-extensions/extensions/usd_to_f.rb', line 19 def usd_to_f gsub(/[$,]/, '').to_f end |
#usd_to_i ⇒ Object
:nodoc:
12 13 14 |
# File 'lib/ruby-rails-extensions/extensions/usd_to_i.rb', line 12 def usd_to_i gsub(/[$,]/, '').to_i end |
#utf8_encode(**kwargs) ⇒ String
32 33 34 |
# File 'lib/ruby-rails-extensions/extensions/utf8_encode.rb', line 32 def utf8_encode(**kwargs) dup.utf8_encode!(**kwargs) end |
#utf8_encode!(binary: true, **kwargs) ⇒ void
This method returns an undefined value.
Remove all non-UTF-8 chars from a string.
20 21 22 23 24 25 26 |
# File 'lib/ruby-rails-extensions/extensions/utf8_encode.rb', line 20 def utf8_encode!(binary: true, **kwargs) if binary return replace(utf8_encode_str(self, 'binary', kwargs)) end replace(utf8_encode_str(self, kwargs)) end |
#word_wrap(line_width: 80, break_sequence: "\n", return_type: 'string') ⇒ String+
Wraps a string by inserting line breaks at a specified line width.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby-rails-extensions/extensions/word_wrap.rb', line 12 def word_wrap(line_width: 80, break_sequence: "\n", return_type: 'string') results = split("\n").map! do |line| (line.length > line_width) ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1#{break_sequence}").strip : line end results *= break_sequence # Special case for returning an array of the results if return_type.to_s == 'array' return results.split(break_sequence) end results end |