Class: Localeapp::CLI::Install::DefaultInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/localeapp/cli/install.rb

Direct Known Subclasses

HerokuInstaller, StandaloneInstaller

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ DefaultInstaller

Returns a new instance of DefaultInstaller.



22
23
24
# File 'lib/localeapp/cli/install.rb', line 22

def initialize(output)
  @output = output
end

Instance Attribute Details

#config_file_pathObject

Returns the value of attribute config_file_path.



20
21
22
# File 'lib/localeapp/cli/install.rb', line 20

def config_file_path
  @config_file_path
end

#data_directoryObject

Returns the value of attribute data_directory.



20
21
22
# File 'lib/localeapp/cli/install.rb', line 20

def data_directory
  @data_directory
end

#keyObject

Returns the value of attribute key.



20
21
22
# File 'lib/localeapp/cli/install.rb', line 20

def key
  @key
end

#project_dataObject

Returns the value of attribute project_data.



20
21
22
# File 'lib/localeapp/cli/install.rb', line 20

def project_data
  @project_data
end

Instance Method Details

#check_data_directory_existsObject



94
95
96
97
98
# File 'lib/localeapp/cli/install.rb', line 94

def check_data_directory_exists
  unless File.directory?(data_directory)
    @output.puts "WARNING: please create the #{data_directory} directory. Your translation data will be stored there."
  end
end

#check_default_localeObject



64
65
66
67
68
69
70
# File 'lib/localeapp/cli/install.rb', line 64

def check_default_locale
  localeapp_default_code = project_data['default_locale']['code']
  @output.puts "Default Locale: #{localeapp_default_code} (#{project_data['default_locale']['name']})"
  if I18n.default_locale.to_s != localeapp_default_code
    @output.puts "WARNING: I18n.default_locale is #{I18n.default_locale}, change in config/environment.rb (Rails 2) or config/application.rb (Rails 3)"
  end
end

#check_key(key) ⇒ Object



100
101
102
# File 'lib/localeapp/cli/install.rb', line 100

def check_key(key)
  Localeapp::KeyChecker.new.check(key)
end

#execute(key = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/localeapp/cli/install.rb', line 26

def execute(key = nil)
  self.key = key
  print_header
  if validate_key
    check_default_locale
    set_config_paths
    @output.puts "Writing configuration file to #{config_file_path}"
    write_config_file
    check_data_directory_exists
    true
  else
    false
  end
end


41
42
43
44
# File 'lib/localeapp/cli/install.rb', line 41

def print_header
  @output.puts "Localeapp Install"
  @output.puts ""
end

#set_config_pathsObject



72
73
74
75
# File 'lib/localeapp/cli/install.rb', line 72

def set_config_paths
  @config_file_path = "config/initializers/localeapp.rb"
  @data_directory   = "config/locales"
end

#validate_keyObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/localeapp/cli/install.rb', line 46

def validate_key
  @output.puts "Checking API key: #{key}"
  if key.nil?
    @output.puts "ERROR: You must supply an API key"
    return false
  end

  valid_key, @project_data = check_key(key)
  if valid_key
    @output.puts "Success!"
    @output.puts "Project: #{project_data['name']}"
    true
  else
    @output.puts "ERROR: Project not found"
    false
  end
end

#write_config_fileObject



77
78
79
80
# File 'lib/localeapp/cli/install.rb', line 77

def write_config_file
  create_config_dir
  write_rails_config
end

#write_rails_configObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/localeapp/cli/install.rb', line 82

def write_rails_config
  File.open(config_file_path, 'w+') do |file|
    file.write <<-CONTENT
require 'localeapp/rails'

Localeapp.configure do |config|
  config.api_key = '#{key}'
end
CONTENT
  end
end