Skip to content

Commit

Permalink
Extract the registration request from the registration service
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed May 7, 2019
1 parent 7b36240 commit 3e9b48e
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 151 deletions.
39 changes: 15 additions & 24 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-05-02 14:26:35 -0500 using RuboCop version 0.65.0.
# on 2019-05-07 10:46:45 -0700 using RuboCop version 0.65.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -29,27 +29,27 @@ Lint/UriEscapeUnescape:
- 'app/controllers/sdr_controller.rb'
- 'spec/controllers/sdr_controller_spec.rb'

# Offense count: 22
# Offense count: 23
Metrics/AbcSize:
Max: 122
Max: 94

# Offense count: 3
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 139
Max: 124

# Offense count: 4
# Offense count: 5
Metrics/CyclomaticComplexity:
Max: 27
Max: 11

# Offense count: 27
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 63
Max: 43

# Offense count: 3
# Offense count: 4
Metrics/PerceivedComplexity:
Max: 27
Max: 11

# Offense count: 9
Naming/AccessorMethodName:
Expand Down Expand Up @@ -155,7 +155,7 @@ RSpec/ExpectInHook:
- 'spec/services/publish_metadata_service_spec.rb'
- 'spec/services/registration_service_spec.rb'

# Offense count: 127
# Offense count: 111
# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Exclude:
Expand All @@ -178,11 +178,6 @@ RSpec/MessageSpies:
- 'spec/services/registration_service_spec.rb'
- 'spec/services/version_service_spec.rb'

# Offense count: 2
# Configuration parameters: AggregateFailuresByDefault.
RSpec/MultipleExpectations:
Max: 11

# Offense count: 22
RSpec/NestedGroups:
Max: 5
Expand Down Expand Up @@ -261,7 +256,7 @@ Style/ConditionalAssignment:
Exclude:
- 'spec/support/foxml_helper.rb'

# Offense count: 13
# Offense count: 12
Style/Documentation:
Exclude:
- 'spec/**/*'
Expand All @@ -275,7 +270,6 @@ Style/Documentation:
- 'app/models/dor/service_item.rb'
- 'app/models/dor/update_marc_record_service.rb'
- 'app/services/dublin_core_service.rb'
- 'app/services/registration_service.rb'
- 'config/application.rb'
- 'config/initializers/okcomputer.rb'

Expand Down Expand Up @@ -363,17 +357,14 @@ Style/StringLiterals:

# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, MinSize.
# Configuration parameters: .
# SupportedStyles: percent, brackets
Style/SymbolArray:
Exclude:
- 'app/controllers/versions_controller.rb'
- 'app/services/release_tags.rb'
- 'lib/tasks/rspec.rake'
EnforcedStyle: percent
MinSize: 4

# Offense count: 3
# Offense count: 2
# Cop supports --auto-correct.
Style/ZeroLengthPredicate:
Exclude:
- 'app/models/dor/update_marc_record_service.rb'
- 'app/services/registration_service.rb'
103 changes: 103 additions & 0 deletions app/models/registration_request.rb
@@ -0,0 +1,103 @@
# frozen_string_literal: true

require 'uuidtools'

# This represents the request a user could make to register an object
class RegistrationRequest
# @param [Hash{Symbol => various}] params
# @option params [String] :object_type required
# @option params [String] :label required
# @option params [String] :admin_policy required
# @option params [String] :metadata_source
# @option params [String] :rights
# @option params [String] :collection
# @option params [Hash{String => String}] :source_id Primary ID from another system, max one key/value pair!
# @option params [Hash] :other_ids including :uuid if known
# @option params [String] :pid Fully qualified PID if you don't want one generated for you
# @option params [Integer] :workflow_priority]
# @option params [Array<String>] :seed_datastream datastream_names (only 'descMetadata' is a permitted value)
# @option params [Array<String>] :initiate_workflow workflow_ids
# @option params [Array] :tags
def initialize(params)
@params = params
end

# Raises Dor::ParameterError if the parameters aren't valid
def validate!
%i[object_type label admin_policy].each do |required_param|
raise Dor::ParameterError, "#{required_param.inspect} must be specified in call to register_object" unless params[required_param]
end
raise Dor::ParameterError, 'label cannot be empty to call register_object' if params[:label].empty? && %w[label none].include?(metadata_source)

raise Dor::ParameterError, "Unknown item type: '#{object_type}'" if item_class.nil?

raise Dor::ParameterError, "Unknown value for seed_datastream: '#{params[:seed_datastream]}'" if params[:seed_datastream] && params[:seed_datastream] != ['descMetadata']

raise ArgumentError, ":source_id Hash can contain at most 1 pair: recieved #{source_id.size}" if source_id.size > 1

raise Dor::ParameterError, "Unknown rights setting '#{rights}' when calling #{name}.register_object" if rights && rights != 'default' && !Dor::RightsMetadataDS.valid_rights_type?(rights)
end

def pid
params[:pid]
end

# TODO: Move to validate
def rights
params[:rights]
end

def initiate_workflow
Array(params[:initiate_workflow])
end

def workflow_priority
params[:workflow_priority] ? params[:workflow_priority].to_i : 0
end

def object_type
params[:object_type]
end

def admin_policy
params[:admin_policy]
end

def item_class
Dor.registered_classes[object_type]
end

def metadata_source
params[:metadata_source]
end

def seed_desc_metadata
params[:seed_datastream] && params[:seed_datastream] == ['descMetadata']
end

def label
params[:label].length > 254 ? params[:label][0, 254] : params[:label]
end

def source_id
params[:source_id] || {}
end

def other_ids
hash = params[:other_ids] || {}
hash[:uuid] ||= UUIDTools::UUID.timestamp_create.to_s
hash
end

def tags
params[:tags] || []
end

def collection
params[:collection]
end

private

attr_reader :params
end

0 comments on commit 3e9b48e

Please sign in to comment.