Class: Sounder::SoundGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/sounder/sound_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sounds_hsh) ⇒ SoundGroup

Returns a new instance of SoundGroup.



5
6
7
8
9
10
# File 'lib/sounder/sound_group.rb', line 5

def initialize sounds_hsh
  @sounds = {}
  sounds_hsh.each do |name, file|
    @sounds[name.to_s] = Sounder::Sound.new file
  end
end

Instance Attribute Details

#soundsObject (readonly)

Returns the value of attribute sounds.



3
4
5
# File 'lib/sounder/sound_group.rb', line 3

def sounds
  @sounds
end

Instance Method Details

#play(name) ⇒ Object



12
13
14
15
# File 'lib/sounder/sound_group.rb', line 12

def play name
  sound = sounds.fetch(fuzzy_find name) { raise UnknownSoundError }
  sound.play
end

#randomObject



17
18
19
20
21
# File 'lib/sounder/sound_group.rb', line 17

def random
  @sounds.map do |_,sound|
    sound
  end.sample.play
end

#usageObject



23
24
25
26
27
28
29
30
# File 'lib/sounder/sound_group.rb', line 23

def usage
  [ "random (picks a random sound)",
    "<sound name> (it will fuzzy match the name)",
    "Available sounds:"
  ] + @sounds.keys.map do |sound|
    "  #{sound}"
  end
end