Class: Nayati::Workflow

Inherits:
ApplicationRecord show all
Includes:
NameBasedConstantable
Defined in:
app/models/nayati/workflow.rb

Defined Under Namespace

Classes: ContextNotSetError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NameBasedConstantable

#camelcased_name, name_as_constant, name_as_namespace, #namespaced_name, #underscored_name, underscored_name

Instance Attribute Details

#contextObject

Returns the value of attribute context.



6
7
8
# File 'app/models/nayati/workflow.rb', line 6

def context
  @context
end

#initial_operation_nameObject

Returns the value of attribute initial_operation_name.



6
7
8
# File 'app/models/nayati/workflow.rb', line 6

def initial_operation_name
  @initial_operation_name
end

Class Method Details

.assign_from_creation_params(params) ⇒ Object



147
148
149
150
151
# File 'app/models/nayati/workflow.rb', line 147

def assign_from_creation_params(params)
  wf = self.new(name: params[:name])
  wf.initial_operation_name = params[:initial_operation_name]
  wf
end

.create_or_update_from_workflow_json(workflow_hash) ⇒ Object

] }



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/nayati/workflow.rb', line 72

def create_or_update_from_workflow_json(workflow_hash)
  ActiveRecord::Base.transaction do
    workflow_name = workflow_hash[:name]
    workflow_identifier = workflow_hash[:workflow_identifier]
    initial_operation_name = workflow_hash[:initial_operation_name]

    wf = find_or_initialize_by(name: workflow_name.to_s, identifier: workflow_identifier)
    wf.initial_operation_name = initial_operation_name
    wf.save!

    initial_operation_record = wf.operations.find_or_initialize_by(name: initial_operation_name)
    initial_operation_record.save!

    workflow_hash[:operations].each do |operation_hash|
      operation_name = operation_hash[:name].to_s
      operation_record = wf.operations.find_or_initialize_by(name: operation_name)

      after_failure_operation_name = operation_hash[:after_failure_operation_name].to_s
      if after_failure_operation_name.present?
        after_failure_operation_record = wf.operations.find_or_initialize_by(name: after_failure_operation_name)
        after_failure_operation_record.save!
        operation_record.after_failure_operation = after_failure_operation_record
      end

      after_success_operation_name = operation_hash[:after_success_operation_name].to_s
      if after_success_operation_name.present?
        after_success_operation_record = wf.operations.find_or_initialize_by(name: after_success_operation_name)
        after_success_operation_record.save!
        operation_record.after_success_operation = after_success_operation_record
      end

      operation_record.save!
    end
    # puts "---------------- printing operation sequences ----------------"
    # wf.print_sequences
    # puts "-----------------------------------------------------------------"
  end
end

.name_selection_optionsObject



54
55
56
# File 'app/models/nayati/workflow.rb', line 54

def name_selection_options
  Nayati::OperationConfiguration.workflow_names.map { |name| [name, name] }
end

Instance Method Details

#entire_workflow_as_hashObject



44
45
46
47
48
49
50
51
# File 'app/models/nayati/workflow.rb', line 44

def entire_workflow_as_hash
  {
    name: self.name,
    initial_operation_name: self.initial_operation.try(:name),
    workflow_identifier: self.identifier,
    operations: operations.map { |operation| operation.details_hash }
  }
end

#klassObject

Raises:



26
27
28
29
# File 'app/models/nayati/workflow.rb', line 26

def klass
  raise ContextNotSetError.new unless context.present?
  klass_name.constantize.new(context)
end

#klass_nameObject



22
23
24
# File 'app/models/nayati/workflow.rb', line 22

def klass_name
  name_space.camelcase + 'Workflow'
end

#name_spaceObject



18
19
20
# File 'app/models/nayati/workflow.rb', line 18

def name_space
  name.parameterize.underscore
end

#result_objObject



35
36
37
38
39
40
41
42
# File 'app/models/nayati/workflow.rb', line 35

def result_obj
  result_class = begin
                  Module.const_get(specific_result_obj_klass_constant_name)
                rescue NameError
                  ::Nayati::WorkflowResults::Base
                end
  result_class.new
end

#specific_result_obj_klass_constant_nameObject



31
32
33
# File 'app/models/nayati/workflow.rb', line 31

def specific_result_obj_klass_constant_name
  "Nayati::WorkflowResults::#{klass_name}"
end