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

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

Constant Summary collapse

RESERVED_ARG_NAMES =
%w[opts options].freeze

Instance Method Summary collapse

Instance Method Details

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



59
60
61
62
63
64
65
# File 'lib/legion/extensions/lex/runners/function.rb', line 59

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

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



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/lex/runners/function.rb', line 12

def create(runner_id:, name:, active: true, **opts)
  existing = find_cached_function(name, runner_id)
  return update(function_id: existing.values[:id], name: name, active: active, **opts) if existing # rubocop:disable Legion/Extension/RunnerReturnHash

  insert = { runner_id: runner_id, name: name.to_s, active: active }
  insert[:args] = json_dump(opts[:formatted_args]) if opts.key?(:formatted_args)

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

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



50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/lex/runners/function.rb', line 50

def delete(function_id:, **_opts)
  record = Legion::Data::Model::Function[function_id]
  return { success: false, reason: 'not found' } if record.nil?

  record.delete
  reload_static_caches
  { success: true, function_id: function_id }
end

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



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

def get(function_id:, **_opts)
  record = Legion::Data::Model::Function[function_id]
  return { success: false, reason: 'not found' } if record.nil?

  { success: true, values: record.values }
end

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



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

def update(function_id:, **opts)
  function = Legion::Data::Model::Function[function_id]
  return { success: false, reason: 'function not found' } if function.nil?

  changes = {}
  changes[:active] = opts[:active] if opts.key?(:active) && function.values[:active] != opts[:active]

  if opts.key?(:formatted_args)
    args = json_dump(opts[:formatted_args])
    changes[:args] = args unless args == function.values[:args]
  end

  return { success: true, changed: false, function_id: function_id } if changes.empty?

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