Class: Config

Inherits:
Object
  • Object
show all
Defined in:
lib/share/config.rb

Overview

chichilku3 config base used by client and server

Direct Known Subclasses

ClientCfg, ServerCfg

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(console, file) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/share/config.rb', line 11

def initialize(console, file)
  @chichilku3_dir = ''
  if OS.linux?
    @chichilku3_dir = "#{ENV['HOME']}/.chichilku/chichilku3/"
  elsif OS.mac?
    @chichilku3_dir = "#{ENV['HOME']}/Library/Application Support/chichilku/chichilku3/"
  # elsif OS.windows?
  #   @chichilku3_dir = "%APPDATA%\\chichilku\\chichilku3\\"
  else
    puts 'os not supported.'
    exit
  end
  puts "path: #{@chichilku3_dir}"
  FileUtils.mkdir_p @chichilku3_dir
  FileUtils.mkdir_p "#{@chichilku3_dir}recordings"
  FileUtils.mkdir_p "#{@chichilku3_dir}maps_b64"
  FileUtils.mkdir_p "#{@chichilku3_dir}downloadedmaps"
  FileUtils.mkdir_p "#{@chichilku3_dir}tmp"
  unless File.directory? "#{@chichilku3_dir}maps"
    if File.directory? 'maps'
      FileUtils.cp_r 'maps', "#{@chichilku3_dir}maps"
    else
      FileUtils.mkdir_p "#{@chichilku3_dir}maps"
    end
  end
  @source_file = File.join(File.dirname(__FILE__), '../../', file)
  create_default_cfg(@source_file, "#{@chichilku3_dir}/#{file}")
  @file = @chichilku3_dir + file
  @console = console
  @data = load
end

Instance Attribute Details

#chichilku3_dirObject (readonly)

Returns the value of attribute chichilku3_dir.



9
10
11
# File 'lib/share/config.rb', line 9

def chichilku3_dir
  @chichilku3_dir
end

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/share/config.rb', line 9

def data
  @data
end

Instance Method Details

#create_default_cfg(from, to) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/share/config.rb', line 43

def create_default_cfg(from, to)
  return if File.file?(to)

  tmp = JSON.parse(File.read(from))
  File.open(to, 'w') do |f|
    f.write(tmp.to_json)
  end
end

#loadObject



56
57
58
59
60
61
# File 'lib/share/config.rb', line 56

def load
  defaults = JSON.parse(File.read(@source_file))
  data = JSON.parse(File.read(@file))
  data = defaults.merge(data)
  sanitize_data(data)
end

#sanitize_data(data) ⇒ Object



52
53
54
# File 'lib/share/config.rb', line 52

def sanitize_data(data)
  data
end

#saveObject



63
64
65
66
67
# File 'lib/share/config.rb', line 63

def save
  File.open(@file, 'w') do |f|
    f.write(JSON.pretty_generate(data))
  end
end