Class: Locraft::SwiftConstantsGenerator

Inherits:
ConstantsGenerator show all
Defined in:
lib/locraft/generators/swift_constants_generator.rb

Instance Method Summary collapse

Methods inherited from ConstantsGenerator

#constants_keys_with, #files_header, #files_name, #write_file_content

Methods inherited from Generator

#initialize

Constructor Details

This class inherits a constructor from Locraft::Generator

Instance Method Details

#const_templateObject



28
29
30
31
32
33
34
# File 'lib/locraft/generators/swift_constants_generator.rb', line 28

def const_template
  <<EOF
static var %<const>s: String {
    return localized("%<key>s")
}
EOF
end

#constant_from(key) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/locraft/generators/swift_constants_generator.rb', line 36

def constant_from(key)
  parts = key.split(/\W/).reject(&:empty?)
  parts.map! do |p|
    p[0] = p[0].capitalize if p != parts[0]
    p
  end
  parts.join
end

#file_content(localizations) ⇒ Object



23
24
25
26
# File 'lib/locraft/generators/swift_constants_generator.rb', line 23

def file_content(localizations)
  content = constants_keys_with(localizations).map { |d| const_template % d }.join("\n")
  Utils.resource_file('swift_macro_template.txt') % content
end

#generate(localizations_hash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/locraft/generators/swift_constants_generator.rb', line 9

def generate(localizations_hash)
  if @config.relative_plist_destination.nil?
    warn 'Skipping objc constants generation. No destination path provided.'
    return
  end

  localizations = localizations_hash[@config.default_lang]
  destination_dir = @config.relative_macro_destination
  file_content = file_content(localizations)
  mkdir_p destination_dir unless Dir.exist?(destination_dir)
  write_file_content("#{destination_dir}/#{files_name}.swift", file_content)
  puts 'constants files was created for [swift] language'
end

#valid?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/locraft/generators/swift_constants_generator.rb', line 5

def valid?
  !@config.relative_macro_destination.nil?
end