Module: Legion::Extensions::Lex::Runners::Function

Includes:
Helpers::Lex
Defined in:
lib/legion/extensions/lex/runners/function.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_args(raw_args:, **_opts) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/legion/extensions/lex/runners/function.rb', line 47

def self.build_args(raw_args:, **_opts)
  args = {}
  raw_args.each do |arg|
    args[arg[1]] = arg[0] unless %w[opts options].include? arg[1]
  end
  { success: true, formatted_args: args }
end

Instance Method Details

#create(runner_id:, name:, active: 1, **opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/lex/runners/function.rb', line 6

def create(runner_id:, name:, active: 1, **opts)
  exist = Legion::Data::Model::Function.where(name: name.to_s).where(runner_id: runner_id).first
  unless exist.nil?
    log.debug "function: #{exist.values[:id]} already exists, updating it"
    update_hash = { function_id: exist.values[:id], name: name, active: active, **opts }
    return Legion::Runner.run(runner_class: 'Legion::Extensions::Lex::Runners::Function',
                              function:     'update',
                              args:         update_hash,
                              parent_id:    opts[:task_id],
                              master_id:    opts[:master_id])
  end
  insert = { runner_id: runner_id, name: name.to_s, active: active }
  insert[:args] = Legion::JSON.dump(opts[:formatted_args]) if opts.key? :formatted_args

  { success: true, function_id: Legion::Data::Model::Function.insert(insert) }
end

#delete(function_id:, **_opts) ⇒ Object



43
44
45
# File 'lib/legion/extensions/lex/runners/function.rb', line 43

def delete(function_id:, **_opts)
  { function_id: function_id, result: Legion::Data::Model::Function[function_id].delete }
end

#get(function_id:, **_opts) ⇒ Object



39
40
41
# File 'lib/legion/extensions/lex/runners/function.rb', line 39

def get(function_id:, **_opts)
  { function_id: function_id, values: Legion::Data::Model::Function[function_id].values }
end

#update(function_id:, **opts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/lex/runners/function.rb', line 23

def update(function_id:, **opts)
  function = Legion::Data::Model::Function[function_id]
  update = {}
  update[:active] = true unless function.values[:active]

  if opts.key? :formatted_args
    args = JSON.dump(opts[:formatted_args])
    update[:args] = args unless args == function.values[:args]
  end

  return { success: true, changed: false, function_id: function_id } if update.count.zero?

  function.update(update)
  { success: true, changed: true, updates: update, function_id: function_id }
end