module MiGA::Dataset::Hooks

def default_hooks

Dataset hooks triggered by default
#
def default_hooks
  {
    on_create: [[:recalculate_status]],
    on_save: [[:check_type]],
    on_activate: [[:clear_run_counts], [:recalculate_status]],
    on_inactivate: [[:recalculate_status]],
    on_result_ready: [[:_pull_result_hooks]],
    on_preprocessing_ready: [[:clear_run_counts], [:recalculate_status]],
  }
end

def hook__pull_result_hooks(_hook_args, event_args)

Pull the hook specific to the type of result
Dataset Action :pull_result_hooks([], [res])
#
def hook__pull_result_hooks(_hook_args, event_args)
  pull_hook(:"on_result_ready_#{event_args.first}", *event_args)
  pull_hook(:on_preprocessing_ready) if done_preprocessing?
end

def hook_check_type(_hook_args, _event_args)

Ensure that the dataset type exists and is compatible with the project type
#
def hook_check_type(_hook_args, _event_args)
  check_type
end

def hook_clear_run_counts(_hook_args, _event_args)

Clear metadata from run counts
#
def hook_clear_run_counts(_hook_args, _event_args)
  metadata
    .data.keys
    .select { |k| k.to_s =~ /^_try_/ }
    .each { |k| metadata[k] = nil }
  metadata[:_step] = nil
  save
end

def hook_recalculate_status(_hook_args, _event_args)

Recalculate the dataset status and save in metadata
#
def hook_recalculate_status(_hook_args, _event_args)
  recalculate_status
end

def hook_run_cmd(hook_args, event_args)

- +event_args+: +[object (optional)]+
- +hook_args+: +[cmd]+
dataset, project, project_name, miga, object (if defined for the event)
Run +cmd+ in the command-line with {{variables}}:
#
def hook_run_cmd(hook_args, event_args)
  Process.wait(
    spawn hook_args.first.miga_variables(
      dataset: name, project: project.path, project_name: project.name,
      miga: MiGA::MiGA.root_path, object: event_args.first
    )
  )
end