Class: TextToNoise::Router

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/text_to_noise/router.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initializeRouter

Returns a new instance of Router.



6
7
8
# File 'lib/text_to_noise/router.rb', line 6

def initialize()
  @mappings = []
end

Instance Attribute Details

#mappingsObject

Returns the value of attribute mappings.



4
5
6
# File 'lib/text_to_noise/router.rb', line 4

def mappings
  @mappings
end

Class Method Details

.parse(config) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/text_to_noise/router.rb', line 10

def self.parse( config )
  raise ArgumentError, "No configuration given" if config.nil? or config.strip.empty?
  new.tap { |m|
    m.instance_eval config
    m.info "Configured #{m.mappings.size} mappings."
  }
end

Instance Method Details

#dispatch(line) ⇒ Object



18
19
20
21
22
23
# File 'lib/text_to_noise/router.rb', line 18

def dispatch( line )
  debug "Dispatching: #{line.rstrip}"
  matches = mappings.select { |m| m === line }
  debug "#{matches.size} matched mappings"
  matches.map { |m| m.call }
end

#match(expression, &block) ⇒ Object Also known as: map



29
30
31
32
33
# File 'lib/text_to_noise/router.rb', line 29

def match( expression, &block )
  debug "Creating map for #{expression.inspect}"
  mappings << mapping = NoiseMapping.new( expression, &block )
  mapping
end

#sound_path(path) ⇒ Object



25
26
27
# File 'lib/text_to_noise/router.rb', line 25

def sound_path( path )
  TextToNoise.player.sound_dirs << path
end