Skip to content

Commit

Permalink
avoid false positive from .try(:internal_resource)
Browse files Browse the repository at this point in the history
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>>
```
  • Loading branch information
bkiahstroud committed Sep 26, 2024
1 parent 62517e6 commit 5ae4568
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions lib/bulkrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,22 @@ 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=...
# ```
ir = begin
collection_model_class.internal_resource
rescue NoMethodError
nil
end

ir.presence || collection_model_class.to_s
end

def file_model_class
Expand All @@ -108,7 +123,22 @@ 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=...
# ```
ir = begin
file_model_class.internal_resource
rescue NoMethodError
nil
end

ir.presence || file_model_class.to_s
end

def curation_concerns
Expand All @@ -118,7 +148,24 @@ 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=...
# ```
ir = begin
cc.internal_resource
rescue NoMethodError
nil
end

ir.presence || cc.to_s
end.uniq
end

attr_writer :ingest_queue_name
Expand Down

0 comments on commit 5ae4568

Please sign in to comment.