scinote-web/app/utilities/protocol_importers/protocol_intermediate_object.rb

53 lines
1.6 KiB
Ruby
Raw Normal View History

2019-05-21 23:16:30 +08:00
# frozen_string_literal: true
module ProtocolImporters
class ProtocolIntermediateObject
attr_accessor :normalized_protocol_data, :user, :team, :protocol
2019-05-21 23:16:30 +08:00
def initialize(normalized_json: {}, user:, team:)
@normalized_protocol_data = normalized_json.with_indifferent_access[:protocol] if normalized_json
2019-05-21 23:16:30 +08:00
@user = user
@team = team
end
def import
build unless @protocol
@protocol.save
@protocol
2019-05-21 23:16:30 +08:00
end
def build
@protocol = Protocol.new(protocol_attributes)
@protocol.description = ProtocolDescriptionBuilder.generate(@normalized_protocol_data&.reject { |k| k == :steps })
@protocol.steps << build_steps
@protocol
2019-05-21 23:16:30 +08:00
end
private
def build_steps
@normalized_protocol_data[:steps].map do |s|
step = Step.new(step_attributes(s))
step.description = StepDescriptionBuilder.generate(s)
step.assets << AttachmentsBuilder.generate(s)
step.tables << TablesBuilder.extract_tables_from_html_string(s[:description][:body])
step
2019-05-21 23:16:30 +08:00
end
end
def protocol_attributes
defaults = { protocol_type: :in_repository_public, added_by: @user, team: @team }
values = %i(name published_on authors)
p_attrs = @normalized_protocol_data.slice(*values).each_with_object({}) do |(k, v), h|
2019-05-21 23:16:30 +08:00
h[k] = k == 'published_on' ? Time.at(v) : v
end
p_attrs.merge!(defaults)
end
def step_attributes(step_json)
defaults = { user: @user, completed: false }
step_json.slice(:name, :position).merge!(defaults)
2019-05-21 23:16:30 +08:00
end
end
end