Class: Perus::Server::CommandConfig

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/perus/server/models/command_config.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_with_params(params) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/perus/server/models/command_config.rb', line 50

def self.create_with_params(params)
    options = process_options(params)
    CommandConfig.create(
        command: params['command'],
        options: options
    )
end

.process_options(params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/perus/server/models/command_config.rb', line 29

def self.process_options(params)
    return {} if params['options'].nil?

    # ignore empty options (will use default)
    options = params['options'].reject do |attr, value|
        value.empty?
    end

    # replace 'true' and 'false' with actual boolean values
    # but only for boolean options
    command = Perus::Pinger.const_get(params['command'])
    command.options.each do |option|
        next unless option.boolean?
        next unless options.include?(option.name.to_s)
        value = options[option.name.to_s]
        options[option.name.to_s] = value == 'true'
    end

    options
end

Instance Method Details

#command_classObject



20
21
22
# File 'lib/perus/server/models/command_config.rb', line 20

def command_class
    Perus::Pinger.const_get(command)
end

#config_hashObject



7
8
9
10
11
12
13
# File 'lib/perus/server/models/command_config.rb', line 7

def config_hash
    {
        id: id,
        type: command,
        options: options
    }
end

#update_options!(params) ⇒ Object



24
25
26
27
# File 'lib/perus/server/models/command_config.rb', line 24

def update_options!(params)
    self.options = self.class.process_options(params)
    save
end

#validateObject



15
16
17
18
# File 'lib/perus/server/models/command_config.rb', line 15

def validate
    super
    validates_presence :command
end