Class: Locale Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Representation of a system locale.
Used to compare the system language and languages defined using the cask language
stanza.
Defined Under Namespace
Classes: ParserError
Instance Attribute Summary collapse
- #language ⇒ Object readonly private
- #region ⇒ Object readonly private
- #script ⇒ Object readonly private
Class Method Summary collapse
- .parse(string) ⇒ Object private
- .try_parse(string) ⇒ Object private
Instance Method Summary collapse
- #detect(locale_groups) ⇒ Object private
- #eql?(other) ⇒ Boolean (also: #==) private
- #include?(other) ⇒ Boolean private
-
#initialize(language, region, script) ⇒ Locale
constructor
private
A new instance of Locale.
- #to_s ⇒ Object private
Constructor Details
#initialize(language, region, script) ⇒ Locale
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Locale.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'Library/Homebrew/locale.rb', line 64 def initialize(language, region, script) raise ArgumentError, "#{self.class} cannot be empty" if language.nil? && region.nil? && script.nil? { language: language, region: region, script: script, }.each do |key, value| next if value.nil? regex = self.class.const_get("#{key.upcase}_REGEX") raise ParserError, "'#{value}' does not match #{regex}" unless value&.match?(regex) instance_variable_set(:"@#{key}", value) end end |
Instance Attribute Details
#language ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
62 63 64 |
# File 'Library/Homebrew/locale.rb', line 62 def language @language end |
#region ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
62 63 64 |
# File 'Library/Homebrew/locale.rb', line 62 def region @region end |
#script ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
62 63 64 |
# File 'Library/Homebrew/locale.rb', line 62 def script @script end |
Class Method Details
.parse(string) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
31 32 33 34 35 36 37 |
# File 'Library/Homebrew/locale.rb', line 31 def self.parse(string) if (locale = try_parse(string)) return locale end raise ParserError, "'#{string}' cannot be parsed to a #{self}" end |
.try_parse(string) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'Library/Homebrew/locale.rb', line 40 def self.try_parse(string) return if string.blank? scanner = StringScanner.new(string) if (language = scanner.scan(LANGUAGE_REGEX)) sep = scanner.scan(/-/) return if (sep && scanner.eos?) || (sep.nil? && !scanner.eos?) end if (region = scanner.scan(REGION_REGEX)) sep = scanner.scan(/-/) return if (sep && scanner.eos?) || (sep.nil? && !scanner.eos?) end script = scanner.scan(SCRIPT_REGEX) return unless scanner.eos? new(language, region, script) end |
Instance Method Details
#detect(locale_groups) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
108 109 110 111 |
# File 'Library/Homebrew/locale.rb', line 108 def detect(locale_groups) locale_groups.find { |locales| locales.any? { |locale| eql?(locale) } } || locale_groups.find { |locales| locales.any? { |locale| include?(locale) } } end |
#eql?(other) ⇒ Boolean Also known as: ==
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 99 100 101 102 103 104 105 |
# File 'Library/Homebrew/locale.rb', line 96 def eql?(other) unless other.is_a?(self.class) other = self.class.try_parse(other) return false if other.nil? end [:language, :region, :script].all? do |var| public_send(var) == other.public_send(var) end end |
#include?(other) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'Library/Homebrew/locale.rb', line 81 def include?(other) unless other.is_a?(self.class) other = self.class.try_parse(other) return false if other.nil? end [:language, :region, :script].all? do |var| if other.public_send(var).nil? true else public_send(var) == other.public_send(var) end end end |
#to_s ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
114 115 116 |
# File 'Library/Homebrew/locale.rb', line 114 def to_s [@language, @region, @script].compact.join("-") end |