Class: Charosc::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/charosc/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, options = {}) ⇒ Generator

str - input String



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/charosc/generator.rb', line 8

def initialize(str, options = {})
  @chars  = Text.new(str).chars
  @markov = Markov.new(@chars)

  @options     = default_options.merge(options)
  @depth       = @options[:depth]
  @mod_enabled = @options[:mod_enabled]
  @mod_inc     = @options[:mod_inc]
  @mod_top     = @options[:mod_top]
  @mod_bottom  = @options[:mod_bottom]

  @oscil = Oscillator.new(
    depth,
    top:    mod_top,
    bottom: mod_bottom,
    inc:    mod_inc
  )
end

Instance Attribute Details

#charsObject (readonly)

Returns the value of attribute chars.



3
4
5
# File 'lib/charosc/generator.rb', line 3

def chars
  @chars
end

#depthObject

Returns the value of attribute depth.



4
5
6
# File 'lib/charosc/generator.rb', line 4

def depth
  @depth
end

#mod_bottomObject

Returns the value of attribute mod_bottom.



4
5
6
# File 'lib/charosc/generator.rb', line 4

def mod_bottom
  @mod_bottom
end

#mod_enabledObject

Returns the value of attribute mod_enabled.



4
5
6
# File 'lib/charosc/generator.rb', line 4

def mod_enabled
  @mod_enabled
end

#mod_incObject

Returns the value of attribute mod_inc.



4
5
6
# File 'lib/charosc/generator.rb', line 4

def mod_inc
  @mod_inc
end

#mod_topObject

Returns the value of attribute mod_top.



4
5
6
# File 'lib/charosc/generator.rb', line 4

def mod_top
  @mod_top
end

#oscilObject

Returns the value of attribute oscil.



4
5
6
# File 'lib/charosc/generator.rb', line 4

def oscil
  @oscil
end

Instance Method Details

#generate(num_chars, &block) ⇒ Object

Public: Generate text

num_chars - Number of characters to generate block - Yields to block after output is concatenated

Returns String



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/charosc/generator.rb', line 33

def generate(num_chars, &block)
  output = rand_char

  until output.size > num_chars
    seq = @markov.get_sequence(output[-1], next_depth)
    output += seq.join("")

    yield seq if block_given?
  end

  format_text(output[0..(num_chars - 1)])
end