module Legion::Extensions::Lex::Runners::Function

Public Class Methods

build_args(raw_args:, **_opts) click to toggle source
# 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

Public Instance Methods

create(runner_id:, name:, active: 1, **opts) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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