Module: RandomPhrase

Defined in:
lib/random_phrase.rb,
lib/random_phrase/version.rb

Defined Under Namespace

Classes: Dictionary

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.dictionaryObject



11
12
13
# File 'lib/random_phrase.rb', line 11

def dictionary
	@@dictionary ||= RandomPhrase::Dictionary.new
end

.dictionary=(dictionary) ⇒ Object



7
8
9
# File 'lib/random_phrase.rb', line 7

def dictionary=(dictionary)
	@@dictionary = dictionary
end

.phrase(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/random_phrase.rb', line 15

def phrase(*args)
	options = args.extract_options!
	word_patterns = []
	
	unless args.empty?
		if args.first.respond_to?(:times)
			word_count = args.first
		elsif args.first.kind_of?(Regexp)
			word_patterns = args.first.source.split(/(?:\s|\\\\s)/)
			word_count = word_patterns.count
		elsif args.first.respond_to?(:to_s)
			word_count = args.first.to_s.split(/\s/).count
		end
	end
	
	word_count ||= 1
	phrase = []
	word_count.times do |i|
		options.merge!(:pattern => Regexp.compile(word_patterns[i])) unless word_patterns.empty?
		phrase << dictionary.words(options).sample
	end
	phrase.join(" ")
end