Skip to content

Commit

Permalink
Merge pull request #4340 from rafaelfranca/patch-1
Browse files Browse the repository at this point in the history
Remove more Array.wrap calls
  • Loading branch information
josevalim committed Jan 6, 2012
2 parents e09c3c7 + 4311fc4 commit 75a630e
Show file tree
Hide file tree
Showing 26 changed files with 44 additions and 66 deletions.
5 changes: 2 additions & 3 deletions activemodel/lib/active_model/callbacks.rb
@@ -1,4 +1,3 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/callbacks'

module ActiveModel
Expand Down Expand Up @@ -93,7 +92,7 @@ def define_model_callbacks(*callbacks)
:only => [:before, :around, :after]
}.merge(options)

types = Array.wrap(options.delete(:only))
types = Array(options.delete(:only))

callbacks.each do |callback|
define_callbacks(callback, options)
Expand Down Expand Up @@ -125,7 +124,7 @@ def _define_after_model_callback(klass, callback) #:nodoc:
def self.after_#{callback}(*args, &block)
options = args.extract_options!
options[:prepend] = true
options[:if] = Array.wrap(options[:if]) << "!halted && value != false"
options[:if] = Array(options[:if]) << "!halted && value != false"
set_callback(:#{callback}, :after, *(args << options), &block)
end
CALLBACK
Expand Down
3 changes: 1 addition & 2 deletions activemodel/lib/active_model/observing.rb
@@ -1,6 +1,5 @@
require 'singleton'
require 'active_model/observer_array'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/module/remove_method'
require 'active_support/core_ext/string/inflections'
Expand Down Expand Up @@ -200,7 +199,7 @@ def observe(*models)
# end
# end
def observed_classes
Array.wrap(observed_class)
Array(observed_class)
end

# The class observed by default is inferred from the observer's class name:
Expand Down
6 changes: 3 additions & 3 deletions activemodel/lib/active_model/serialization.rb
Expand Up @@ -73,15 +73,15 @@ def serializable_hash(options = nil)

attribute_names = attributes.keys.sort
if only = options[:only]
attribute_names &= Array.wrap(only).map(&:to_s)
attribute_names &= Array(only).map(&:to_s)
elsif except = options[:except]
attribute_names -= Array.wrap(except).map(&:to_s)
attribute_names -= Array(except).map(&:to_s)
end

hash = {}
attribute_names.each { |n| hash[n] = read_attribute_for_serialization(n) }

method_names = Array.wrap(options[:methods]).select { |n| respond_to?(n) }
method_names = Array(options[:methods]).select { |n| respond_to?(n) }
method_names.each { |n| hash[n] = send(n) }

serializable_add_includes(options) do |association, records, opts|
Expand Down
5 changes: 2 additions & 3 deletions activemodel/lib/active_model/serializers/xml.rb
@@ -1,4 +1,3 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/hash/conversions'
Expand Down Expand Up @@ -56,7 +55,7 @@ def serializable_hash
end

def serializable_collection
methods = Array.wrap(options[:methods]).map(&:to_s)
methods = Array(options[:methods]).map(&:to_s)
serializable_hash.map do |name, value|
name = name.to_s
if methods.include?(name)
Expand Down Expand Up @@ -146,7 +145,7 @@ def add_associations(association, records, opts)

def add_procs
if procs = options.delete(:procs)
Array.wrap(procs).each do |proc|
Array(procs).each do |proc|
if proc.arity == 1
proc.call(options)
else
Expand Down
3 changes: 1 addition & 2 deletions activemodel/lib/active_model/validations.rb
@@ -1,5 +1,4 @@
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/except'
Expand Down Expand Up @@ -133,7 +132,7 @@ def validate(*args, &block)
options = args.extract_options!
if options.key?(:on)
options = options.dup
options[:if] = Array.wrap(options[:if])
options[:if] = Array(options[:if])
options[:if].unshift("validation_context == :#{options[:on]}")
end
args << options
Expand Down
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/callbacks.rb
Expand Up @@ -30,7 +30,7 @@ module ClassMethods
def before_validation(*args, &block)
options = args.last
if options.is_a?(Hash) && options[:on]
options[:if] = Array.wrap(options[:if])
options[:if] = Array(options[:if])
options[:if].unshift("self.validation_context == :#{options[:on]}")
end
set_callback(:validation, :before, *args, &block)
Expand All @@ -39,7 +39,7 @@ def before_validation(*args, &block)
def after_validation(*args, &block)
options = args.extract_options!
options[:prepend] = true
options[:if] = Array.wrap(options[:if])
options[:if] = Array(options[:if])
options[:if] << "!halted"
options[:if].unshift("self.validation_context == :#{options[:on]}") if options[:on]
set_callback(:validation, :after, *(args << options), &block)
Expand Down
3 changes: 1 addition & 2 deletions activemodel/lib/active_model/validator.rb
@@ -1,4 +1,3 @@
require 'active_support/core_ext/array/wrap'
require "active_support/core_ext/module/anonymous"
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/inclusion'
Expand Down Expand Up @@ -137,7 +136,7 @@ class EachValidator < Validator
# +options+ reader, however the <tt>:attributes</tt> option will be removed
# and instead be made available through the +attributes+ reader.
def initialize(options)
@attributes = Array.wrap(options.delete(:attributes))
@attributes = Array(options.delete(:attributes))
raise ":attributes cannot be blank" if @attributes.empty?
super
check_validity!
Expand Down
Expand Up @@ -15,7 +15,7 @@ def initialize(association)

def scope
scope = klass.unscoped
scope = scope.extending(*Array.wrap(options[:extend]))
scope = scope.extending(*Array(options[:extend]))

# It's okay to just apply all these like this. The options will only be present if the
# association supports that option; this is enforced by the association builder.
Expand Down
Expand Up @@ -32,7 +32,7 @@ def writable?
private

def wrap_block_extension
options[:extend] = Array.wrap(options[:extend])
options[:extend] = Array(options[:extend])

if block_extension
silence_warnings do
Expand All @@ -51,7 +51,7 @@ def define_callback(callback_name)

# TODO : why do i need method_defined? I think its because of the inheritance chain
model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name)
model.send("#{full_callback_name}=", Array.wrap(options[callback_name.to_sym]))
model.send("#{full_callback_name}=", Array(options[callback_name.to_sym]))
end

