class PolyglotCli::Command::Setup

Attributes

subdomain[R]

override subdomain for the setup, use the cached value (from prompt) instead of reading it from the config file (which does not exist yet during the setup)

Public Class Methods

init(options) click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 10
def self.init(options)
  new(options).call
end
new(options = Commander::Command::Options.new) click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 14
def initialize(options = Commander::Command::Options.new)
  @options = options
end

Public Instance Methods

call() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 18
def call
  @config = {
    subdomain: subdomain_prompt,
    project_id: project_id_prompt,
    locale_path: locale_path_prompt
  }
  PolyglotCli::IO::Config.write(@config.merge(locale_mapping: locale_mapping))
  success
rescue TTY::Prompt::ConfigurationError
  prompt.error('Could not find any projects. Please try a new search.')
end

Private Instance Methods

filtered_projects() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 88
def filtered_projects
  @filtered_projects ||= projects.select { |key, _id| key[/^(.*?(#{option_query})[^$]*)$/i] }.sort_by { |p| p[0].downcase }.to_h
end
locale_map() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 51
def locale_map
  languages.each_with_object(Hash.new([])) do |l, hash|
    hash[l.locale[0..1]] += [l.locale]
  end
end
locale_mapping() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 38
def locale_mapping
  return if mapping_prompt

  locale_map.each_with_object({}) do |map, hash|
    key = if map[1].size > 1
            locale_prompt(map[0], map[1])
          else
            map[1].first
          end
    hash[key] = map[0]
  end
end
locale_path_prompt() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 77
def locale_path_prompt
  prompt.ask('Where would you like to store your locale files?', default: 'config/locales')
end
locale_prompt(language, locales) click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 61
def locale_prompt(language, locales)
  prompt.select("Choose a locale for #{language}: ", locales)
end
mapping_prompt() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 57
def mapping_prompt
  prompt.yes?('Would you like to include locales?')
end
option_query() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 92
def option_query
  @option_query ||= @options.__hash__.fetch(:query, '')
end
project_id_prompt() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 69
def project_id_prompt
  if filtered_projects.any?
    prompt.select('Choose a project: ', filtered_projects)
  else
    raise StandardError, "No projects for the subdomain #{subdomain}!"
  end
end
projects() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 81
def projects
  prompt.say('Getting projects...')
  PolyglotCli::Resource::Project.with_subdomain(subdomain).token(token).depaginate.each_with_object({}) do |r, hash|
    hash[r.name] = r.id
  end
end
subdomain_prompt() click to toggle source
# File lib/polyglot_cli/commands/setup.rb, line 65
def subdomain_prompt
  @subdomain = prompt.ask('Enter your subdomain: ', default: 'infinum')
end