Class: RandomPhrase::Dictionary
- Inherits:
-
Object
- Object
- RandomPhrase::Dictionary
- Defined in:
- lib/random_phrase/dictionary.rb
Defined Under Namespace
Modules: DefaultLoader
Instance Attribute Summary collapse
-
#loader ⇒ Object
Returns the value of attribute loader.
Instance Method Summary collapse
-
#initialize(loader = nil) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #words(*args) ⇒ Object
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
#loader ⇒ Object
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) = args. @dictionary ||= loader.call().group_by {|word| word.length} result = @dictionary.values.flatten if .key?(:length) result = @dictionary.fetch([:length], []) end if .key?(:pattern) result = result.select { |w| w =~ Regexp.try_convert([:pattern]) } end result.flatten end |