Class: Random
Constant Summary
collapse
- WORDS_PER_SENTENCE =
3..20
- SENTENCES_PER_PARAGRAPH =
3..8
- TLDS =
%w[com net org biz info gov]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(count = 1) ⇒ Random
Returns a new instance of Random.
38
39
40
|
# File 'lib/can_has_fixtures/random.rb', line 38
def initialize(count = 1)
@count = count
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
Also known as:
method_missing_without_singular_check
36
37
38
39
40
41
42
|
# File 'lib/can_has_fixtures/random.rb', line 36
def method_missing_with_singular_check(meth, *args, &block)
if Random.instance_methods(false).include?(method = English::Inflect.singular(meth.to_s))
(1..count).collect { send(method, *args, &block) }
else
method_missing_without_singular_check(meth, *args, &block)
end
end
|
Instance Attribute Details
Returns the value of attribute count.
8
9
10
|
# File 'lib/can_has_fixtures/random.rb', line 8
def count
@count
end
|
Class Method Details
.chain(target) ⇒ Object
10
11
12
|
# File 'lib/can_has_fixtures/random.rb', line 10
def self.chain(target)
Random.new(target)
end
|
.method_missing_with_instance_method_check(meth, *args, &block) ⇒ Object
Also known as:
method_missing
14
15
16
17
18
19
20
|
# File 'lib/can_has_fixtures/random.rb', line 14
def self.method_missing_with_instance_method_check(meth, *args, &block)
if instance_methods(false).include?(meth.to_s)
Random.new.send(meth, *args, &block)
else
method_missing_without_instance_method_check(meth, *args, &block)
end
end
|
Instance Method Details
56
57
58
|
# File 'lib/can_has_fixtures/random.rb', line 56
def boolean
[true, false].random
end
|
#default_paragraph_options ⇒ Object
96
97
98
|
# File 'lib/can_has_fixtures/random.rb', line 96
def default_paragraph_options
default_sentence_options.merge(:end_with_line => true)
end
|
#default_sentence_options ⇒ Object
80
81
82
|
# File 'lib/can_has_fixtures/random.rb', line 80
def default_sentence_options
default_word_options.merge(:punctuate => true)
end
|
#default_word_options ⇒ Object
67
68
69
|
# File 'lib/can_has_fixtures/random.rb', line 67
def default_word_options
{:used => true}
end
|
#dictionary ⇒ Object
42
43
44
|
# File 'lib/can_has_fixtures/random.rb', line 42
def dictionary
@@dictionary ||= load_dictionary
end
|
#email(options = {}) ⇒ Object
112
113
114
|
# File 'lib/can_has_fixtures/random.rb', line 112
def email(options = {})
"#{word(options)}@#{word(options)}.#{TLDS.random}"
end
|
#load_dictionary ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'lib/can_has_fixtures/random.rb', line 46
def load_dictionary
if File.exists?("/usr/share/dict/words")
File.read("/usr/share/dict/words").split
elsif File.exists?("/usr/dict/words")
File.read("/usr/dict/words").split
else
raise "words file not found"
end
end
|
#method_missing_with_singular_check(meth, *args, &block) ⇒ Object
Also known as:
method_missing
27
28
29
30
31
32
33
|
# File 'lib/can_has_fixtures/random.rb', line 27
def method_missing_with_singular_check(meth, *args, &block)
if Random.instance_methods(false).include?(method = English::Inflect.singular(meth.to_s))
(1..count).collect { send(method, *args, &block) }
else
method_missing_without_singular_check(meth, *args, &block)
end
end
|
#paragraph(options = default_paragraph_options) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/can_has_fixtures/random.rb', line 100
def paragraph(options = default_paragraph_options)
paragraph = SENTENCES_PER_PARAGRAPH.random.random.sentences(options).join(' ')
until satisfies?(paragraph, options)
paragraph = SENTENCES_PER_PARAGRAPH.random.random.sentences(options).join(' ')
end
paragraph << '\n' if options[:end_with_line]
@@used << paragraph if options[:end_with_line]
paragraph
end
|
#satisfies?(value, options) ⇒ Boolean
60
61
62
63
64
65
|
# File 'lib/can_has_fixtures/random.rb', line 60
def satisfies?(value, options)
return false if @@used.include?(value) if options[:unique]
return false if value.size > options[:max] if options[:max]
return false if value.size < options[:min] if options[:min]
true
end
|
#sentence(options = default_sentence_options) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/can_has_fixtures/random.rb', line 84
def sentence(options = default_sentence_options)
sentence = WORDS_PER_SENTENCE.random.random.words(options.merge(:used => false)).join(' ')
until satisfies?(sentence, options)
sentence = WORDS_PER_SENTENCE.random.random.words(options.merge(:used => false)).join(' ')
end
sentence << '.' if options[:punctuate]
@@used << sentence if options[:used]
sentence
end
|
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/can_has_fixtures/random.rb', line 116
def url
str = "#{%w[http https ftp].random}://"
if boolean
str << word
if boolean
str << ":#{word}"
end
str << "@"
end
if boolean
str << "www."
else
str << "#{word}."
end
str << "#{word}.#{TLDS.random}"
if boolean
str << "/#{word}"
if boolean
str << "/#{word}"
end
if boolean
str << "/#{word}.#{word}"
end
if boolean
str << "?#{word}=#{word}"
end
end
str
end
|
#word(options = default_word_options) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/can_has_fixtures/random.rb', line 71
def word(options = default_word_options)
word = dictionary.random
word = dictionary.random until satisfies?(word, options)
@@used << word if options[:used]
word
end
|