Class: UnitF::Tag::File

Inherits:
Object
  • Object
show all
Defined in:
lib/unitf/tag/file.rb

Direct Known Subclasses

FLAC, MP3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ File

Returns a new instance of File.

Raises:



12
13
14
15
16
17
18
19
20
21
# File 'lib/unitf/tag/file.rb', line 12

def initialize(file_path)
  raise Error, "Invalid file #{file_path}" unless ::File.exist?(file_path)

  @path = ::File.path(file_path.to_s)
  @realpath = ::File.realpath(path)
  @dirname = ::File.dirname(path)
  @extname = ::File.extname(path)

  raise Error, "Unknown file type: #{file_path}" unless mp3? || flac?
end

Instance Attribute Details

#dirnameObject

Returns the value of attribute dirname.



10
11
12
# File 'lib/unitf/tag/file.rb', line 10

def dirname
  @dirname
end

#extnameObject

Returns the value of attribute extname.



10
11
12
# File 'lib/unitf/tag/file.rb', line 10

def extname
  @extname
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/unitf/tag/file.rb', line 10

def path
  @path
end

#realpathObject

Returns the value of attribute realpath.



10
11
12
# File 'lib/unitf/tag/file.rb', line 10

def realpath
  @realpath
end

Instance Method Details

#auto_cover!Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/unitf/tag/file.rb', line 85

def auto_cover!
  raise Error, "File is not open #{self.class.name}" if tag.nil?

  cover!(cover_path) if cover_available?
  true
rescue StandardError => e
  UnitF::Log.error("Failed to auto-cover file #{e}")
  puts e.backtrace
  false
end

#auto_tag!Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/unitf/tag/file.rb', line 100

def auto_tag!
  UnitF::Log.info("Auto tagging #{self}")
  UnitF::Log.info(auto_tags)

  tag.album = auto_tags[:album]
  tag.artist = auto_tags[:artist]
  tag.title = auto_tags[:title]
  tag.track = auto_tags[:track] if auto_tags.key?(:track)
  tag.year = auto_tags[:year] if auto_tags.key?(:year)
  self.album_artist = auto_tags[:artist]
end

#auto_tagsObject



96
97
98
# File 'lib/unitf/tag/file.rb', line 96

def auto_tags
  @auto_tags ||= UnitF::Tag::AutoTags.new(path)
end

#closeObject

Raises:



153
154
155
156
157
158
# File 'lib/unitf/tag/file.rb', line 153

def close
  raise Error, "File is not open #{self.class.name}" if @file&.tag.nil?

  @file&.close
  @file = nil
end

#cover_available?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/unitf/tag/file.rb', line 81

def cover_available?
  !cover_path.nil?
end

#cover_pathObject



65
66
67
68
69
70
71
# File 'lib/unitf/tag/file.rb', line 65

def cover_path
  ["#{dirname}/cover.jpg", "#{dirname}/../cover.jpg"].each do |path|
    return ::File.realpath(path) if ::File.exist?(path)
  end

  nil
end

#flac?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/unitf/tag/file.rb', line 77

def flac?
  extname.match(/\.flac$/i)
end

#format_jsonObject



27
28
29
# File 'lib/unitf/tag/file.rb', line 27

def format_json
  JSON.pretty_generate(info)
end

#format_lineObject



31
32
33
34
35
36
37
# File 'lib/unitf/tag/file.rb', line 31

def format_line
  buff = []
  info.each_key do |key|
    buff << "#{key}=#{info[key]}"
  end
  buff.join(',')
end

#infoObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/unitf/tag/file.rb', line 39

def info
  {
    file: realpath,
    artist: tag.artist,
    album: tag.album,
    title: tag.title,
    track: tag.track,
    genre: tag.genre,
    year: tag.year,
    cover: cover?
  }
end

#mp3?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/unitf/tag/file.rb', line 73

def mp3?
  extname.match(/\.mp3$/i)
end

#open {|file| ... } ⇒ Object

Yields:

  • (file)


128
129
130
131
132
133
134
135
136
# File 'lib/unitf/tag/file.rb', line 128

def open
  file = if flac?
           UnitF::Tag::FLAC.new(path)
         elsif mp3?
           UnitF::Tag::MP3.new(path)
         end
  yield(file) if block_given?
  file.close
end


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/unitf/tag/file.rb', line 52

def print
  puts "File  : #{realpath}"
  puts "Artist: #{tag.artist}"
  puts "Album : #{tag.album}"
  puts "Title : #{tag.title}"
  puts "Track : #{tag.track}"
  puts "Genre : #{tag.genre}"
  puts "Year  : #{tag.year}"
  puts "Cover : #{cover?}"
  puts "Stats : #{stats}"
  puts
end

#properties!(properties) ⇒ Object



112
113
114
115
116
117
# File 'lib/unitf/tag/file.rb', line 112

def properties!(properties)
  properties.each_pair do |property, value|
    UnitF::Log.info("Setting #{property} to #{value}")
    tag.send("#{property}=", value)
  end
end

#saveObject

Raises:



147
148
149
150
151
# File 'lib/unitf/tag/file.rb', line 147

def save
  raise Error, "File is not open #{self.class.name}" if @file&.tag.nil?

  @file&.save
end

#tagObject

Methods that use @file, which comes from child classes

Raises:



141
142
143
144
145
# File 'lib/unitf/tag/file.rb', line 141

def tag
  raise Error, "File is not open #{self.class.name}" if @file&.tag.nil?

  @file&.tag
end

#to_sObject



23
24
25
# File 'lib/unitf/tag/file.rb', line 23

def to_s
  @path
end

#updateObject



119
120
121
122
123
124
125
126
# File 'lib/unitf/tag/file.rb', line 119

def update
  open do |file|
    if block_given?
      yield(file)
      file.save || (raise Error, "Failed to save file #{file}")
    end
  end
end