Class: RandomPhrase::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/random_phrase/dictionary.rb

Defined Under Namespace

Modules: DefaultLoader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader = nil) ⇒ Dictionary

Returns a new instance of Dictionary.



16
17
18
19
# File 'lib/random_phrase/dictionary.rb', line 16

def initialize(loader = nil)
	loader = Proc.new {loader} unless loader.nil? || loader.kind_of?(Proc)
	self.loader = loader ||= Proc.new { DefaultLoader.load }
end

Instance Attribute Details

#loaderObject

Returns the value of attribute loader.



4
5
6
# File 'lib/random_phrase/dictionary.rb', line 4

def loader
  @loader
end

Instance Method Details

#words(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/random_phrase/dictionary.rb', line 22

def words(*args)
	options = args.extract_options!

	@dictionary ||= loader.call().group_by {|word| word.length}
	result = @dictionary.values.flatten
	if options.key?(:length)
		result = @dictionary.fetch(options[:length], [])
	end
	
	if options.key?(:pattern)
		result = result.select { |w| w =~ Regexp.try_convert(options[:pattern]) }
	end
	
	result.flatten
end