Module: Drakkon::Hub

Defined in:
lib/drakkon/lib/hub.rb

Overview

General Info Helpers

Class Method Summary collapse

Class Method Details

.configObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/drakkon/lib/hub.rb', line 69

def self.config
  # Write Default
  File.write(config_file, JSON.pretty_generate({})) unless File.exist?(config_file)

  @config ||= JSON.parse(File.read(config_file), { symbolize_names: true })

  # TODO: Optimize this a bit more
  config_defaults.each do |key, value|
    # ||= Is problematic with falses
    @config[key] = value unless @config.key?(key)
  end

  @config
end

.config_defaultsObject



24
25
26
27
28
29
# File 'lib/drakkon/lib/hub.rb', line 24

def self.config_defaults
  {
    versions: [],
    skeletons: {}
  }
end

.config_fileObject



36
37
38
# File 'lib/drakkon/lib/hub.rb', line 36

def self.config_file
  "#{dir}/config.json"
end

.delete(key) ⇒ Object



89
90
91
92
# File 'lib/drakkon/lib/hub.rb', line 89

def self.delete(key)
  config.delete key
  write
end

.dirObject

Combine Dir Location



32
33
34
# File 'lib/drakkon/lib/hub.rb', line 32

def self.dir
  "#{Dir.home}/.drakkon"
end

.initObject

Ensrue Setup



5
6
7
8
9
10
11
12
# File 'lib/drakkon/lib/hub.rb', line 5

def self.init
  # rubocop:disable Lint/NonAtomicFileOperation
  # Ensure Home Directory; It is neccessary stupid rubocop
  Dir.mkdir(Hub.dir) unless Dir.exist?(Hub.dir)
  # rubocop:enable Lint/NonAtomicFileOperation

  setup unless File.exist?(config_file)
end

.log_fileObject



99
100
101
# File 'lib/drakkon/lib/hub.rb', line 99

def self.log_file
  "#{dir}/output.log"
end

.setupObject



14
15
16
17
18
19
20
21
22
# File 'lib/drakkon/lib/hub.rb', line 14

def self.setup
  LogBot.info('Hub', 'Init Config')
  opts = config_defaults

  # TODO: Be better, this is lazy...
  opts.each do |key, value|
    Hub.update(key, value)
  end
end

.skeletonsObject



110
111
112
# File 'lib/drakkon/lib/hub.rb', line 110

def self.skeletons
  config.skeletons
end

.skeletons?Boolean

Template Helpers

Returns:

  • (Boolean)


106
107
108
# File 'lib/drakkon/lib/hub.rb', line 106

def self.skeletons?
  !config.skeletons.empty?
end

.update(key, value) ⇒ Object



84
85
86
87
# File 'lib/drakkon/lib/hub.rb', line 84

def self.update(key, value)
  config[key] = value
  write
end

.version?(version) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/drakkon/lib/hub.rb', line 40

def self.version?(version)
  versions.include? version
end

.version_add(version) ⇒ Object



63
64
65
66
67
# File 'lib/drakkon/lib/hub.rb', line 63

def self.version_add(version)
  config[:versions].push version

  update(:versions, config[:versions])
end

.version_installed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/drakkon/lib/hub.rb', line 48

def self.version_installed?
  versions.any?
end

.version_latestObject



52
53
54
55
56
# File 'lib/drakkon/lib/hub.rb', line 52

def self.version_latest
  list = versions.map { |v| v.split('.').map(&:to_i) }
  latest = list.sort.reverse.max
  latest.join('.')
end

.versionsObject



44
45
46
# File 'lib/drakkon/lib/hub.rb', line 44

def self.versions
  config.versions
end

.versions_sortedObject



58
59
60
61
# File 'lib/drakkon/lib/hub.rb', line 58

def self.versions_sorted
  list = versions.map { |v| v.split('.').map(&:to_i) }
  list.sort.reverse.map { |v| v.join('.') }
end

.writeObject



94
95
96
97
# File 'lib/drakkon/lib/hub.rb', line 94

def self.write
  # LogBot.debug('Hub', "Writing Config: #{config_file}")
  File.write(config_file, JSON.pretty_generate(config))
end