Class: Locale Private
Overview
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
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) ⇒ T.attached_class?
private
Instance Method Summary collapse
-
#detect(locale_groups) ⇒ Object
private
-
#eql?(other) ⇒ Boolean
(also: #==)
private
-
#include?(other) ⇒ Boolean
private
-
#initialize(language, script, region) ⇒ Locale
constructor
private
A new instance of Locale.
-
#to_s ⇒ String
private
Constructor Details
#initialize(language, script, region) ⇒ Locale
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns a new instance of Locale.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'locale.rb', line 62 def initialize(language, script, region) raise ArgumentError, "#{self.class} cannot be empty" if language.nil? && region.nil? && script.nil? { language: language, script: script, region: region, }.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. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
60 61 62 |
# File 'locale.rb', line 60 def language @language end |
#region ⇒ Object (readonly)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
60 61 62 |
# File 'locale.rb', line 60 def region @region end |
#script ⇒ Object (readonly)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
60 61 62 |
# File 'locale.rb', line 60 def script @script end |
Class Method Details
.parse(string) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
29 30 31 32 33 34 35 |
# File 'locale.rb', line 29 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) ⇒ T.attached_class?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'locale.rb', line 38 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 (script = scanner.scan(SCRIPT_REGEX)) sep = scanner.scan(/-/) return if (sep && scanner.eos?) || (sep.nil? && !scanner.eos?) end region = scanner.scan(REGION_REGEX) return unless scanner.eos? new(language, script, region) end |
Instance Method Details
#detect(locale_groups) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
106 107 108 109 |
# File 'locale.rb', line 106 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. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
94 95 96 97 98 99 100 101 102 103 |
# File 'locale.rb', line 94 def eql?(other) unless other.is_a?(self.class) other = self.class.try_parse(other) return false if other.nil? end [:language, :script, :region].all? do |var| public_send(var) == other.public_send(var) end end |
#include?(other) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'locale.rb', line 79 def include?(other) unless other.is_a?(self.class) other = self.class.try_parse(other) return false if other.nil? end [:language, :script, :region].all? do |var| if other.public_send(var).nil? true else public_send(var) == other.public_send(var) end end end |
#to_s ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
112 113 114 |
# File 'locale.rb', line 112 def to_s [@language, @script, @region].compact.join("-") end |