Sha256: f7b366dce3270d23286ac479da53917a640094dc53e64c34accf54ef701670bb

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module VER
  class Syntax
    module Detector
      module_function

      EXTS_LIST = {}
      HEAD_LIST = {}

      def exts(name, exts)
        EXTS_LIST[name] = exts
      end

      def head(name, head)
        HEAD_LIST[name] = head
      end

      def names
        [EXTS_LIST.keys, HEAD_LIST.keys].flatten.compact.sort
      end

      def detect(filename, override_name = nil)
        path = Pathname(filename.to_s)
        path = path.readlink if path.symlink?

        VER.load('detect')

        override_name || detect_ext(path) || detect_head(path)
      end

      def detect_ext(path)
        basename = path.basename.to_s

        EXTS_LIST.each do |name, exts|
          exts.each do |ext|
            if ext == basename
              return name
            elsif basename.end_with?(".#{ext}")
              return name
            end
          end

        end

        nil
      end

      def detect_head(path)
        line = path.open{|io| io.gets }
        return unless line && line.valid_encoding?

        HEAD_LIST.find do |name, head|
          return name if line =~ head
        end
      rescue Errno::ENOENT
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ver-2010.08 lib/ver/syntax/detector.rb
ver-2010.02 lib/ver/syntax/detector.rb