aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/container/criteria.rb
blob: 66ecb8c874d7ef0764e53d04173b128f1af58bed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Hbc
  class Container
    class Criteria
      attr_reader :path, :command

      def initialize(path, command)
        @path = path
        @command = command
      end

      def extension(regex)
        path.extname.sub(/^\./, "") =~ Regexp.new(regex.source, regex.options | Regexp::IGNORECASE)
      end

      def magic_number(regex)
        # 262: length of the longest regex (currently: Hbc::Container::Tar)
        @magic_number ||= File.open(@path, "rb") { |f| f.read(262) }
        @magic_number =~ regex
      end
    end
  end
end