Class: FactoryHelper::Lorem

Inherits:
Base
  • Object
show all
Defined in:
lib/factory-helper/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, fetch, flexible, letterify, method_missing, numerify, parse, rand_in_range, regexify, translate

Class Method Details

.characterObject



20
21
22
# File 'lib/factory-helper/lorem.rb', line 20

def character
  characters(1)
end

.characters(char_count = 255) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/factory-helper/lorem.rb', line 24

def characters(char_count= 255)
  return '' if char_count.respond_to?(:to_i) && char_count.to_i < 1
  char_count = resolve(char_count)
  FactoryHelper::Config.random.rand(36**char_count).to_s(36).
    rjust(char_count, '0').chars.to_a.
    shuffle(:random => FactoryHelper::Config.random).join
end

.paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3) ⇒ Object



44
45
46
# File 'lib/factory-helper/lorem.rb', line 44

def paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3)
  sentences(resolve(sentence_count) + FactoryHelper::Config.random.rand(random_sentences_to_add.to_i).to_i, supplemental).join(' ')
end

.paragraphs(paragraph_count = 3, supplemental = false) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/factory-helper/lorem.rb', line 48

def paragraphs(paragraph_count = 3, supplemental = false)
  [].tap do |paragraphs|
    1.upto(resolve(paragraph_count)) do
      paragraphs << paragraph(3, supplemental)
    end
  end
end

.sentence(word_count = 4, supplemental = false, random_words_to_add = 6) ⇒ Object



32
33
34
# File 'lib/factory-helper/lorem.rb', line 32

def sentence(word_count = 4, supplemental = false, random_words_to_add = 6)
  words(word_count + FactoryHelper::Config.random.rand(random_words_to_add.to_i).to_i, supplemental).join(' ').capitalize + '.'
end

.sentences(sentence_count = 3, supplemental = false) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/factory-helper/lorem.rb', line 36

def sentences(sentence_count = 3, supplemental = false)
  [].tap do |sentences|
    1.upto(resolve(sentence_count)) do
      sentences << sentence(3, supplemental)
    end
  end
end

.wordObject



6
7
8
# File 'lib/factory-helper/lorem.rb', line 6

def word
  translate('factory_helper.lorem.words').sample(:random => FactoryHelper::Config.random)
end

.words(num = 3, supplemental = false) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/factory-helper/lorem.rb', line 10

def words(num = 3, supplemental = false)
  resolved_num = resolve(num)
  word_list = (
    translate('factory_helper.lorem.words') +
    (supplemental ? translate('factory_helper.lorem.supplemental') : [])
  )
  word_list = word_list * ((resolved_num / word_list.length) + 1)
  word_list.shuffle(:random => FactoryHelper::Config.random)[0, resolved_num]
end