Class: Wavesync::Set

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

Constant Summary collapse

SETS_FOLDER =
'.sets'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library_path, name, tracks = []) ⇒ Set

: (String library_path, String name, ?Array tracks) -> void



48
49
50
51
52
# File 'lib/wavesync/set.rb', line 48

def initialize(library_path, name, tracks = [])
  @library_path = library_path
  @name = name
  @tracks = tracks.dup
end

Instance Attribute Details

#library_pathObject (readonly)

: String



13
14
15
# File 'lib/wavesync/set.rb', line 13

def library_path
  @library_path
end

#nameObject (readonly)

: String



11
12
13
# File 'lib/wavesync/set.rb', line 11

def name
  @name
end

#tracksObject (readonly)

: Array



12
13
14
# File 'lib/wavesync/set.rb', line 12

def tracks
  @tracks
end

Class Method Details

.all(library_path) ⇒ Object

: (String library_path) -> Array



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

def self.all(library_path)
  path = sets_path(library_path)
  return [] unless Dir.exist?(path)

  Dir.glob(File.join(path, '*.yml')).map do |file|
    data = YAML.load_file(file)
    new(library_path, data['name'], expand_tracks(library_path, data['tracks']))
  end.sort_by(&:name)
end

.exists?(library_path, name) ⇒ Boolean

: (String library_path, String name) -> bool

Returns:

  • (Boolean)


43
44
45
# File 'lib/wavesync/set.rb', line 43

def self.exists?(library_path, name)
  File.exist?(set_path(library_path, name))
end

.load(library_path, name) ⇒ Object

: (String library_path, String name) -> Set



26
27
28
29
# File 'lib/wavesync/set.rb', line 26

def self.load(library_path, name)
  data = YAML.load_file(set_path(library_path, name))
  new(library_path, data['name'], expand_tracks(library_path, data['tracks']))
end

.set_path(library_path, name) ⇒ Object

: (String library_path, String name) -> String



21
22
23
# File 'lib/wavesync/set.rb', line 21

def self.set_path(library_path, name)
  File.join(sets_path(library_path), "#{name}.yml")
end

.sets_path(library_path) ⇒ Object

: (String library_path) -> String



16
17
18
# File 'lib/wavesync/set.rb', line 16

def self.sets_path(library_path)
  File.join(library_path, SETS_FOLDER)
end

Instance Method Details

#add_track(path) ⇒ Object

: (String path) -> void



55
56
57
# File 'lib/wavesync/set.rb', line 55

def add_track(path)
  @tracks << path
end

#move_down(index) ⇒ Object

: (Integer index) -> void



72
73
74
75
76
# File 'lib/wavesync/set.rb', line 72

def move_down(index)
  return if index >= @tracks.size - 1

  @tracks[index], @tracks[index + 1] = @tracks[index + 1], @tracks[index]
end

#move_up(index) ⇒ Object

: (Integer index) -> void



65
66
67
68
69
# File 'lib/wavesync/set.rb', line 65

def move_up(index)
  return if index <= 0

  @tracks[index], @tracks[index - 1] = @tracks[index - 1], @tracks[index]
end

#remove_track(index) ⇒ Object

: (Integer index) -> String?



60
61
62
# File 'lib/wavesync/set.rb', line 60

def remove_track(index)
  @tracks.delete_at(index)
end

#saveObject

: () -> void



79
80
81
82
# File 'lib/wavesync/set.rb', line 79

def save
  FileUtils.mkdir_p(self.class.sets_path(@library_path))
  File.write(self.class.set_path(@library_path, @name), to_yaml)
end