Class: Wavesync::PathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/wavesync/path_resolver.rb

Constant Summary collapse

BPM_PATTERN =
/ \d+ bpm/

Instance Method Summary collapse

Constructor Details

#initialize(source_library_path, target_library_path, device) ⇒ PathResolver

Returns a new instance of PathResolver.



7
8
9
10
11
# File 'lib/wavesync/path_resolver.rb', line 7

def initialize(source_library_path, target_library_path, device)
  @source_library_path = Pathname(File.expand_path(source_library_path))
  @target_library_path = Pathname(File.expand_path(target_library_path))
  @device = device
end

Instance Method Details

#find_files_to_cleanup(target_path, audio) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/wavesync/path_resolver.rb', line 24

def find_files_to_cleanup(target_path, audio)
  return [] unless @device.bpm_source == :filename && audio.bpm

  ext = target_path.extname
  basename = target_path.basename(ext).to_s.gsub(BPM_PATTERN, '')

  pattern = target_path.dirname.join("#{basename}{, * bpm}#{ext}")
  Dir.glob(pattern.to_s).map { |f| Pathname(f) }
     .reject { |path| path == target_path }
end

#resolve(source_file_path, audio, target_file_type: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/wavesync/path_resolver.rb', line 13

def resolve(source_file_path, audio, target_file_type: nil)
  relative_path = Pathname(source_file_path).relative_path_from(@source_library_path)
  target_path = @target_library_path.join(relative_path)

  target_path = target_path.sub_ext(".#{target_file_type}") if target_file_type

  target_path = add_bpm_to_filename(target_path, audio.bpm) if @device.bpm_source == :filename && audio.bpm

  target_path
end