def define_readers
Expand Down
@@ -1,5 +1,3 @@
require 'active_support/core_ext/array/wrap'

module ActiveRecord
module Associations
# = Active Record Association Collection
Expand Down Expand Up @@ -67,7 +65,7 @@ def ids_reader
# Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
def ids_writer(ids)
pk_column = reflection.primary_key_column
ids = Array.wrap(ids).reject { |id| id.blank? }
ids = Array(ids).reject { |id| id.blank? }
ids.map! { |i| pk_column.type_cast(i) }
replace(klass.find(ids).index_by { |r| r.id }.values_at(*ids))
end
Expand Down
Expand Up @@ -52,7 +52,7 @@ class CollectionProxy # :nodoc:

def initialize(association)
@association = association
Array.wrap(association.options[:extend]).each { |ext| proxy_extend(ext) }
Array(association.options[:extend]).each { |ext| proxy_extend(ext) }
end

alias_method :new, :build
Expand Down
2 changes: 0 additions & 2 deletions activerecord/lib/active_record/autosave_association.rb
@@ -1,5 +1,3 @@
require 'active_support/core_ext/array/wrap'

module ActiveRecord
# = Active Record Autosave Association
#
Expand Down
2 changes: 0 additions & 2 deletions activerecord/lib/active_record/callbacks.rb
@@ -1,5 +1,3 @@
require 'active_support/core_ext/array/wrap'

module ActiveRecord
# = Active Record Callbacks
#
Expand Down
@@ -1,4 +1,3 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/deprecation/reporting'

