Class: Lokale::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/lokale/config.rb,
lib/lokale/lokalefile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



23
24
25
# File 'lib/lokale/config.rb', line 23

def actions
  @actions
end

#base_langObject (readonly)

Returns the value of attribute base_lang.



67
68
69
# File 'lib/lokale/lokalefile.rb', line 67

def base_lang
  @base_lang
end

#macrosObject (readonly)

Returns the value of attribute macros.



66
67
68
# File 'lib/lokale/lokalefile.rb', line 66

def macros
  @macros
end

#main_langObject (readonly)

Returns the value of attribute main_lang.



67
68
69
# File 'lib/lokale/lokalefile.rb', line 67

def main_lang
  @main_lang
end

#project_nameObject

Returns the value of attribute project_name.



24
25
26
# File 'lib/lokale/config.rb', line 24

def project_name
  @project_name
end

#project_pathObject

Returns the value of attribute project_path.



24
25
26
# File 'lib/lokale/config.rb', line 24

def project_path
  @project_path
end

Class Method Details

.getObject



74
75
76
77
# File 'lib/lokale/config.rb', line 74

def self.get
  init if @config.nil?
  @config
end

.initObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lokale/config.rb', line 26

def self.init 
  return unless @config.nil?

  actions = []

  OptionParser.new do |opts|
    opts.banner = "Usage: lokale [-bsh]"

    opts.on("-b", "--copy-base", "Copies 'en' localization files to 'Base'") do |n|
      actions << Action.copy_base
    end

    opts.on("-s", "--summary", "Prints project summary") do |n|
      actions << Action.summary
    end

    opts.on("-a", "--append", "Appends new strings to english localization file") do |n|
      actions << Action.append
    end

    opts.on("-e", "--export", "Creates xliff files with missing localization") do |n|
      actions << Action.export
    end

    opts.on("-i", "--import", "Looks for xliffs in project dir and imports whatever possible") do |n|
      actions << Action.import
    end

    opts.on("-f", "--create-config-file", "Create default `.lokale` config file") do |n|
      actions << Action.create_config
    end

    opts.on("-p", "--add-to-project", "Creates project strings file") do |n|
      actions << Action.add_to_proj
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end.parse!

  actions << Action.summary if actions.empty? 

  @config = Config.new
  @config.actions = actions
end

Instance Method Details

#create_default_fileObject



50
51
52
53
54
55
56
57
58
# File 'lib/lokale/lokalefile.rb', line 50

def create_default_file
  if File.file? lokalefile_path 
    puts "Config file `#{lokalefile_path.blue}` already exists."
  else
    File.write(lokalefile_path, DEFAULT_LOKALEFILE)
    puts "Created config file at `#{lokalefile_path.blue}`"
  end
  
end

#fill_defaultsObject



75
76
77
78
79
80
81
82
# File 'lib/lokale/lokalefile.rb', line 75

def fill_defaults
  default = Config.new
  default.read_default_config

  @macros ||= default.macros
  @main_lang ||= default.main_lang
  @base_lang ||= default.base_lang
end

#lokalefile_pathObject



60
61
62
# File 'lib/lokale/lokalefile.rb', line 60

def lokalefile_path
  File.join(@project_path, ".lokale") 
end

#read_config_from_file(file_path) ⇒ Object



43
44
45
46
47
48
# File 'lib/lokale/lokalefile.rb', line 43

def read_config_from_file(file_path)
  content = File.read(file_path)
  reset_config
  instance_eval(content)
  fill_defaults
end

#read_default_configObject



38
39
40
41
# File 'lib/lokale/lokalefile.rb', line 38

def read_default_config
  reset_config
  instance_eval(DEFAULT_LOKALEFILE)
end

#read_lokalefileObject



30
31
32
33
34
35
36
# File 'lib/lokale/lokalefile.rb', line 30

def read_lokalefile
  if File.file? lokalefile_path 
    read_config_from_file(lokalefile_path)
  else
    read_default_config
  end
end

#reset_configObject



69
70
71
72
73
# File 'lib/lokale/lokalefile.rb', line 69

def reset_config 
  @macros = nil
  @main_lang = nil
  @base_lang = nil  
end