Class: BBMB::Util::Multilingual

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bbmb/util/multilingual.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canonical = {}) ⇒ Multilingual

Returns a new instance of Multilingual.



10
11
12
13
# File 'lib/bbmb/util/multilingual.rb', line 10

def initialize(canonical={})
  @canonical = canonical
  @synonyms = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bbmb/util/multilingual.rb', line 23

def method_missing(meth, *args, &block)
  case meth.to_s
  when /^[a-z]{2}$/
    @canonical[meth]
  when /^([a-z]{2})=$/
    key = $~[1].to_sym
    if(value = args.first)
      @canonical.store(key, value)
    else
      @canonical.delete(key)
    end
  else
    super(meth, *args, &block)
  end
end

Instance Attribute Details

#canonicalObject (readonly)

Returns the value of attribute canonical.



8
9
10
# File 'lib/bbmb/util/multilingual.rb', line 8

def canonical
  @canonical
end

#synonymsObject (readonly)

Returns the value of attribute synonyms.



9
10
11
# File 'lib/bbmb/util/multilingual.rb', line 9

def synonyms
  @synonyms
end

Instance Method Details

#<=>(other) ⇒ Object



52
53
54
# File 'lib/bbmb/util/multilingual.rb', line 52

def <=>(other)
  all.sort <=> other.all.sort
end

#==(other) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bbmb/util/multilingual.rb', line 41

def ==(other)
  case other
  when String
    @canonical.values.any? { |val| val == other } \
      || @synonyms.any? { |val| val == other }
  when Multilingual
    @canonical == other.canonical && @synonyms == other.synonyms
  else
    false
  end
end

#allObject



14
15
16
# File 'lib/bbmb/util/multilingual.rb', line 14

def all
  @canonical.values.concat(@synonyms)
end

#defaultObject



17
18
19
# File 'lib/bbmb/util/multilingual.rb', line 17

def default
  @canonical.values.sort.first
end

#empty?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/bbmb/util/multilingual.rb', line 20

def empty?
  @canonical.empty?
end

#to_sObject



38
39
40
# File 'lib/bbmb/util/multilingual.rb', line 38

def to_s
  default.to_s
end