Class: Legion::Workflow::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/workflow/loader.rb

Instance Method Summary collapse

Instance Method Details

#install(manifest) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/workflow/loader.rb', line 6

def install(manifest)
  return { success: false, errors: manifest.errors } unless manifest.valid?

  missing = check_requirements(manifest.requires)
  return { success: false, error: :missing_gems, gems: missing } if missing.any?

  chain_id = find_or_create_chain(manifest.name)
  ids = []

  manifest.relationships.each_with_index do |rel, idx|
    trigger_id = resolve_function_id(rel[:trigger])
    return { success: false, error: :trigger_not_found, relationship: rel[:name] || idx } unless trigger_id

    action_id = resolve_function_id(rel[:action])
    return { success: false, error: :action_not_found, relationship: rel[:name] || idx } unless action_id

    id = Legion::Data::Model::Relationship.insert(
      trigger_id:        trigger_id,
      action_id:         action_id,
      name:              rel[:name],
      chain_id:          chain_id,
      conditions:        rel[:conditions] ? Legion::JSON.dump(rel[:conditions]) : nil,
      transformation:    rel[:transformation] ? Legion::JSON.dump(rel[:transformation]) : nil,
      delay:             rel.fetch(:delay, 0),
      allow_new_chains:  idx.zero? || rel[:allow_new_chains],
      active:            true,
      status:            'active',
      relationship_type: 'chain'
    )
    ids << id
  end

  { success: true, chain_id: chain_id, relationship_ids: ids }
end

#listObject



52
53
54
55
56
57
58
# File 'lib/legion/workflow/loader.rb', line 52

def list
  Legion::Data::Model::Chain.all.map do |chain|
    v = chain.values
    rel_count = Legion::Data::Model::Relationship.where(chain_id: v[:id]).count
    { id: v[:id], name: v[:name], relationships: rel_count }
  end
end

#status(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/legion/workflow/loader.rb', line 60

def status(name)
  chain = Legion::Data::Model::Chain.where(name: name).first
  return { success: false, error: :not_found } unless chain

  chain_id = chain.values[:id]
  rels = Legion::Data::Model::Relationship
         .where(chain_id: chain_id)
         .all
         .map { |r| format_relationship(r) }

  { success: true, name: name, chain_id: chain_id, relationships: rels }
end

#uninstall(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/workflow/loader.rb', line 41

def uninstall(name)
  chain = Legion::Data::Model::Chain.where(name: name).first
  return { success: false, error: :not_found } unless chain

  chain_id = chain.values[:id]
  count = Legion::Data::Model::Relationship.where(chain_id: chain_id).delete
  chain.delete

  { success: true, deleted_relationships: count }
end