Skip to content

Instantly share code, notes, and snippets.

@mjgiarlo
Created July 15, 2015 18:32
Show Gist options
  • Save mjgiarlo/25dc3f9c85baab1ccdf6 to your computer and use it in GitHub Desktop.
Save mjgiarlo/25dc3f9c85baab1ccdf6 to your computer and use it in GitHub Desktop.
A pattern for handling CurationConcerns events in Sufia::Core
# These events are triggered by actions within CurationConcerns Actors
CurationConcerns.config.after_create_content = lambda { |generic_file, user|
Sufia::CreateContentService.new(generic_file, user).call
# which will call CurationConcerns.queue.push(ContentDepositEventJob.new(generic_file.id, user.user_key))
}
CurationConcerns.config.after_revert_content = lambda { |generic_file, user, revision|
Sufia::RevertContentService.new(generic_file, user, revision).call
# which will call CurationConcerns.queue.push(ContentRestoredVersionEventJob.new(generic_file.id, user.user_key, revision))
}
CurationConcerns.config.after_update_content = lambda { |generic_file, user|
Sufia::UpdateContentService.new(generic_file, user).call
# which will call CurationConcerns.queue.push(ContentNewVersionEventJob.new(generic_file.id, user.user_key))
}
CurationConcerns.config.after_update_metadata = lambda { |generic_file, user|
Sufia::UpdateMetadataService.new(generic_file, user).call
# which will call CurationConcerns.queue.push(ContentUpdateEventJob.new(generic_file.id, user.user_key))
}
CurationConcerns.config.after_destroy = lambda { |destroyed_id, user|
Sufia::DestroyService.new(destroyed_id, user).call
# which will call CurationConcerns.queue.push(ContentDeleteEventJob.new(destroyed_id, user.user_key))
}
CurationConcerns.config.after_audit_failure = lambda { |generic_file, user, log_date|
Sufia::AuditFailureService.new(generic_file, user, log_date).call
# which will call CurationConcerns.queue.push(AuditFailureEventJob.new(generic_file.id, user.user_key, log_date))
}
CurationConcerns.config.after_import_url_success = lambda { |generic_file, user|
Sufia::ImportUrlSuccessService.new(generic_file, user).call
# which will call CurationConcerns.queue.push(ImportUrlSuccessEventJob.new(generic_file.id, user.user_key))
# ... which in turn will call CurationConcerns.queue.push(ContentDepositEventJob.new(generic_file.id, user.user_key))
}
CurationConcerns.config.after_import_url_failure = lambda { |generic_file, user|
Sufia::ImportUrlFailureService.new(generic_file, user).call
# which will call CurationConcerns.queue.push(ImportUrlFailureEventJob.new(generic_file.id, user.user_key))
}
CurationConcerns.config.after_import_local_file_success = lambda { |generic_file, user, filename|
Sufia::ImportLocalFileSuccessService.new(generic_file, user, filename).call
# which will call CurationConcerns.queue.push(ImportLocalFileSuccessEventJob.new(generic_file.id, user.user_key, filename))
}
CurationConcerns.config.after_import_local_file_failure = lambda { |generic_file, user, filename|
Sufia::ImportLocalFileFailureService.new(generic_file, user, filename).call
# which will call CurationConcerns.queue.push(ImportLocalFileFailureEventJob.new(generic_file.id, user.user_key, filename))
}
@flyingzumwalt
Copy link

According to the approach proposed here https://wiki.duraspace.org/display/hydra/Service+Object+Approach, especially in http://brewhouse.io/blog/2014/04/30/gourmet-service-objects.html you would name the services differently and call them differently.

Example:

Instead of

Sufia::ImportLocalFileSuccessService.new(generic_file, user, filename).call

you would do

Sufia::ReportImportLocalFileSuccess.call(generic_file, user, filename)

I agree with @terrellt's skepticism about using services at all, as opposed to other design patterns that are clearer and more consistent, but if you do implement Services then I agree with that blog post's suggestion that

  • you should be calling a class method
  • the class name should start with an action/verb
  • the class should not have Service in the name (I'm less strongly convinced of this one)

@flyingzumwalt
Copy link

Also (separate from previous comment): I suspect that CurationConcerns should be providing abstract classes for these services since it makes assumptions about what variables they will need (ie. it assumes that after_import_local_file_failure will need generic_file, user, filename while after_audit_failure will need generic_file, user, log_date, but there's no spot for testing or documenting those assumptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment