Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 68 lines (57 sloc) 3.392 kb
0e02772 Caius Durling First Commit -- v0.3
authored
1 #
2 # LoremGenerator.rb
3 # LoremGenerator
4 #
5 # Created by Caius Durling on 01/04/2008.
6 # Copyright (c) 2008 __MyCompanyName__. All rights reserved.
7 #
8 # We only return strings from this to make it easier on ourselves elsewhere
9
10
11 class Array
12 # Choose a random element
13 def rand
14 self[Kernel.rand(length)]
15 end
16
17 # Shuffle into a random order
18 def shuffle
19 self.sort_by{Kernel.rand}
20 end
21 end
22
23 # Generates random text
24 # Most of this ripped straight out of the faker gem
25 class LoremGenerator
26
27 Words = %w( alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat )
28
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
29 def self.words(num = 3, prepend = false, wrap_tags = false )
0e02772 Caius Durling First Commit -- v0.3
authored
30 # ignore prepend here
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
31 out = Words.shuffle[0, num]
32 out.join(" ")
0e02772 Caius Durling First Commit -- v0.3
authored
33 end
34
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
35 def self.sentence(word_count = 4, prepend = false, wrap_tags = false )
0e02772 Caius Durling First Commit -- v0.3
authored
36 out = []
37 out << "lorem ipsum dolor" if prepend
38 out << words(word_count + rand(6))
39 out.join(" ").capitalize + '.'
40 end
41
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
42 def self.sentences(sentence_count = 3, prepend = false, wrap_tags = false )
0e02772 Caius Durling First Commit -- v0.3
authored
43 sentences = []
44 1.upto(sentence_count) do |i|
45 sentences << sentence( rand(5) + 3, prepend ) if i == 1
46 sentences << sentence unless i == 1
47 end
48 sentences.join(" ")
49 end
50
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
51 def self.paragraph(sentence_count = 3, prepend = false, wrap_tags = false )
52 out = []
53 out << "<p>\n\t" if wrap_tags
54 out << sentences(sentence_count + rand(3), prepend )
55 out << "\n</p>" if wrap_tags
56 out.join
0e02772 Caius Durling First Commit -- v0.3
authored
57 end
58
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
59 def self.paragraphs(paragraph_count = 3, prepend = false, wrap_tags = false )
0e02772 Caius Durling First Commit -- v0.3
authored
60 paragraphs = []
61 1.upto(paragraph_count) do |i|
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
62 paragraphs << paragraph( rand(5) + 3, prepend, wrap_tags ) if i == 1
63 paragraphs << paragraph( rand(5) + 3, false, wrap_tags ) unless i == 1
0e02772 Caius Durling First Commit -- v0.3
authored
64 end
65 paragraphs.join("\n\n")
66 end
67
e7f87c0 Caius Durling Adding the option to generate paragraphs wrapped in <p> tags
authored
68 end
Something went wrong with that request. Please try again.