Class: Koota::Generator
- Inherits:
-
Object
- Object
- Koota::Generator
- Defined in:
- lib/koota/generator.rb
Overview
This class uses a parser, compiler, and VM to generate words.
Constant Summary collapse
- DEFAULT_CALL_OPTIONS =
{ words: 100, syllables: 1, syllable_separator: '', duplicates: false }.freeze
Instance Method Summary collapse
- #call(pattern, options = {}) ⇒ Object
-
#initialize(parser: Koota::Parser.new, compiler: Koota::Compiler.new, vm: Koota::VM.new) ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize(parser: Koota::Parser.new, compiler: Koota::Compiler.new, vm: Koota::VM.new) ⇒ Generator
Returns a new instance of Generator.
17 18 19 20 21 |
# File 'lib/koota/generator.rb', line 17 def initialize(parser: Koota::Parser.new, compiler: Koota::Compiler.new, vm: Koota::VM.new) @parser = parser @compiler = compiler @vm = vm end |
Instance Method Details
#call(pattern, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/koota/generator.rb', line 23 def call(pattern, = {}) = DEFAULT_CALL_OPTIONS.merge() bytecode = compile(pattern) syllables = if [:syllables].is_a?(Integer) -> { [:syllables] } elsif [:syllables].is_a?(Range) -> { rand([:syllables]) } else type = [:syllables].class.to_s raise ArgumentError, "expected Integer or Range for syllables option, not #{type}" end result = Array.new([:words]) do Array.new(syllables.call) { @vm.call(bytecode) }.join([:syllable_separator]) end result.uniq! unless [:duplicates] result end |