Class: Faker::Lorem

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/lorem.rb

Overview

Based on Perl’s Text::Lorem

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.characterString

Generates single character

Examples:

Faker::Lorem.character    #=> "e"

Returns:



60
61
62
# File 'lib/faker/default/lorem.rb', line 60

def character
  sample(Types::CHARACTERS)
end

.characters(number: 255, min_alpha: 0, min_numeric: 0) ⇒ String

Produces a random string of alphanumeric characters

Examples:

Faker::Lorem.characters #=> "uw1ep04lhs0c4d931n1jmrspprf5w..."
Faker::Lorem.characters(number: 10) #=> "ang9cbhoa8"
Faker::Lorem.characters(number: 10, min_alpha: 4) #=> "ang9cbhoa8"
Faker::Lorem.characters(number: 10, min_alpha: 4, min_numeric: 1) #=> "ang9cbhoa8"

Parameters:

  • number (Integer) (defaults to: 255)

    The number of characters to generate

  • min_alpha (Integer) (defaults to: 0)

    The minimum number of alphabetic to add to the string

  • min_numeric (Integer) (defaults to: 0)

    The minimum number of numbers to add to the string

Returns:



80
81
82
# File 'lib/faker/default/lorem.rb', line 80

def characters(number: 255, min_alpha: 0, min_numeric: 0)
  Alphanumeric.alphanumeric(number: number, min_alpha: min_alpha, min_numeric: min_numeric)
end

.multibyteString

Generates the emoji

Examples:

Faker::Lorem.multibyte  #=> "😀"
Faker::Lorem.multibyte  #=> "❤"

Returns:



94
95
96
# File 'lib/faker/default/lorem.rb', line 94

def multibyte
  sample(translate('faker.lorem.multibyte')).pack('C*').force_encoding('utf-8')
end

.paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil) ⇒ String

Generates three sentence paragraph

Examples:

Faker::Lorem.paragraph
  #=> "Impedit et est. Aliquid deleniti necessitatibus. Et aspernatur minima."
Faker::Lorem.paragraph(sentence_count: 2)
  #=> "Rerum fugit vitae. Et atque autem."
Faker::Lorem.paragraph(sentence_count: 2, supplemental: true)
  #=> "Terreo coerceo utor. Vester sunt cogito."
Faker::Lorem.paragraph(sentence_count: 2, supplemental: true, random_sentences_to_add: 2)
  #=> "Texo tantillus tamisium. Tribuo amissio tamisium. Facere aut canis."

Parameters:

  • sentence_count (Integer) (defaults to: 3)

    Number of sentences in the paragraph

  • supplemental (Boolean) (defaults to: false)
  • random_sentences_to_add (Integer) (defaults to: 0)

Returns:



156
157
158
# File 'lib/faker/default/lorem.rb', line 156

def paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil)
  sentences(number: resolve(sentence_count) + rand(random_sentences_to_add.to_i), supplemental: supplemental, exclude_words: exclude_words).join(locale_space)
end

.paragraph_by_chars(number: 256, supplemental: false) ⇒ String

Generates paragraph with 256 characters

Examples:

Faker::Lorem.paragraph_by_chars
Faker::Lorem.paragraph_by_chars(number: 20)                       #=> "Sit modi alias. Imp."
Faker::Lorem.paragraph_by_chars(number: 20, supplemental: true)   #=> "Certus aveho admove."

Parameters:

  • number (Integer) (defaults to: 256)
  • supplemental (Boolean) (defaults to: false)

Returns:



192
193
194
195
196
197
198
# File 'lib/faker/default/lorem.rb', line 192

def paragraph_by_chars(number: 256, supplemental: false)
  paragraph = paragraph(sentence_count: 3, supplemental: supplemental)

  paragraph += " #{paragraph(sentence_count: 3, supplemental: supplemental)}" while paragraph.length < number

  "#{paragraph[0...number - 1]}."
end

.paragraphs(number: 3, supplemental: false, exclude_words: nil) ⇒ Array

Generates three paragraphs

Examples:

Faker::Lorem.paragraphs
Faker::Lorem.paragraphs(number:2)
Faker::Lorem.paragraphs(number:2, supplemental: true)

Parameters:

  • number (Integer) (defaults to: 3)
  • supplemental (Boolean) (defaults to: false)

Returns:

  • (Array)


174
175
176
# File 'lib/faker/default/lorem.rb', line 174

def paragraphs(number: 3, supplemental: false, exclude_words: nil)
  1.upto(resolve(number)).collect { paragraph(sentence_count: 3, supplemental: supplemental, exclude_words: exclude_words) }
end

.question(word_count: 4, supplemental: false, random_words_to_add: 0, exclude_words: nil) ⇒ String

Returns the question with 4 words

Examples:

Faker::Lorem.question                                                               #=> "Natus deleniti sequi laudantium?"
Faker::Lorem.question(word_count: 2)                                                #=> "Quo ut?"
Faker::Lorem.question(word_count: 2, supplemental: true)                            #=> "Terga consequatur?"
Faker::Lorem.question(word_count: 2, supplemental: true, random_words_to_add: 2)    #=> "Depulso uter ut?"

Parameters:

  • word_count (Integer) (defaults to: 4)
  • supplemental (Boolean) (defaults to: false)
  • random_words_to_add (Integer) (defaults to: 0)

Returns:



216
217
218
# File 'lib/faker/default/lorem.rb', line 216

