Skip to content

Commit

Permalink
Preferring Array.wrap() over Array()
Browse files Browse the repository at this point in the history
`Array()` is a Ruby core method for coercing objects into an Array.
`Array.wrap()` is an ActiveSupport method that behaves a bit more as
expected.

The primary example of why to prefer `Array.wrap` is the following:

```ruby
now = Time.now
Array.wrap(now) == [now]
=> true
Array(now) == [now]
=> false
Array(now)
=> [43, 53, 13, 5, 5, 2016, 4, 126, true, "EDT"]
```
  • Loading branch information
jeremyf committed May 5, 2016
1 parent 35efa63 commit 5e71eb2
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/actors/curation_concerns/base_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def save
end

def apply_save_data_to_curation_concern(attributes)
attributes[:rights] = Array(attributes[:rights]) if attributes.key? :rights
attributes[:rights] = Array.wrap(attributes[:rights]) if attributes.key? :rights
remove_blank_attributes!(attributes)
curation_concern.attributes = attributes.symbolize_keys
curation_concern.date_modified = CurationConcerns::TimeService.time_in_utc
Expand Down
2 changes: 1 addition & 1 deletion app/forms/curation_concerns/forms/work_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def multiple?(term)
# Overriden to cast 'rights' to an array
def sanitize_params(form_params)
super.tap do |params|
params['rights'] = Array(params['rights']) if params.key?('rights')
params['rights'] = Array.wrap(params['rights']) if params.key?('rights')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/curation_concerns/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module CurationConcerns
module Serializers
def to_s
if title.present?
Array(title).join(' | ')
Array.wrap(title).join(' | ')
elsif label.present?
Array(label).join(' | ')
Array.wrap(label).join(' | ')
else
'No Title'
end
Expand Down
12 changes: 6 additions & 6 deletions app/models/concerns/curation_concerns/solr_document_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def hydra_model
end

def human_readable_type
Array(self[Solrizer.solr_name('human_readable_type', :stored_searchable)]).first
Array.wrap(self[Solrizer.solr_name('human_readable_type', :stored_searchable)]).first
end

def representative_id
Expand All @@ -58,24 +58,24 @@ def date_uploaded
end

def depositor(default = '')
val = Array(self[Solrizer.solr_name('depositor')]).first
val = Array.wrap(self[Solrizer.solr_name('depositor')]).first
val.present? ? val : default
end

def title
Array(self[Solrizer.solr_name('title')]).first
Array.wrap(self[Solrizer.solr_name('title')]).first
end

def description
Array(self[Solrizer.solr_name('description')]).first
Array.wrap(self[Solrizer.solr_name('description')]).first
end

def label
Array(self[Solrizer.solr_name('label')]).first
Array.wrap(self[Solrizer.solr_name('label')]).first
end

def file_format
Array(self[Solrizer.solr_name('file_format')]).first
Array.wrap(self[Solrizer.solr_name('file_format')]).first
end

def creator
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/curation_concerns/work_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _to_partial_path #:nodoc:

def to_s
if title.present?
Array(title).join(' | ')
Array.wrap(title).join(' | ')
else
'No Title'
end
Expand Down
4 changes: 2 additions & 2 deletions app/presenters/curation_concerns/file_set_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def initialize(solr_document, current_ability)
:depositor, :tags, :title_or_label, to: :solr_document

def page_title
Array(solr_document['label_tesim']).first
Array.wrap(solr_document['label_tesim']).first
end

def link_name
current_ability.can?(:read, id) ? Array(solr_document['label_tesim']).first : 'File'
current_ability.can?(:read, id) ? Array.wrap(solr_document['label_tesim']).first : 'File'
end
end
end
2 changes: 1 addition & 1 deletion app/renderers/curation_concerns/attribute_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def render
return markup if !values.present? && !options[:include_empty]
markup << %(<tr><th>#{label}</th>\n<td><ul class='tabular'>)
attributes = microdata_object_attributes(field).merge(class: "attribute #{field}")
Array(values).each do |value|
Array.wrap(values).each do |value|
markup << "<li#{html_attributes(attributes)}>#{attribute_value_to_html(value.to_s)}</li>"
end
markup << %(</ul></td></tr>)
Expand Down
4 changes: 2 additions & 2 deletions app/search_builders/curation_concerns/filter_by_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def work_types
end

def work_clauses
return [] if blacklight_params.key?(:f) && Array(blacklight_params[:f][:generic_type_sim]).include?('Collection')
return [] if blacklight_params.key?(:f) && Array.wrap(blacklight_params[:f][:generic_type_sim]).include?('Collection')
work_types.map do |klass|
ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: klass.to_class_uri)
end
end

def collection_clauses
return [] if blacklight_params.key?(:f) && Array(blacklight_params[:f][:generic_type_sim]).include?('Work')
return [] if blacklight_params.key?(:f) && Array.wrap(blacklight_params[:f][:generic_type_sim]).include?('Work')
[ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: ::Collection.to_class_uri)]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/curation_concerns/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def lock_retry_delay
# Registers the given curation concern model in the configuration
# @param [Array<Symbol>,Symbol] curation_concern_types
def register_curation_concern(*curation_concern_types)
Array(curation_concern_types).flatten.compact.each do |cc_type|
Array.wrap(curation_concern_types).flatten.compact.each do |cc_type|
unless @registered_concerns.include?(cc_type)
@registered_concerns << cc_type
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/file_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
context 'when it is initialized' do
it 'has empty arrays for all the properties' do
subject.attributes.each do |_k, v|
expect(Array(v)).to eq([])
expect(Array.wrap(v)).to eq([])
end
end
end
Expand Down

0 comments on commit 5e71eb2

Please sign in to comment.