Class: ReeText::ToSentence

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_text/package/ree_text/functions/to_sentence.rb

Constant Summary collapse

DEFAULTS =
{
  locale: :en
}
DEFAULT_CONNECTORS =
{
  words_connector: ", ",
  two_words_connector: " and ",
  last_word_connector: ", and "
}

Instance Method Summary collapse

Instance Method Details

#call(array, locale: nil, **opts) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ree_lib/packages/ree_text/package/ree_text/functions/to_sentence.rb', line 43

def call(array, locale: nil, **opts)
  locale = locale || DEFAULTS[:locale]

  i18n_connectors = t("sentence", locale: locale, default_by_locale: :en)
  DEFAULT_CONNECTORS.merge(i18n_connectors)
  options  = DEFAULT_CONNECTORS.merge(opts)

  case array.length
  when 0
    ""
  when 1
    escape_html(array[0])
  when 2
    safe_join([array[0], array[1]], sep: options[:two_words_connector])
  else
    safe_join(
      [
        safe_join(
          array[0...-1],
          sep: options[:words_connector]
        ),
        options[:last_word_connector],
        array[-1]
      ],
      sep: ""
    )
  end
end