Class: SlimKeyfy::Transformer::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/slimkeyfy/transformer/word.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, key_base, extension) ⇒ Word

Returns a new instance of Word.



5
6
7
8
9
10
11
# File 'lib/slimkeyfy/transformer/word.rb', line 5

def initialize(line, key_base, extension)
  @line = line
  @key_base = key_base
  @extension = extension
  @indentation = " " * (@line.size - unindented_line.size)
  @translations = {}
end

Instance Attribute Details

#indentationObject (readonly)

Returns the value of attribute indentation.



2
3
4
# File 'lib/slimkeyfy/transformer/word.rb', line 2

def indentation
  @indentation
end

#lineObject (readonly)

Returns the value of attribute line.



2
3
4
# File 'lib/slimkeyfy/transformer/word.rb', line 2

def line
  @line
end

#tokensObject (readonly)

Returns the value of attribute tokens.



2
3
4
# File 'lib/slimkeyfy/transformer/word.rb', line 2

def tokens
  @tokens
end

#translationsObject

Returns the value of attribute translations.



3
4
5
# File 'lib/slimkeyfy/transformer/word.rb', line 3

def translations
  @translations
end

Instance Method Details

#as_list(delim = " ") ⇒ Object



13
14
15
# File 'lib/slimkeyfy/transformer/word.rb', line 13

def as_list(delim=" ")
  @line.split(delim)
end

#extract_arguments(translation) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/slimkeyfy/transformer/word.rb', line 50

def extract_arguments(translation)
  args = {}
  translation.scan(/\#{[^}]*}/).each_with_index do |arg, index|
    stripped_arg = arg[2..-2]
    key = arg[/\w+/]
    key = key + index.to_s if index > 0
    translation = translation.gsub(arg, "%{#{key}}")
    args[key]= stripped_arg
  end
  [args, translation]
end

#extract_updated_key(translation_key_with_base) ⇒ Object



62
63
64
65
# File 'lib/slimkeyfy/transformer/word.rb', line 62

def extract_updated_key(translation_key_with_base)
  return "" if (translation_key_with_base.nil? or translation_key_with_base.empty?)
  translation_key_with_base.split(".").last
end

#headObject



21
22
23
# File 'lib/slimkeyfy/transformer/word.rb', line 21

def head
  as_list.first
end

#i18n_string(translation_key, args = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slimkeyfy/transformer/word.rb', line 29

def i18n_string(translation_key, args={})
  args_string = args.inject('') do |string, (k,v)|
    string += ", #{k}: (#{v})"
  end
  
  if @extension == "rb"
    "t('#{@key_base}.#{translation_key}'#{args_string})"
  else
    "t('.#{translation_key}'#{args_string})"
  end
end

#tailObject



25
26
27
# File 'lib/slimkeyfy/transformer/word.rb', line 25

def tail
  as_list.drop(1)
end

#unindented_lineObject



17
18
19
# File 'lib/slimkeyfy/transformer/word.rb', line 17

def unindented_line
  @line.sub(/^\s*/, "")
end

#update_translation_key_hash(yaml_processor, translation) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/slimkeyfy/transformer/word.rb', line 41

def update_translation_key_hash(yaml_processor, translation)
  arguments, translation = extract_arguments(translation)
  translation_key = SlimKeyfy::Slimutils::TranslationKeyGenerator.new(translation).generate_key_name
  translation_key_with_base = "#{@key_base}.#{translation_key}"
  translation_key_with_base, translation = yaml_processor.merge!(translation_key_with_base, translation) unless yaml_processor.nil?
  @translations.merge!({translation_key_with_base => translation})
  i18n_string(extract_updated_key(translation_key_with_base), arguments)
end