Class: Locale Private

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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, region, script) ⇒ 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)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File '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

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



62
63
64
# File 'locale.rb', line 62

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.



62
63
64
# File 'locale.rb', line 62

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.



62
63
64
# File 'locale.rb', line 62

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:



31
32
33
34
35
36
37
# File '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) ⇒ 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)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File '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. 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.



108
109
110
111
# File '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. 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)


96
97
98
99
100
101
102
103
104
105
# File '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. 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)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File '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_sString

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:



114
115
116
# File 'locale.rb', line 114

def to_s
  [@language, @region, @script].compact.join("-")
end