Class: Lang::Tag::Composition

Inherits:
Object
  • Object
show all
Defined in:
lib/lang/tag/composition.rb

Overview

Handles abstract compositions of subtags incl. basic and extended language-ranges.

Example

class LanguageRange < Lang::Tag::Composition

  def initialize(thing)
    raise TypeError, "Can't convert #{thing.class} into String" unless thing.respond_to?(:to_str)
    sequence = thing.to_str
    unless /^(?:\*|[a-z]{1,8})(?:-[a-z\d]{1,8}|-\*)*$/i === sequence
      raise Error, "#{sequence.inspect} is not a language-range."
    end
    @sequence = sequence
  end

  def simplify! # to basic language-range
    /^\*-/ === @sequence ? @sequence = '*' : @sequence.gsub!('-*','')
    dirty
  end

end

Direct Known Subclasses

Grandfathered, Langtag, Privateuse

Instance Method Summary collapse

Constructor Details

#initialize(thing) ⇒ Composition

Returns a new instance of Composition.

Raises:

  • (TypeError)


29
30
31
32
# File 'lib/lang/tag/composition.rb', line 29

def initialize(thing)
  raise TypeError, "Can't convert #{thing.class} into String" unless thing.respond_to?(:to_str)
  @sequence = thing.to_str
end

Instance Method Details

#==(other) ⇒ Object

Returns true if Compositions are equal.



45
46
47
48
# File 'lib/lang/tag/composition.rb', line 45

def ==(other)
  return false unless other.kind_of?(self.class)
  self.composition == other.composition
end

#===(other) ⇒ Object

Returns true if compositions are equal. Allows comparison against Strings.



37
38
39
40
41
# File 'lib/lang/tag/composition.rb', line 37

def ===(other)
  return false unless other.respond_to?(:to_str)
  s = other.to_str
  composition == s || composition == s.downcase
end

#compositionObject



59
60
61
# File 'lib/lang/tag/composition.rb', line 59

def composition
  @composition ||= to_s.downcase
end

#dupObject

Duplicates self.



89
90
91
# File 'lib/lang/tag/composition.rb', line 89

def dup
  self.class.new(to_s.dup)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/lang/tag/composition.rb', line 50

def eql?(other)
  return false unless other.kind_of?(self.class)
  self.to_s == other.to_s
end

#hashObject



55
56
57
# File 'lib/lang/tag/composition.rb', line 55

def hash
  to_s.hash
end

#inspectObject



133
134
135
# File 'lib/lang/tag/composition.rb', line 133

def inspect
  sprintf("#<%s:%#0x %s>", self.class.to_s, self.object_id, self.to_s)
end

#lengthObject



93
94
95
# File 'lib/lang/tag/composition.rb', line 93

def length
  to_s.length
end

#nicecaseObject



127
128
129
130
131
# File 'lib/lang/tag/composition.rb', line 127

def nicecase
  duplicated = self.dup
  duplicated.nicecase!
  duplicated
end

#nicecase!Object

– RFC 5646, Section 2.1.1 An implementation can reproduce this format without accessing the registry as follows. All subtags, including extension and private use subtags, use lowercase letters with two exceptions: two-letter and four-letter subtags that neither appear at the start of the tag nor occur after singletons. Such two-letter subtags are all uppercase (as in the tags “en-CA-x-ca” or “sgn-BE-FR”) and four- letter subtags are titlecase (as in the tag “az-Latn-x-latn”). ++



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lang/tag/composition.rb', line 114

def nicecase!
  @sequence.downcase!
  @sequence.gsub!(/-(?:([a-z\d]{4})|[a-z\d]{2}|[a-z\d]-.*)(?=-|$)/) do |sequence|
    if $1
      sequence = HYPHEN + $1.capitalize
    elsif sequence.size == 3
      sequence.upcase!
    end
    sequence
  end
  nil
end

#subtags_countObject

Returns the number of subtags in self.



99
100
101
# File 'lib/lang/tag/composition.rb', line 99

def subtags_count
  to_s.count(HYPHEN) + 1
end

#to_aObject



69
70
71
# File 'lib/lang/tag/composition.rb', line 69

def to_a
  to_s.split(HYPHEN_SPLITTER)
end

#to_sObject Also known as: to_str



63
64
65
# File 'lib/lang/tag/composition.rb', line 63

def to_s
  @sequence
end