Module: PolyglotIos::Helper::General

Constant Summary collapse

ESCAPE_KEYWORDS =
["associatedtype", "class", "deinit", "enum", "extension", "fileprivate", "func",
"import", "init", "inout", "internal", "let", "operator", "private", "protocol",
"public", "static", "struct", "subscript", "typealias", "var", "break", "case",
"continue", "default", "defer", "do", "else", "fallthrough", "for", "guard", "if",
"in", "repeat", "return", "switch", "where", "while", "as", "Any", "catch", "false",
"is", "nil", "rethrows", "super", "self", "Self", "throw", "throws", "true", "try", "_"]

Instance Method Summary collapse

Methods included from Terminal

#generate_token_message, #prompt, #success

Instance Method Details

#clean_case_name(name) ⇒ Object



69
70
71
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 69

def clean_case_name(name)
  clean_variable_name(name)
end

#clean_enum_name(name) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 59

def clean_enum_name(name)
  clean_name = name
    .split(/[^0-9a-zA-Z]/)
    .reject { |c| c.empty? }
    .map { |value| value.capitalize }
    .join

  escape_with_underscore_if_needed(clean_name)
end

#clean_variable_name(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 73

def clean_variable_name(name)
  clean_name = name
    .split(/[^0-9a-zA-Z]/)
    .reject { |c| c.empty? }
    .each_with_index
    .map { |value, index| index == 0 ? value.downcase : value.capitalize }
    .join

  escaped_underscore = escape_with_underscore_if_needed(clean_name)
  escape_keyword_if_needed(escaped_underscore)
end

#configObject



21
22
23
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 21

def config
  @config ||= PolyglotIos::IO::Config.read.with_indifferent_access
end

#escape_keyword_if_needed(name) ⇒ Object



90
91
92
93
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 90

def escape_keyword_if_needed(name)
  return name if !ESCAPE_KEYWORDS.include? name
  "`#{name}`"
end

#escape_with_underscore_if_needed(name) ⇒ Object



85
86
87
88
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 85

def escape_with_underscore_if_needed(name)
  return name if name.match(/^[A-Za-z_]/)
  "_" + name
end

#indent(level = 0, initial = "") ⇒ Object



53
54
55
56
57
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 53

def indent(level = 0, initial = "")
  (1..level)
    .to_a.reduce("") { |result, value| result + "    " }
    .concat(initial)
end

#programming_languageObject



29
30
31
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 29

def programming_language
  @programming_language ||= config[:language]
end

#project_configsObject



25
26
27
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 25

def project_configs
  @project_configs ||= config[:projects]
end

#skip_writing_language_structObject



33
34
35
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 33

def skip_writing_language_struct
  @skip_writing_language_struct ||= config[:skip_writing_language_struct]
end

#tokenObject



11
12
13
14
15
16
17
18
19
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 11

def token
  @token ||= PolyglotIos::IO::Token.read(token_env_variable)
  if @token.to_s.empty?
    prompt.error("No Auth Token provided!\n")
    prompt.warn(generate_token_message(token_env_variable))
    exit
  end
  @token
end

#token_env_variableObject



45
46
47
48
49
50
51
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 45

def token_env_variable
  begin
    @token_env_variable ||= config[:token_env_variable] || PolyglotIos::DEFAULT_TOKEN_ENVIRONMENT_VARIABLE
  rescue
    @token_env_variable = PolyglotIos::DEFAULT_TOKEN_ENVIRONMENT_VARIABLE
  end
end

#urlObject



41
42
43
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 41

def url
  @url ||= config[:url] || PolyglotIos::DEFAULT_API_URL
end

#use_old_namingObject



37
38
39
# File 'lib/ios_polyglot_cli/helpers/general.rb', line 37

def use_old_naming
  @useOldNaming ||= config[:useOldNaming]
end