Class: Locale Private

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/locale.rb

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

Raises:

  • (ArgumentError)

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

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. You should avoid using this method if possible, as it may be removed or be changed in the future.


58
59
60
# File 'Library/Homebrew/locale.rb', line 58

def language
  @language
end

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


58
59
60
# File 'Library/Homebrew/locale.rb', line 58

def region
  @region
end

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


58
59
60
# File 'Library/Homebrew/locale.rb', line 58

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.

Raises:


28
29
30
31
32
33
34
# File 'Library/Homebrew/locale.rb', line 28

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.


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'Library/Homebrew/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 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.


104
105
106
107
# File 'Library/Homebrew/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

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

Returns:

  • (Boolean)

92
93
94
95
96
97
98
99
100
101
# File 'Library/Homebrew/locale.rb', line 92

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.

Returns:

  • (Boolean)

77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'Library/Homebrew/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, :region, :script].all? do |var|
    if other.public_send(var).nil?
      true
    else
      public_send(var) == other.public_send(var)
    end
  end
end

#to_sObject

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.


109
110
111
# File 'Library/Homebrew/locale.rb', line 109

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