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

: (String source_library_path, String target_library_path, Device device) -> void



11
12
13
14
15
# File 'lib/wavesync/path_resolver.rb', line 11

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

Instance Method Details

#find_files_to_cleanup(target_path, audio) ⇒ Object

: (Pathname target_path, Audio audio) -> Array



31
32
33
34
35
36
37
38
39
40
# File 'lib/wavesync/path_resolver.rb', line 31

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

: (String source_file_path, Audio audio, ?target_file_type: String?) -> Pathname



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wavesync/path_resolver.rb', line 18

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

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

  target_path
end