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::Letters, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

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

Class Method Details

.characterObject


21
22
23
# File 'lib/faker/default/lorem.rb', line 21

def character
  sample(Types::CHARACTERS)
end

.characters(number: 255) ⇒ Object


25
26
27
# File 'lib/faker/default/lorem.rb', line 25

def characters(number: 255)
  Alphanumeric.alphanumeric(number: number)
end

.multibyteObject


29
30
31
# File 'lib/faker/default/lorem.rb', line 29

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) ⇒ Object


41
42
43
# File 'lib/faker/default/lorem.rb', line 41

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

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


49
50
51
52
53
54
55
# File 'lib/faker/default/lorem.rb', line 49

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) ⇒ Object


45
46
47
# File 'lib/faker/default/lorem.rb', line 45

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

.question(word_count: 4, supplemental: false, random_words_to_add: 0) ⇒ Object


57
58
59
# File 'lib/faker/default/lorem.rb', line 57

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

.questions(number: 3, supplemental: false) ⇒ Object


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

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

.sentence(word_count: 4, supplemental: false, random_words_to_add: 0) ⇒ Object


33
34
35
# File 'lib/faker/default/lorem.rb', line 33

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

.sentences(number: 3, supplemental: false) ⇒ Object


37
38
39
# File 'lib/faker/default/lorem.rb', line 37

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

.wordObject


7
8
9
# File 'lib/faker/default/lorem.rb', line 7

def word
  sample(translate('faker.lorem.words'))
end

.words(number: 3, supplemental: false) ⇒ Object


11
12
13
14
15
16
17
18
19
# File 'lib/faker/default/lorem.rb', line 11

def words(number: 3, supplemental: false)
  resolved_num = resolve(number)
  word_list = (
    translate('faker.lorem.words') +
    (supplemental ? translate('faker.lorem.supplemental') : [])
  )
  word_list *= ((resolved_num / word_list.length) + 1)
  shuffle(word_list)[0, resolved_num]
end