module ActiveRecord
Expand Down Expand Up @@ -42,7 +41,7 @@ def table_exists?(table_name)
# # Check an index with a custom name exists
# index_exists?(:suppliers, :company_id, :name => "idx_company_id"
def index_exists?(table_name, column_name, options = {})
column_names = Array.wrap(column_name)
column_names = Array(column_name)
index_name = options.key?(:name) ? options[:name].to_s : index_name(table_name, :column => column_names)
if options[:unique]
indexes(table_name).any?{ |i| i.unique && i.name == index_name }
Expand Down Expand Up @@ -377,7 +376,7 @@ def rename_index(table_name, old_name, new_name)
def index_name(table_name, options) #:nodoc:
if Hash === options # legacy support
if options[:column]
"index_#{table_name}_on_#{Array.wrap(options[:column]) * '_and_'}"
"index_#{table_name}_on_#{Array(options[:column]) * '_and_'}"
elsif options[:name]
options[:name]
else
Expand Down Expand Up @@ -436,7 +435,7 @@ def initialize_schema_migrations_table
end

def assume_migrated_upto_version(version, migrations_paths = ActiveRecord::Migrator.migrations_paths)
migrations_paths = Array.wrap(migrations_paths)
migrations_paths = Array(migrations_paths)
version = version.to_i
sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)

Expand Down Expand Up @@ -551,7 +550,7 @@ def options_include_default?(options)
end

def add_index_options(table_name, column_name, options = {})
column_names = Array.wrap(column_name)
column_names = Array(column_name)
index_name = index_name(table_name, :column => column_names)

if Hash === options # legacy support, since this param was a string
Expand Down
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/fixtures.rb
Expand Up @@ -2,7 +2,6 @@
require 'yaml'
require 'zlib'
require 'active_support/dependencies'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/blank'
require 'active_support/ordered_hash'
require 'active_record/fixtures/file'
Expand Down Expand Up @@ -783,7 +782,7 @@ def require_fixture_classes(fixture_names = nil)
end

def setup_fixture_accessors(fixture_names = nil)
fixture_names = Array.wrap(fixture_names || fixture_table_names)
fixture_names = Array(fixture_names || fixture_table_names)
methods = Module.new do
fixture_names.each do |fixture_name|
fixture_name = fixture_name.to_s
Expand Down
5 changes: 2 additions & 3 deletions activerecord/lib/active_record/migration.rb
@@ -1,6 +1,5 @@
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/class/attribute_accessors"
require "active_support/core_ext/array/wrap"

module ActiveRecord
# Exception that can be raised to stop migrations from going backwards.
Expand Down Expand Up @@ -587,15 +586,15 @@ def proper_table_name(name)
def migrations_paths
@migrations_paths ||= ['db/migrate']
# just to not break things if someone uses: migration_path = some_string
Array.wrap(@migrations_paths)
Array(@migrations_paths)
end

def migrations_path
migrations_paths.first
end

def migrations(paths, subdirectories = true)
paths = Array.wrap(paths)
paths = Array(paths)

glob = subdirectories ? "**/" : ""
files = Dir[*paths.map { |p| "#{p}/#{glob}[0-9]*_*.rb" }]
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/serialization.rb
Expand Up @@ -7,8 +7,8 @@ module Serialization
def serializable_hash(options = nil)
options = options.try(:clone) || {}

options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }
options[:except] |= Array.wrap(self.class.inheritance_column)
options[:except] = Array(options[:except]).map { |n| n.to_s }
options[:except] |= Array(self.class.inheritance_column)

super(options)
end
Expand Down
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/serializers/xml_serializer.rb
@@ -1,4 +1,3 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/hash/conversions'

module ActiveRecord #:nodoc:
Expand Down Expand Up @@ -179,7 +178,7 @@ def to_xml(options = {}, &block)
class XmlSerializer < ActiveModel::Serializers::Xml::Serializer #:nodoc:
def initialize(*args)
super
options[:except] = Array.wrap(options[:except]) | Array.wrap(@serializable.class.inheritance_column)
options[:except] = Array(options[:except]) | Array(@serializable.class.inheritance_column)
end

