Skip to content

Commit

Permalink
Avoid false positive from .try(:internal_resource) (#980)
Browse files Browse the repository at this point in the history
* avoid false positive from .try(:internal_resource)

Using #try on :internal_resource can yield unexpected results.
If the method is undefined, it can return a truthy value instead of
the typical nil.

E.g.

```ruby
Hyrax::FileSet.try(:internal_resource) || 'hi'
=> #<Dry::Types::Result::Failure input=:internal_resource error=#<Dry::Struct::Error: [Hyrax::FileSet.new] can't convert Symbol into Hash>>
```

* favor #respond_to? over rescuing errors

Co-authored-by: Rob Kaufman <[email protected]>

---------

Co-authored-by: Rob Kaufman <[email protected]>
  • Loading branch information
bkiahstroud and orangewolf authored Sep 27, 2024
1 parent 62517e6 commit 92d6042
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions lib/bulkrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,20 @@ def collection_model_class
attr_writer :collection_model_class

def collection_model_internal_resource
collection_model_class.try(:internal_resource) || collection_model_class.to_s
# WARN: Using #try on :internal_resource can yield unexpected results.
# If the method is undefined, it can return a truthy value instead of
# the typical nil.
#
# E.g.
# ```ruby
# Hyrax::FileSet.try(:internal_resource) || 'hi'
# => #<Dry::Types::Result::Failure input=:internal_resource error=...
# ```
if collection_model_class.respond_to?(:internal_resource)
collection_model_class.internal_resource
else
collection_model_class.to_s
end
end

def file_model_class
Expand All @@ -108,7 +121,20 @@ def file_model_class
attr_writer :file_model_class

def file_model_internal_resource
file_model_class.try(:internal_resource) || file_model_class.to_s
# WARN: Using #try on :internal_resource can yield unexpected results.
# If the method is undefined, it can return a truthy value instead of
# the typical nil.
#
# E.g.
# ```ruby
# Hyrax::FileSet.try(:internal_resource) || 'hi'
# => #<Dry::Types::Result::Failure input=:internal_resource error=...
# ```
if file_model_class.respond_to?(:internal_resource)
file_model_class.internal_resource
else
file_model_class.to_s
end
end

def curation_concerns
Expand All @@ -118,7 +144,18 @@ def curation_concerns
attr_writer :curation_concerns

def curation_concern_internal_resources
curation_concerns.map { |cc| cc.try(:internal_resource) || cc.to_s }.uniq
curation_concerns.map do |cc|
# WARN: Using #try on :internal_resource can yield unexpected results.
# If the method is undefined, it can return a truthy value instead of
# the typical nil.
#
# E.g.
# ```ruby
# Hyrax::FileSet.try(:internal_resource) || 'hi'
# => #<Dry::Types::Result::Failure input=:internal_resource error=...
# ```
cc.respond_to?(:internal_resource) ? cc.internal_resource : cc.to_s
end.uniq
end

attr_writer :ingest_queue_name
Expand Down

0 comments on commit 92d6042

Please sign in to comment.