Module: Drakkon::Sounds::Index

Defined in:
lib/drakkon/lib/sounds/sounds.rb

Overview

General Sound Index Helper

Class Method Summary collapse

Class Method Details

.build_indexObject



54
55
56
# File 'lib/drakkon/lib/sounds/sounds.rb', line 54

def self.build_index
  check sounds_directory
end

.catalogObject



17
18
19
20
21
22
# File 'lib/drakkon/lib/sounds/sounds.rb', line 17

def self.catalog
  # Better way to do this?
  @catalog ||= Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }

  @catalog
end

.check(dir = nil) ⇒ Object

Recursively Go through



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/drakkon/lib/sounds/sounds.rb', line 76

def self.check(dir = nil)
  LogBot.info('Sound Index', "Check: #{dir}")

  # Collect Sounds
  list = file_list

  # Ignore Empties
  return if list.empty?

  # Do other things
  Dir["#{dir}/*"].each do |file|
    if File.directory?(file)
      check(file)
      next
    end

    process(file)
  end

  :done
end

.contextObject



11
12
13
14
15
# File 'lib/drakkon/lib/sounds/sounds.rb', line 11

def self.context
  @context ||= Dir.pwd

  @context
end

.digestObject



28
29
30
# File 'lib/drakkon/lib/sounds/sounds.rb', line 28

def self.digest
  Digest::MD5.hexdigest(file_list.map { |x| Digest::MD5.file(x).hexdigest }.join)
end

.file_listObject



24
25
26
# File 'lib/drakkon/lib/sounds/sounds.rb', line 24

def self.file_list
  Dir["#{sounds_directory}/**/*"].select { |file| File.file?(file) }
end

.indexObject



5
6
7
8
9
# File 'lib/drakkon/lib/sounds/sounds.rb', line 5

def self.index
  @index ||= {}

  @index
end

.process(file = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/drakkon/lib/sounds/sounds.rb', line 98

def self.process(file = nil)
  # Safety
  return if file.nil?

  full_name = file.gsub(sounds_directory, '')[1..].gsub(File.extname(file), '')

  name = File.basename(file, File.extname(file))
  path = file.gsub(sounds_directory, '')[1..]
  index[full_name] = path

  details = { path: full_name }
  tag = WahWah.open(file)
  details[:duration_s] = tag.duration
  details[:duration_tick] = tag.duration * 60

  # Catalog
  catalog_list = path.split('/')[0..-2]
  catalog_location = []
  catalog_list.each do |idx|
    catalog_location.push idx
    catalog.dig(*catalog_location)
  end
  catalog.dig(*catalog_list)[name] = details
end

.resultObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/drakkon/lib/sounds/sounds.rb', line 58

def self.result
  <<~RB
       module Drakkon
         module Sounds
           def self.index
             #{index.inspect}
           end

    def self.catalog
             #{catalog.inspect}
           end

         end
       end
  RB
end

.run!(force: false, dir: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/drakkon/lib/sounds/sounds.rb', line 36

def self.run!(force: false, dir: nil)
  @context = dir || Dir.pwd

  # Create Directory if sprites directory is missing
  FileUtils.mkdir_p(sounds_directory)

  if Settings.config[:sound_digest] == digest && File.exist?("#{context}/app/drakkon/sound_index.rb")
    LogBot.info('Sounds Index', 'Nothing New')
    return unless force
  end

  build_index

  Settings.update(:sound_digest, digest)

  File.write("#{context}/app/drakkon/sound_index.rb", result)
end

.sounds_directoryObject



32
33
34
# File 'lib/drakkon/lib/sounds/sounds.rb', line 32

def self.sounds_directory
  "#{context}/sounds"
end