Class: Forgery::LoremIpsum

Inherits:
Forgery
  • Object
show all
Defined in:
lib/forgery/forgery/lorem_ipsum.rb

Constant Summary

Constant Summary

Constants inherited from Forgery

VERSION

Class Method Summary (collapse)

Methods inherited from Forgery

Extend, dictionaries, formats, load_from!, load_paths, rails?, rails_root

Class Method Details

+ (Object) character(options = {})



26
27
28
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 26

def self.character(options={})
  characters(1, options)
end

+ (Object) characters(quantity = 10, options = {})



30
31
32
33
34
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 30

def self.characters(quantity=10, options={})
  options.merge!(:random_limit => lorem_ipsum_characters.length-quantity) if quantity.is_a?(Fixnum)

  lorem_ipsum_characters[range_from_quantity(quantity, options)]
end

+ (Object) paragraph(options = {})



56
57
58
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 56

def self.paragraph(options={})
  paragraphs(1, options)
end

+ (Object) paragraphs(quantity = 2, options = {})



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 60

def self.paragraphs(quantity=2, options={})
  default_options = {:separator => "\n\n",
                         :wrap => {
                           :start => "",
                           :end => "" },
                         :html => false,
                         :sentences => 3}
  options = default_options.merge(options)
  options.merge!(:random_limit => (dictionaries[:lorem_ipsum].length/options[:sentences])-quantity) if quantity.is_a?(Fixnum)

  if options[:html]
    options[:wrap] = { :start => "<p>",
                       :end => "</p>" }
  end

  range = range_from_quantity(quantity, options)
  start = range.first * options[:sentences]

  paragraphs = []

  range.to_a.length.times do |i|
    paragraphs << (
      options[:wrap][:start] +
      dictionaries[:lorem_ipsum][start..(start+options[:sentences]-1)].join(" ") +
      options[:wrap][:end]
    )
    start += options[:sentences]
  end

  paragraphs.join(options[:separator])
end

+ (Object) sentence(options = {})



46
47
48
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 46

def self.sentence(options={})
  sentences(1, options)
end

+ (Object) sentences(quantity = 2, options = {})



50
51
52
53
54
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 50

def self.sentences(quantity=2, options={})
  options.merge!(:random_limit => (dictionaries[:lorem_ipsum].length-quantity)) if quantity.is_a?(Fixnum)

  dictionaries[:lorem_ipsum][range_from_quantity(quantity, options)].join(" ")
end

+ (Object) text(what = :sentence, quantity = 2, options = {})



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 3

def self.text(what=:sentence, quantity=2, options={})
  case what
  when :character
    character(options)
  when :characters
    characters(quantity, options)
  when :word
    word(options)
  when :words
    words(quantity, options)
  when :sentence
    sentence(options)
  when :sentences
    sentences(quantity, options)
  when :paragraph
    paragraph(options)
  when :paragraphs
    paragraphs(quantity, options)
  when :title
    title(options)
  end
end

+ (Object) title(options = {})



92
93
94
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 92

def self.title(options={})
  sentence(options).chop.gsub(/\b./){$&.upcase}
end

+ (Object) word(options = {})



36
37
38
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 36

def self.word(options={})
  words(1, options)
end

+ (Object) words(quantity = 10, options = {})



40
41
42
43
44
# File 'lib/forgery/forgery/lorem_ipsum.rb', line 40

def self.words(quantity=10, options={})
  options.merge!(:random_limit => lorem_ipsum_words.length-quantity) if quantity.is_a?(Fixnum)

  lorem_ipsum_words[range_from_quantity(quantity, options)].join(" ")
end