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

Returns a new instance of Set.



39
40
41
42
43
# File 'lib/wavesync/set.rb', line 39

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

Instance Attribute Details

#library_pathObject (readonly)

Returns the value of attribute library_path.



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

def library_path
  @library_path
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tracksObject (readonly)

Returns the value of attribute tracks.



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

def tracks
  @tracks
end

Class Method Details

.all(library_path) ⇒ Object



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

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/wavesync/set.rb', line 35

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

.load(library_path, name) ⇒ Object



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

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



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

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

.sets_path(library_path) ⇒ Object



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

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

Instance Method Details

#add_track(path) ⇒ Object



45
46
47
# File 'lib/wavesync/set.rb', line 45

def add_track(path)
  @tracks << path
end

#move_down(index) ⇒ Object



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

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



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

def move_up(index)
  return if index <= 0

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

#remove_track(index) ⇒ Object



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

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

#saveObject



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

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