Module: Cog::Config::LanguageConfig

Included in:
Cog::Config
Defined in:
lib/cog/config/language_config.rb

Overview

Cog::Config methods related to languages

Instance Method Summary collapse

Instance Method Details

#activate_language(key, opt = {}) { ... } ⇒ Object

Activate a given language within the scope of the provided block. Either provide key, :ext, or :filename but not more than one. If the extension does not match any of the supported languages, the #active_language will not change, but the block will still be called.

Parameters:

  • key (:String)

    the lanuage identifier. Type cog language list to see the possible values

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :ext (:String) — default: nil

    a file extension which will map to a language identifier. Type cog language map to see mapped extensions

  • :filename (:String) — default: nil

    a filename or path to file which will map to a language identifier

Yields:

Returns:

  • (Object)

    the value returned by the block



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cog/config/language_config.rb', line 19

def activate_language(key, opt={}, &block)
  opt, key = key, nil if key.is_a? Hash
  key = if opt[:ext]
    ext = opt[:ext].to_s.downcase
    ext = ext.slice(1..-1) if ext.start_with?('.')
    @language_extension_map[ext] unless ext.empty?
  elsif opt[:filename]
    ext = File.extname(opt[:filename]).slice(1..-1)
    @language_extension_map[ext] unless ext.nil? || ext.empty?
  else
    key
  end
  if key
    @active_languages << @language[key]
    if block
      r = block.call
      @active_languages.pop
      r
    end
  else
    block.call
  end
end

#active_languageLanguage

Returns language which is active in the current context.

Returns:

  • (Language)

    language which is active in the current context



8
9
10
# File 'lib/cog/config/language_config.rb', line 8

def active_language
  @active_languages.last
end

#language(key) ⇒ Language

Returns the language for the given key.

Parameters:

  • key (String)

    the language key

Returns:

  • (Language)

    the language for the given key



50
51
52
# File 'lib/cog/config/language_config.rb', line 50

def language(key)
  @language[key]
end

#language_extensionsArray<String>

Returns list of file extensions for supported languages.

Returns:

  • (Array<String>)

    list of file extensions for supported languages



44
45
46
# File 'lib/cog/config/language_config.rb', line 44

def language_extensions
  @language_extension_map.keys
end

#language_for(path) ⇒ Language?

Returns the language for the given extension.

Parameters:

  • path (String)

    the file system extension, or full path to a file

Returns:

  • (Language, nil)

    the language for the given extension



56
57
58
59
60
61
62
63
# File 'lib/cog/config/language_config.rb', line 56

def language_for(path)
  ext = File.extname(path.to_s)
  ext = path.to_s if ext.empty?
  ext = ext.downcase
  ext = ext.slice(1..-1) if ext.start_with? '.'
  key = @language_extension_map[ext]
  @language[key] if key
end

#language_summaryArray<Language>

Returns current configuration of supported languages.

Returns:

  • (Array<Language>)

    current configuration of supported languages



66
67
68
# File 'lib/cog/config/language_config.rb', line 66

def language_summary
  @language.values.sort
end