Class: Locale Private

Inherits:
Object show all
Defined in:
locale.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'locale.rb', line 60

def initialize(language, script, region)
  raise ArgumentError, "#{self.class} cannot be empty" if language.nil? && region.nil? && script.nil?

  {
    language:,
    script:,
    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

#languageObject (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.



58
59
60
# File 'locale.rb', line 58

def language
  @language
end

#regionObject (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.



58
59
60
# File 'locale.rb', line 58

def region
  @region
end

#scriptObject (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.



58
59
60
# File 'locale.rb', line 58

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.

Raises:



27
28
29
30
31
32
33
# File 'locale.rb', line 27

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.

Parameters:

Returns:

  • (T.attached_class, nil)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'locale.rb', line 36

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.



104
105
106
107
# File 'locale.rb', line 104

def detect(locale_groups)
  locale_groups.find { |locales| locales.any? { |locale| eql?(locale) } } ||
    locale_groups.find { |locales| locales.any? { |locale| include?(locale) } }
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.

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'locale.rb', line 77

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