Sha256: 2c9cde5b5507d2ab9e5bba14d856e9a2b9a5dd678aa44091996841ad1ee18ec9

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Ad
  module AgentArchitecture
    module Database
      # Save workflow graph to database
      class SaveWorkflowGraph
        def initialize(workflow_hash)
          @workflow_hash = workflow_hash
        end

        def save
          DB.transaction do
            # Save workflow
            workflow_record = Ad::AgentArchitecture::Database::Workflow.create(name: @workflow_hash[:name])

            # Save attributes
            attribute_records = @workflow_hash[:attributes].map do |_name, attr|
              Ad::AgentArchitecture::Database::Attribute.create(
                name: attr[:name], type: attr[:type], is_array: attr[:is_array], workflow: workflow_record
              )
            end
            attribute_map = attribute_records.to_h { |ar| [ar.name.to_sym, ar] }

            # Save sections and steps
            @workflow_hash[:sections].each do |section|
              section_record = Ad::AgentArchitecture::Database::Section.create(
                name: section[:name], order: section[:order], workflow: workflow_record
              )

              section[:steps].each do |step|
                step_record = Ad::AgentArchitecture::Database::Step.create(
                  name: step[:name], order: step[:order], prompt: step[:prompt], section: section_record
                )

                step[:input_attributes].each do |attr_name|
                  Ad::AgentArchitecture::Database::InputAttribute.create(
                    step: step_record, attribute: attribute_map[attr_name.to_sym]
                  )
                end

                step[:output_attributes].each do |attr_name|
                  Ad::AgentArchitecture::Database::OutputAttribute.create(
                    step: step_record, attribute: attribute_map[attr_name.to_sym]
                  )
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ad-agent_architecture-0.0.8 lib/ad/agent_architecture/database/save_workflow_graph.rb