Sha256: fa966e814934e5a099e80bc6f779a65d6c0183458afd6877af79ccc2afdc5374

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

require 'find'
require 'taglib'
require 'pathname'
require 'logger'

module UnitF
  module Tag
    class File < Pathname
      def initialize(file_path)
        super(::File.absolute_path(file_path.to_s))
      end

      def tag
        @file.tag
      end

      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

      def cover_path
        "#{dirname}/cover.jpg"
      end

      def auto_tag_path
        "#{dirname}/.autotag"
      end

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

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

      def cover_available?
        ::File.exist?(cover_path)
      end

      def auto_cover!
        cover!(cover_path)
      end

      def auto_tag!
        title = ::File.basename(realpath.to_path)

        # This must come before gsubbing the title
        track = title.match(/^\s*\d+/).to_s.to_i

        title.gsub!(/\.\w+$/, '')
        title.gsub!(/^\d*\s*(-|\.)*\s*/, '')

        path_parts = realpath.dirname.to_path.split('/')
        album = path_parts[-1]
        artist = path_parts[-2]

        tag.album = album
        tag.artist = artist
        tag.title = title
        tag.track = track
        self.album_artist = artist
      end

      def save
        @file.save
      end

      def close
        @file&.close
        @file = nil
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unitf-tag-0.1.4 lib/unitf/tag/file.rb
unitf-tag-0.1.3 lib/unitf/tag/file.rb
unitf-tag-0.1.2 lib/unitf/tag/file.rb
unitf-tag-0.1.0 lib/unitf/tag/file.rb