Class: LanguageSniffer::Pathname

Inherits:
Object
  • Object
show all
Defined in:
lib/language_sniffer/pathname.rb

Overview

Similar to ::Pathname, LanguageSniffer::Pathname wraps a path string and provides helpful query methods. Its useful when you only have a filename but not a blob and need to figure out the language of the file.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Pathname

Public: Initialize a Pathname

path - A filename String. The file may or maybe actually exist.

Returns a Pathname.



13
14
15
# File 'lib/language_sniffer/pathname.rb', line 13

def initialize(path)
  @path = path
end

Instance Method Details

#basenameObject

Public: Get the basename of the path

Examples

Pathname.new('sub/dir/file.rb').basename
# => 'file.rb'

Returns a String.



25
26
27
# File 'lib/language_sniffer/pathname.rb', line 25

def basename
  File.basename(@path)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


66
67
68
# File 'lib/language_sniffer/pathname.rb', line 66

def eql?(other)
  other.is_a?(self.class) && @path == other.to_s
end

#extnameObject

Public: Get the extname of the path

Examples

Pathname.new('.rb').extname
# => '.rb'

Pathname.new('file.rb').extname
# => '.rb'

Returns a String.



40
41
42
# File 'lib/language_sniffer/pathname.rb', line 40

def extname
  File.extname(@path)
end

#languageObject

Public: Get the language of the path

The path extension name is the only heuristic used to detect the language name.

Examples

Pathname.new('file.rb').language
# => Language['Ruby']

Returns a Language or nil if none was found.



55
56
57
# File 'lib/language_sniffer/pathname.rb', line 55

def language
  @language ||= Language.find_by_filename(@path)
end

#to_sObject

Public: Return self as String

Returns a String



62
63
64
# File 'lib/language_sniffer/pathname.rb', line 62

def to_s
  @path.dup
end