Class: Cinch::BotTemplate::Classes::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/bot_template/classes/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(options:, shell:, all: false) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
14
15
16
# File 'lib/cinch/bot_template/classes/config.rb', line 9

def initialize(options:, shell:, all: false)
  @hl        = HighLine.new($stdin, $stderr, 80)
  @opts      = Hash.new { |hash, key| hash[key] = {} }
  @options   = options
  @all       = all
  @shell     = shell

end

Instance Method Details

#generate(directory = Pathname('.')) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cinch/bot_template/classes/config.rb', line 60

def generate(directory = Pathname('.'))
  meths = self.methods.select { |x| x =~ /^get_[0-9]+_.*/ }
  meths.sort! { |m, n| m.to_s.gsub(/^get_([0-9]+)_.*/, '\1').to_i <=> n.to_s.gsub(/^get_([0-9]+)_.*/, '\1').to_i }
  meths.each do |m|
    self.send(m)
  end
  @hl.say "Generating..."
  tpl = Cinch::BotTemplate::Templates::Config.new.generate(
      nick:     @opts.dig('bot', 'nick'),
      multi:    @options.dig('multi-server'),
      networks: @options.dig('multi-server') ? @opts.dig('bot', 'networks') : @opts.dig('bot', 'server')

  )
  if @opts.fetch('stdout', nil)
    puts tpl
  else
    filename = directory.join(@opts.dig('bot', 'nick')+'.yml')
    open filename, 'a+' do |fd|
      fd.puts tpl
    end
    filename
  end
end

#get_001_bot_networksObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cinch/bot_template/classes/config.rb', line 34

def get_001_bot_networks
  if @options['multi-server']
    @hl.say "What networks? "
    nets     = {}
    networks = @hl.ask "> " do |q|
      q.gather = /#\$/
    end
    networks.each do |network|
      nets[network] = @hl.ask("Server for #{network}?")
    end
    @opts['bot']['networks'] = nets
  else
    @hl.say "Server "
    server                 = @hl.ask "> "
    @opts['bot']['server'] = server
  end

end

#get_002_bot_nameObject

Note:

The bot’s nickname



54
55
56
57
# File 'lib/cinch/bot_template/classes/config.rb', line 54

def get_002_bot_name
  @hl.say "What's the bot's main nickname"
  @opts['bot']['nick'] = @hl.ask "    > ", String
end

#parse_config_path(file_path) ⇒ Object

Parameters:

  • file_path (String)

    File path to config as string to parse



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cinch/bot_template/classes/config.rb', line 19

def parse_config_path(file_path)
  begin
    path = Pathname(file_path)
    path = File.expand_path(path)
    path.to_s
  rescue
    at_exit do
      puts "Could not parse config path. Exiting.."
    end

    exit 1
  end

end