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
-
#activate_language(key, opt = {}) { ... } ⇒ Object
Activate a given language within the scope of the provided block.
-
#active_language ⇒ Language
Language which is active in the current context.
-
#language(key) ⇒ Language
The language for the given key.
-
#language_extensions ⇒ Array<String>
List of file extensions for supported languages.
-
#language_for(path) ⇒ Language?
The language for the given extension.
-
#language_summary ⇒ Array<Language>
Current configuration of supported languages.
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.
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_language ⇒ Language
Returns 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.
50 51 52 |
# File 'lib/cog/config/language_config.rb', line 50 def language(key) @language[key] end |
#language_extensions ⇒ Array<String>
Returns 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.
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 |