Class: UnitF::Tag::File
- Inherits:
-
Object
- Object
- UnitF::Tag::File
- Defined in:
- lib/unitf/tag/file.rb
Instance Attribute Summary collapse
-
#dirname ⇒ Object
Returns the value of attribute dirname.
-
#extname ⇒ Object
Returns the value of attribute extname.
-
#path ⇒ Object
Returns the value of attribute path.
-
#realpath ⇒ Object
Returns the value of attribute realpath.
Instance Method Summary collapse
- #auto_cover! ⇒ Object
- #auto_tag! ⇒ Object
- #auto_tags ⇒ Object
- #close ⇒ Object
- #cover_available? ⇒ Boolean
- #cover_path ⇒ Object
- #flac? ⇒ Boolean
- #format_json ⇒ Object
- #format_line ⇒ Object
- #info ⇒ Object
-
#initialize(file_path) ⇒ File
constructor
A new instance of File.
- #mp3? ⇒ Boolean
- #open {|file| ... } ⇒ Object
- #print ⇒ Object
- #properties!(properties) ⇒ Object
- #save ⇒ Object
-
#tag ⇒ Object
Methods that use @file, which comes from child classes.
- #to_s ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(file_path) ⇒ File
Returns a new instance of File.
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
#dirname ⇒ Object
Returns the value of attribute dirname.
10 11 12 |
# File 'lib/unitf/tag/file.rb', line 10 def dirname @dirname end |
#extname ⇒ Object
Returns the value of attribute extname.
10 11 12 |
# File 'lib/unitf/tag/file.rb', line 10 def extname @extname end |
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/unitf/tag/file.rb', line 10 def path @path end |
#realpath ⇒ Object
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() tag.album = [:album] tag.artist = [:artist] tag.title = [:title] tag.track = [:track] if .key?(:track) tag.year = [:year] if .key?(:year) self.album_artist = [:artist] end |
#auto_tags ⇒ Object
96 97 98 |
# File 'lib/unitf/tag/file.rb', line 96 def @auto_tags ||= UnitF::Tag::AutoTags.new(path) end |
#close ⇒ Object
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
81 82 83 |
# File 'lib/unitf/tag/file.rb', line 81 def cover_available? !cover_path.nil? end |
#cover_path ⇒ Object
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
77 78 79 |
# File 'lib/unitf/tag/file.rb', line 77 def flac? extname.match(/\.flac$/i) end |
#format_json ⇒ Object
27 28 29 |
# File 'lib/unitf/tag/file.rb', line 27 def format_json JSON.pretty_generate(info) end |
#format_line ⇒ Object
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 |
#info ⇒ Object
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
73 74 75 |
# File 'lib/unitf/tag/file.rb', line 73 def mp3? extname.match(/\.mp3$/i) end |
#open {|file| ... } ⇒ Object
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 |
#print ⇒ Object
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 |
#save ⇒ Object
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 |
#tag ⇒ Object
Methods that use @file, which comes from child classes
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_s ⇒ Object
23 24 25 |
# File 'lib/unitf/tag/file.rb', line 23 def to_s @path end |
#update ⇒ Object
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 |