def question(word_count: 4, supplemental: false, random_words_to_add: 0, exclude_words: nil)
  words(number: word_count + rand(random_words_to_add), supplemental: supplemental, exclude_words: exclude_words).join(' ').capitalize + locale_question_mark
end

.questions(number: 3, supplemental: false, exclude_words: nil) ⇒ Array

Generates array of three questions

Examples:

Faker::Lorem.questions                                  #=> ["Amet culpa enim?", "Voluptatem deleniti numquam?", "Veniam non cum?"]
Faker::Lorem.questions(number: 2)                       #=> ["Minus occaecati nobis?", "Veniam et alias?"]
Faker::Lorem.questions(number: 2, supplemental: true)   #=> ["Acceptus subito cetera?", "Aro sulum cubicularis?"]

Parameters:

  • number (Integer) (defaults to: 3)
  • supplemental (Boolean) (defaults to: false)

Returns:

  • (Array)


234
235
236
# File 'lib/faker/default/lorem.rb', line 234

def questions(number: 3, supplemental: false, exclude_words: nil)
  1.upto(resolve(number)).collect { question(word_count: 3, supplemental: supplemental, exclude_words: exclude_words) }
end

.sentence(word_count: 4, supplemental: false, random_words_to_add: 0, exclude_words: nil) ⇒ String

Generates sentence

Examples:

Faker::Lorem.sentence                                                             #=> "Magnam qui aut quidem."
Faker::Lorem.sentence(word_count: 5)                                              #=> "Voluptas rerum aut aliquam velit."
Faker::Lorem.sentence(word_count: 5, supplemental: true)                          #=> "Aut viscus curtus votum iusto."
Faker::Lorem.sentence(word_count: 5, supplemental: true, random_words_to_add:2)   #=> "Crinis quo cruentus velit animi vomer."

Parameters:

  • word_count (Integer) (defaults to: 4)

    How many words should be there in a sentence, default to 4

  • supplemental (Boolean) (defaults to: false)

    Add supplemental words, default to false

  • random_words_to_add (Integer) (defaults to: 0)

    Add any random words, default to 0

Returns:



114
115
116
# File 'lib/faker/default/lorem.rb', line 114

def sentence(word_count: 4, supplemental: false, random_words_to_add: 0, exclude_words: nil)
  words(number: word_count + rand(random_words_to_add.to_i), supplemental: supplemental, exclude_words: exclude_words).join(locale_space).capitalize + locale_period
end

.sentences(number: 3, supplemental: false, exclude_words: nil) ⇒ Array

Generates three sentences

Examples:

Faker::Lorem.sentences                                  #=> ["Possimus non tenetur.", "Nulla non excepturi.", "Quisquam rerum facilis."]
Faker::Lorem.sentences(number: 2)                       #=> ["Nulla est natus.", "Perferendis autem cum."]
Faker::Lorem.sentences(number: 2, supplemental: true)   #=> ["Cito cena ad.", "Solvo animus allatus."]

Parameters:

  • number (Integer) (defaults to: 3)

    How many sentences to be generated, default to 3

  • supplemental (Boolean) (defaults to: false)

    Should add supplemental words, defaults to false

Returns:

  • (Array)

    Returns array for sentences.



132
133
134
# File 'lib/faker/default/lorem.rb', line 132

def sentences(number: 3, supplemental: false, exclude_words: nil)
  1.upto(resolve(number)).collect { sentence(word_count: 3, supplemental: supplemental, exclude_words: exclude_words) }
end

.word(exclude_words: nil) ⇒ String

Returs the random word

Examples:

Faker::Lorem.word   #=> "soluto"
Faker::Lorem.word(exclude_words: 'error') #=> "nisi"
Faker::Lorem.word(exclude_words: 'id, error') #=> "et"
Faker::Lorem.word(exclude_words: ['id', 'error']) #=> "consequatur"

Returns:



18
19
20
# File 'lib/faker/default/lorem.rb', line 18

def word(exclude_words: nil)
  words(number: 1, exclude_words: exclude_words).first
end

.words(number: 3, supplemental: false, exclude_words: nil) ⇒ Array

Generates random 3 words

Examples:

Faker::Lorem.words                                    #=> ["hic", "quia", "nihil"]
Faker::Lorem.words(number: 4)                         #=> ["est", "temporibus", "et", "quaerat"]
Faker::Lorem.words(number: 4, supplemental: true)    #=> ["nisi", "sit", "allatus", "consequatur"]
Faker::Lorem.words(number: 4, supplemental: true, exclude_words: 'sit') #=> ["nisi", "allatus", "consequatur", "aut"]

Parameters:

  • number (Integer) (defaults to: 3)

    Number of words to be generated

  • supplemental (Boolean) (defaults to: false)

    Whether to attach supplemental words at the end, default is false

Returns:

  • (Array)

    Array for words



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/faker/default/lorem.rb', line 37

def words(number: 3, supplemental: false, exclude_words: nil)
  resolved_num = resolve(number)
  word_list = (
    translate('faker.lorem.words') +
    (supplemental ? translate('faker.lorem.supplemental') : [])
  )
  if exclude_words
    exclude_words = exclude_words.split(', ') if exclude_words.instance_of?(::String)
    word_list -= exclude_words
  end
  word_list *= ((resolved_num / word_list.length) + 1)
  sample(word_list, resolved_num)
end