Class: Archimate::DataModel::LangString
- Inherits:
-
Object
- Object
- Archimate::DataModel::LangString
show all
- Extended by:
- Forwardable
- Includes:
- Comparison, Comparable
- Defined in:
- lib/archimate/data_model/lang_string.rb
Overview
A base string type for multi-language strings.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Comparison
#[], #dig, #each, #hash, included, #inspect, #pretty_print, #to_h
Constructor Details
#initialize(str = nil, lang_hash: {}, default_lang: nil) ⇒ LangString
Returns a new instance of LangString.
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/archimate/data_model/lang_string.rb', line 35
def initialize(str = nil, lang_hash: {}, default_lang: nil)
@lang_hash = lang_hash
@default_lang = default_lang || lang_hash.keys.first
@default_text = str || lang_hash.fetch(@default_lang, nil)
case str
when String
@lang_hash[@default_lang] = @default_text = str.strip
when LangString
@lang_hash = str.lang_hash
@default_lang = str.default_lang
@default_text = str.default_text
else
@lang_hash[default_lang] = @default_text if @default_text
end
end
|
Instance Attribute Details
#default_lang ⇒ String, NilClass
20
|
# File 'lib/archimate/data_model/lang_string.rb', line 20
model_attr :default_lang
|
#default_text ⇒ String
23
|
# File 'lib/archimate/data_model/lang_string.rb', line 23
model_attr :default_text
|
#lang_hash ⇒ Hash
17
|
# File 'lib/archimate/data_model/lang_string.rb', line 17
model_attr :lang_hash
|
Class Method Details
.string(str, lang = nil) ⇒ Object
25
26
27
28
|
# File 'lib/archimate/data_model/lang_string.rb', line 25
def self.string(str, lang = nil)
return nil if !str || str.strip.empty?
new(str, default_lang: lang)
end
|
Instance Method Details
#<=>(other) ⇒ Object
94
95
96
|
# File 'lib/archimate/data_model/lang_string.rb', line 94
def <=>(other)
to_s <=> other.to_s
end
|
#==(other) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/archimate/data_model/lang_string.rb', line 77
def ==(other)
if other.is_a?(String)
to_s == other
else
super
end
end
|
#=~(other) ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/archimate/data_model/lang_string.rb', line 85
def =~(other)
str = to_s
if other.is_a?(Regexp)
other =~ str
else
Regexp.new(Regexp.escape(str)) =~ other
end
end
|
#by_lang(lang) ⇒ Object
65
66
67
|
# File 'lib/archimate/data_model/lang_string.rb', line 65
def by_lang(lang)
lang_hash.fetch(lang, nil)
end
|
#lang ⇒ Object
73
74
75
|
# File 'lib/archimate/data_model/lang_string.rb', line 73
def lang
default_lang
end
|
#langs ⇒ Object
51
52
53
|
# File 'lib/archimate/data_model/lang_string.rb', line 51
def langs
@lang_hash.keys
end
|
#merge(other) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/archimate/data_model/lang_string.rb', line 98
def merge(other)
return unless other
other.lang_hash.each do |k, v|
if @lang_hash.include?(k)
@lang_hash[k] = [@lang_hash[k], v].join("\n") if @lang_hash[k] != other.lang_hash[k]
else
@lang_hash[k] = v
end
end
@default_lang = @default_lang || other.default_lang || @lang_hash.keys.first
@default_text = @lang_hash[@default_lang]
end
|
#text ⇒ Object
69
70
71
|
# File 'lib/archimate/data_model/lang_string.rb', line 69
def text
to_s
end
|
#to_s ⇒ Object
59
60
61
62
63
|
# File 'lib/archimate/data_model/lang_string.rb', line 59
def to_s
@default_text ||= @lang_hash.fetch(default_lang) do |key|
@lang_hash.fetch(nil, nil) if key
end
end
|
#to_str ⇒ Object
55
56
57
|
# File 'lib/archimate/data_model/lang_string.rb', line 55
def to_str
to_s
end
|