Class: String

Inherits:
Object show all
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

Methods included from StringToNewTime

#to_new_time

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.

Parameters:

  • max (Integer) (defaults to: 60)

    Max string length in characters

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :ending (Boolean)

    This will add the ellipsis in the middle of the word instead of the end, which will show the start and end of the string.

  • :end_more (String)

    Instead of showing and ellipsis at the end of the string, it will show this string. This will be counted against the max and will only show up once the max is hit. *This is ignored when :ending enabled.*

Returns:



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, options = {})
  this = self
  max_len = (max || 60).to_i
  opts = { ending: false, end_more: nil }.merge(options || {})

  # 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_endDate

Return the quarter end date.

Returns:



16
17
18
# File 'lib/ruby-rails-extensions/extensions/quarter_dates.rb', line 16

def quarter_end
  quarter_date.end_of_quarter
end

#quarter_startDate

Return the quarter start date.

Returns:



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.

Returns:



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_boolObject

: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

Parameters:

  • options (Hash) (defaults to: {})

Returns:

See Also:

  • ActiveSupport::NumberHelper#number_to_delimited


19
20
21
# File 'lib/ruby-rails-extensions/extensions/to_delimited.rb', line 19

def to_delimited(options = {})
  number_to_delimited(self, options)
end

#to_moneyObject

See Also:



24
25
26
# File 'lib/ruby-rails-extensions/extensions/to_money.rb', line 24

def to_money(...)
  to_d.to_money(...)
end

#to_negative_iInteger, Nil

Calls to_i and returns the value if it is negative

Returns:

  • (Integer, Nil)


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_iInteger, Nil

Calls to_i and returns the value if it is nonzero

Returns:

  • (Integer, Nil)


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_iInteger, Nil

Calls to_i and returns the value if it is positive

Returns:

  • (Integer, Nil)


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_fObject

: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_iObject

: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

Returns:

See Also:



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.

Examples:


"^\u{0000}-\u{007F}".utf8_encode
# => "^\u0000-\u007F"

Parameters:

  • binary (Boolean) (defaults to: true)

Raises:

  • (Encoding::UndefinedConversionError)

    Failed to encode 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.

Parameters:

  • line_width (Numeric) (defaults to: 80)
  • break_sequence (String) (defaults to: "\n")
  • return_type (String) (defaults to: 'string')

    ‘string’ or ‘array’

Returns:



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