class Attribute < ActiveModel::Serializers::Xml::Serializer::Attribute #:nodoc:
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/transactions.rb
Expand Up @@ -211,7 +211,7 @@ def transaction(options = {}, &block)
def after_commit(*args, &block)
options = args.last
if options.is_a?(Hash) && options[:on]
options[:if] = Array.wrap(options[:if])
options[:if] = Array(options[:if])
options[:if] << "transaction_include_action?(:#{options[:on]})"
end
set_callback(:commit, :after, *args, &block)
Expand All @@ -220,7 +220,7 @@ def after_commit(*args, &block)
def after_rollback(*args, &block)
options = args.last
if options.is_a?(Hash) && options[:on]
options[:if] = Array.wrap(options[:if])
options[:if] = Array(options[:if])
options[:if] << "transaction_include_action?(:#{options[:on]})"
end
set_callback(:rollback, :after, *args, &block)
Expand Down
6 changes: 2 additions & 4 deletions activerecord/lib/active_record/validations/uniqueness.rb
@@ -1,5 +1,3 @@
require 'active_support/core_ext/array/wrap'

module ActiveRecord
module Validations
class UniquenessValidator < ActiveModel::EachValidator
Expand All @@ -25,7 +23,7 @@ def validate_each(record, attribute, value)
relation = build_relation(finder_class, table, attribute, value)
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?

Array.wrap(options[:scope]).each do |scope_item|
Array(options[:scope]).each do |scope_item|
scope_value = record.send(scope_item)
relation = relation.and(table[scope_item].eq(scope_value))
end
Expand Down Expand Up @@ -81,7 +79,7 @@ module ClassMethods
#
# class Person < ActiveRecord::Base
# validates_uniqueness_of :user_name, :scope => :account_id
# end
# end
#
# Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once
# per semester for a particular class.
Expand Down
15 changes: 7 additions & 8 deletions activesupport/lib/active_support/callbacks.rb
@@ -1,6 +1,5 @@
require 'active_support/concern'
require 'active_support/descendants_tracker'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/kernel/reporting'
require 'active_support/core_ext/kernel/singleton_class'
Expand Down Expand Up @@ -121,12 +120,12 @@ def clone(chain, klass)
end

def normalize_options!(options)
options[:if] = Array.wrap(options[:if])
options[:unless] = Array.wrap(options[:unless])
options[:if] = Array(options[:if])
options[:unless] = Array(options[:unless])

options[:per_key] ||= {}
options[:per_key][:if] = Array.wrap(options[:per_key][:if])
options[:per_key][:unless] = Array.wrap(options[:per_key][:unless])
options[:per_key][:if] = Array(options[:per_key][:if])
options[:per_key][:unless] = Array(options[:per_key][:unless])
end

def name
Expand Down Expand Up @@ -246,11 +245,11 @@ def _compile_options(options)
conditions = ["true"]

unless options[:if].empty?
conditions << Array.wrap(_compile_filter(options[:if]))
conditions << Array(_compile_filter(options[:if]))
end

unless options[:unless].empty?
conditions << Array.wrap(_compile_filter(options[:unless])).map {|f| "!#{f}"}
conditions << Array(_compile_filter(options[:unless])).map {|f| "!#{f}"}
end

conditions.flatten.join(" && ")
Expand Down Expand Up @@ -295,7 +294,7 @@ def _compile_filter(filter)
@klass.send(:define_method, "#{method_name}_object") { filter }

_normalize_legacy_filter(kind, filter)
scopes = Array.wrap(chain.config[:scope])
scopes = Array(chain.config[:scope])
method_to_call = scopes.map{ |s| s.is_a?(Symbol) ? send(s) : s }.join("_")

@klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
Expand Down

0 comments on commit 75a630e

Please sign in to comment.