-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_parameter.rb
More file actions
45 lines (34 loc) · 1.44 KB
/
node_parameter.rb
File metadata and controls
45 lines (34 loc) · 1.44 KB
1
2
3
4
5
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
40
41
42
43
44
45
# frozen_string_literal: true
class NodeParameter < ApplicationRecord
belongs_to :parameter_definition, inverse_of: :node_parameters
belongs_to :reference_value, optional: true, autosave: true
belongs_to :function_value, class_name: 'NodeFunction', optional: true, inverse_of: :node_parameter_values
belongs_to :node_function, class_name: 'NodeFunction', inverse_of: :node_parameters
validate :only_one_value_present
before_destroy :destroy_value_objects
def to_grpc
param = Tucana::Shared::NodeParameter.new(
database_id: id,
runtime_parameter_id: parameter_definition.runtime_parameter_definition.runtime_name
)
param.value = Tucana::Shared::NodeValue.new(literal_value: Tucana::Shared::Value.from_ruby({}))
if reference_value.present?
param.value.reference_value = reference_value.to_grpc
elsif function_value.present?
param.value.node_function_id = function_value.id
else
param.value.literal_value = Tucana::Shared::Value.from_ruby(literal_value)
end
param
end
private
def only_one_value_present
values = [!literal_value.nil?, reference_value.present?, function_value.present?]
return if values.count(true) <= 1
errors.add(:value, 'Only one of literal_value, reference_value, or function_value must be present')
end
def destroy_value_objects
reference_value.destroy if reference_value.present?
function_value.destroy if function_value.present?
end
end