Class: TextToNoise::NoiseMapping

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(expression_or_map, &block) ⇒ NoiseMapping

Returns a new instance of NoiseMapping.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/text_to_noise/noise_mapping.rb', line 7

def initialize( expression_or_map, &block )
  debug "Parsing expression: #{expression_or_map.inspect}"

  @matcher_conditions = []
  @regex = nil
  
  case expression_or_map
  when Regexp
    @regex = expression_or_map
  when Hash
    expression_or_map.each do |k,v|
      case k
      when Regexp
        @regex = k
        self.to v
      else
        self.send k.to_sym, v
      end
    end
  else
    raise ArgumentError, "Unrecognized Mapping configuration: #{expression_or_map.inspect}"
  end

  raise ArgumentError,
  "Bad configuration line.  Missing match expression in \"#{expression_or_map.inspect}\"" if @regex.nil?

  matcher_conditions << block if block_given?
end

Instance Attribute Details

#matcher_conditionsObject

Returns the value of attribute matcher_conditions.



5
6
7
# File 'lib/text_to_noise/noise_mapping.rb', line 5

def matcher_conditions
  @matcher_conditions
end

#targetsObject

Returns the value of attribute targets.



5
6
7
# File 'lib/text_to_noise/noise_mapping.rb', line 5

def targets
  @targets
end

Instance Method Details

#===(other) ⇒ Object



36
37
38
39
# File 'lib/text_to_noise/noise_mapping.rb', line 36

def ===( other )
  match_data = @regex.match( other )
  !match_data.nil? && play?( match_data )
end

#callObject



70
71
72
73
# File 'lib/text_to_noise/noise_mapping.rb', line 70

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

#every(iteration_count) ⇒ Object



60
61
62
63
# File 'lib/text_to_noise/noise_mapping.rb', line 60

def every( iteration_count )
  @matcher_conditions << IterationMappingCondition.new( iteration_count )
  self
end

#targetObject



76
77
78
79
80
# File 'lib/text_to_noise/noise_mapping.rb', line 76

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

#to(sound_or_sounds) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/text_to_noise/noise_mapping.rb', line 41

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 "#{self.class.name} : #{@regex.inspect} -> #{sound}"
      TextToNoise.player.play s
    }
  end

  self
end

#when(&clause) ⇒ Object



65
66
67
68
# File 'lib/text_to_noise/noise_mapping.rb', line 65

def when( &clause )
  @matcher_conditions << clause
  self
end