Sha256: 8cb581f27bb8fab43e977075001fb19784aeeff712a41453ec82e9827d088305

Contents?: true

Size: 1.88 KB

Versions: 89

Compression:

Stored size: 1.88 KB

Contents

# Provides a label for an object.
# This simple implementation calls #to_s on the given object, and handles articles 'a/an/the'.
#
class Puppet::Pops::LabelProvider
  VOWELS = %w{a e i o u y}
  SKIPPED_CHARACTERS = %w{" '}
  A = "a"
  AN = "an"

  # Provides a label for the given object by calling `to_s` on the object.
  # The intent is for this method to be overridden in concrete label providers.
  def label o
    o.to_s
  end

  # Produces a label for the given text with indefinite article (a/an)
  def a_an o
    text = label(o)
    "#{article(text)} #{text}"
  end

  # Produces a label for the given text with indefinite article (A/An)
  def a_an_uc o
    text = label(o)
    "#{article(text).capitalize} #{text}"
  end

  # Produces a label for the given text with *definitie article* (the).
  def the o
    "the #{label(o)}"
  end

  # Produces a label for the given text with *definitie article* (The).
  def the_uc o
    "The #{label(o)}"
  end

  # Appends 's' to (optional) text if count != 1 else an empty string
  def plural_s(count, text = '')
    count == 1 ? text : "#{text}s"
  end

  private

  # Produces an *indefinite article* (a/an) for the given text ('a' if
  # it starts with a vowel) This is obviously flawed in the general
  # sense as may labels have punctuation at the start and this method
  # does not translate punctuation to English words. Also, if a vowel is
  # pronounced as a consonant, the article should not be "an".
  #
  def article s
    article_for_letter(first_letter_of(s))
  end

  def first_letter_of(string)
    char = string[0,1]
    if SKIPPED_CHARACTERS.include? char
      char = string[1,1]
    end

    if char == ""
      raise Puppet::DevError, "<#{string}> does not appear to contain a word"
    end

    char
  end

  def article_for_letter(letter)
    downcased = letter.downcase
    if VOWELS.include? downcased
      AN
    else
      A
    end
  end
end

Version data entries

89 entries across 89 versions & 2 rubygems

Version Path
puppet-retrospec-0.12.2 vendor/gems/puppet-3.7.3/lib/puppet/pops/label_provider.rb
puppet-3.8.7 lib/puppet/pops/label_provider.rb
puppet-3.8.7-x86-mingw32 lib/puppet/pops/label_provider.rb
puppet-3.8.7-x64-mingw32 lib/puppet/pops/label_provider.rb
puppet-3.8.6 lib/puppet/pops/label_provider.rb
puppet-3.8.6-x86-mingw32 lib/puppet/pops/label_provider.rb
puppet-retrospec-0.12.1 vendor/gems/puppet-3.7.3/lib/puppet/pops/label_provider.rb
puppet-3.8.6-x64-mingw32 lib/puppet/pops/label_provider.rb
puppet-retrospec-0.12.0 vendor/gems/puppet-3.7.3/lib/puppet/pops/label_provider.rb
puppet-3.8.5 lib/puppet/pops/label_provider.rb
puppet-3.8.5-x86-mingw32 lib/puppet/pops/label_provider.rb
puppet-3.8.5-x64-mingw32 lib/puppet/pops/label_provider.rb
puppet-3.8.4 lib/puppet/pops/label_provider.rb
puppet-3.8.4-x86-mingw32 lib/puppet/pops/label_provider.rb
puppet-3.8.4-x64-mingw32 lib/puppet/pops/label_provider.rb
puppet-4.2.3 lib/puppet/pops/label_provider.rb
puppet-4.2.3-x86-mingw32 lib/puppet/pops/label_provider.rb
puppet-4.2.3-x64-mingw32 lib/puppet/pops/label_provider.rb
puppet-retrospec-0.11.0 vendor/gems/puppet-3.7.3/lib/puppet/pops/label_provider.rb
puppet-retrospec-0.10.0 vendor/gems/puppet-3.7.3/lib/puppet/pops/label_provider.rb