Class: TwitterCldr::Transforms::TransformId

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/transforms/transform_id.rb

Constant Summary collapse

CHAIN =
[
  :normal_fallback1, :normal_fallback2, :laddered_fallback1,
  :normal_fallback3, :laddered_fallback2
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target, variant = nil) ⇒ TransformId

Returns a new instance of TransformId.



146
147
148
149
150
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 146

def initialize(source, target, variant = nil)
  @source = source
  @target = target
  @variant = variant
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



144
145
146
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 144

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



144
145
146
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 144

def target
  @target
end

#variantObject (readonly)

Returns the value of attribute variant.



144
145
146
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 144

def variant
  @variant
end

Class Method Details

.find(source_locale_or_str, target_locale_or_str) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 18

def find(source_locale_or_str, target_locale_or_str)
  source_locale = parse_locale(source_locale_or_str)
  target_locale = parse_locale(target_locale_or_str)
  source_chain = map_chain_for(source_locale)
  target_chain = map_chain_for(target_locale)
  variants = variants_for(source_locale, target_locale)

  # add original locale strings to chain in case they aren't actually
  # locales (think 'hiragana', etc)
  source_chain << [source_locale_or_str.to_s]
  target_chain << [target_locale_or_str.to_s]

  find_in_chains(
    source_chain, target_chain, variants
  )
end

.join(source, target, variant = nil) ⇒ Object



48
49
50
51
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 48

def join(source, target, variant = nil)
  base = "#{source}-#{target}"
  variant ? "#{base}/#{variant}" : base
end

.join_file_name(parts) ⇒ Object



53
54
55
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 53

def join_file_name(parts)
  parts.compact.join('-')
end

.parse(str) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 35

def parse(str)
  if normalized = normalize(str)
    new(*split(normalized))
  else
    raise InvalidTransformIdError,
      "'#{str}' is not a valid transform id"
  end
end

.split(str) ⇒ Object



44
45
46
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 44

def split(str)
  str.split(/[\-\/]/)
end

.transform_id_mapObject



57
58
59
60
61
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 57

def transform_id_map
  @transform_id_map ||= TwitterCldr.get_resource(
    *%w(shared transforms transform_id_map)
  )
end

Instance Method Details

#file_nameObject



160
161
162
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 160

def file_name
  self.class.transform_id_map[to_s]
end

#has_variant?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 152

def has_variant?
  !!variant
end

#reverseObject



156
157
158
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 156

def reverse
  self.class.new(target, source, variant)
end

#to_sObject



164
165
166
# File 'lib/twitter_cldr/transforms/transform_id.rb', line 164

def to_s
  self.class.join(source, target, variant)
end