Class: TextToNoise::Mapping

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(expression_or_map, &block) ⇒ Mapping

Returns a new instance of Mapping.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/text_to_noise/mapping.rb', line 6

def initialize( expression_or_map, &block )
  case expression_or_map
  when Regexp
    @regex = expression_or_map
  when Hash
    @regex = expression_or_map.keys.first
    self.to expression_or_map[@regex]
  else
    raise ArgumentError, "Unrecognized Mapping configuration: #{expression_or_map.inspect}"
  end

  @matcher_proc = block if block_given?
end

Instance Attribute Details

#matcher_procObject

Returns the value of attribute matcher_proc.



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

def matcher_proc
  @matcher_proc
end

#targetsObject

Returns the value of attribute targets.



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

def targets
  @targets
end

Instance Method Details

#===(other) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/text_to_noise/mapping.rb', line 20

def ===( other )
  match_data = @regex.match( other )
  if matcher_proc
    match_data && matcher_proc.call( match_data )
  else
    not match_data.nil?
  end
end

#callObject



46
47
48
49
# File 'lib/text_to_noise/mapping.rb', line 46

def call()
  debug "Calling '#{@regex.inspect}' target..."
  target.call
end

#targetObject



52
53
54
55
56
# File 'lib/text_to_noise/mapping.rb', line 52

def target()
  @i = -1 unless @i
  @i = (@i + 1) % targets.size
  self.targets[@i]
end

#to(sound_or_sounds) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/text_to_noise/mapping.rb', line 29

def to( sound_or_sounds )
  if sound_or_sounds.is_a? Array
    sounds = sound_or_sounds
  else
    sounds = [sound_or_sounds]
  end
  
  sounds.each do |sound|
    s = sound
    s += ".wav" unless sound =~ /.wav$/
    self.targets << Proc.new {
      info "#{@regex.inspect} -> #{sound}"
      TextToNoise.player.play s
    }
  end
end