Class: Crazipsum

Inherits:
Object
  • Object
show all
Defined in:
lib/crazipsum.rb,
lib/crazipsum/version.rb

Constant Summary collapse

REGISTER =
Hash.new { Crazipsum.new([], fillers: false) }
VERSION =
'0.1.2'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.register(type, path, opts = {}) ⇒ Object


7
8
9
10
11
12
# File 'lib/crazipsum.rb', line 7

def register(type, path, opts = {})
  fillers        = opts[:fillers]
  opts[:fillers] = parse_dictionnary(local_dictionnary_path(:fillers)) if fillers.nil? || fillers
  words          = parse_dictionnary(path)
  REGISTER[type] = new(words, opts)
end

Instance Method Details

#paragraph(opts = {}) ⇒ Object Also known as: sentences


47
48
49
50
51
52
53
54
55
# File 'lib/crazipsum.rb', line 47

def paragraph(opts = {})
  quantity  = opts[:sentence_count] || rand(4..7)
  paragraph = []
  quantity.times do
    s = sentence(opts)
    paragraph << s unless s.empty?
  end
  paragraph.join(' ')
end

#paragraphs(opts = {}) ⇒ Object


58
59
60
61
62
63
64
65
66
# File 'lib/crazipsum.rb', line 58

def paragraphs(opts = {})
  quantity = opts[:paragraph_count] || rand(3..5)
  paragraphs = []
  quantity.times do
    p = paragraph(opts)
    paragraphs << p unless p.empty?
  end
  paragraphs.join(opts[:seperator] || "\n\n")
end

#sentence(opts = {}) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/crazipsum.rb', line 31

def sentence(opts = {})
  quantity         = opts[:word_count] || rand(7..15)
  quantity         = 0 if quantity < 0
  sentence_fillers = opts[:fillers]
  sentence_fillers = fillers if sentence_fillers.nil?
  dict             = dictionnary
  words = (0...quantity).map do
    next dict.sample unless sentence_fillers
    rand(3) == 0 ? dict.sample : sentence_fillers.sample
  end
  return '' if words.empty?
  words[0] = words[0].capitalize
  words    = words.join(' ')
  "#{words}."
end