Skip to content

Commit

Permalink
Replaces deprecated Proc.new usage
Browse files Browse the repository at this point in the history
“warning: Capturing the given block using Proc.new is deprecated; use `&block` instead”
  • Loading branch information
hspindell authored and apersaud committed May 4, 2021
1 parent 23bf69c commit 946c311
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 43 deletions.
8 changes: 4 additions & 4 deletions lib/parse/client.rb
Expand Up @@ -202,8 +202,8 @@ def client(conn = :default)
# @see Parse::Middleware::Caching
# @see Parse::Middleware::Authentication
# @see Parse::Protocol
def setup(opts = {})
@clients[:default] = self.new(opts, &Proc.new)
def setup(opts = {}, &block)
@clients[:default] = self.new(opts, &block)
end
end

Expand Down Expand Up @@ -579,9 +579,9 @@ def client
# @yield (see Parse::Client.setup)
# @return (see Parse::Client.setup)
# @see Parse::Client.setup
def self.setup(opts = {})
def self.setup(opts = {}, &block)
if block_given?
Parse::Client.new(opts, &Proc.new)
Parse::Client.new(opts, &block)
else
Parse::Client.new(opts)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/parse/client/batch.rb
Expand Up @@ -85,9 +85,9 @@ def change_requests
end

# @return [Array]
def each
def each(&block)
return enum_for(:each) unless block_given?
@requests.each(&Proc.new)
@requests.each(&block)
end

# @return [Hash] a formatted payload for the batch request.
Expand Down Expand Up @@ -125,15 +125,15 @@ def error?
# @param segment [Integer] the number of requests to send in each batch. Default 50.
# @return [Array<Parse::Response>] the corresponding set of responses for
# each request in the batch.
def submit(segment = 50)
def submit(segment = 50, &block)
@responses = []
@requests.uniq!(&:signature)
@responses = @requests.each_slice(segment).to_a.threaded_map(2) do |slice|
client.batch_request(BatchOperation.new(slice))
end
@responses.flatten!
#puts "Requests: #{@requests.count} == Response: #{@responses.count}"
@requests.zip(@responses).each(&Proc.new) if block_given?
@requests.zip(@responses).each(&block) if block_given?
@responses
end

Expand Down
4 changes: 2 additions & 2 deletions lib/parse/client/response.rb
Expand Up @@ -156,9 +156,9 @@ def first

# Iterate through each result item.
# @yieldparam [Object] a result entry.
def each
def each(&block)
return enum_for(:each) unless block_given?
results.each(&Proc.new)
results.each(&block)
self
end

Expand Down
20 changes: 10 additions & 10 deletions lib/parse/model/associations/collection_proxy.rb
Expand Up @@ -328,33 +328,33 @@ def notify_will_change!
end

# Alias for Array#each
def each
def each(&block)
return collection.enum_for(:each) unless block_given?
collection.each &Proc.new
collection.each(&block)
end

# Alias for Array#map
def map
def map(&block)
return collection.enum_for(:map) unless block_given?
collection.map &Proc.new
collection.map(&block)
end

# Alias for Array#select
def select
def select(&block)
return collection.enum_for(:select) unless block_given?
collection.select &Proc.new
collection.select(&block)
end

# Alias for Array#uniq
def uniq
return collection.uniq(&Proc.new) if block_given?
def uniq(&block)
return collection.uniq(&block) if block_given?
return collection.uniq
end

# Alias for Array#uniq!
def uniq!
def uniq!(&block)
notify_will_change!
return collection.uniq!(&Proc.new) if block_given?
return collection.uniq!(&block) if block_given?
return collection.uniq!
end

Expand Down
4 changes: 2 additions & 2 deletions lib/parse/model/associations/relation_collection_proxy.rb
Expand Up @@ -53,11 +53,11 @@ def initialize(collection = nil, delegate: nil, key: nil, parse_class: nil)

# You can get items within the collection relation filtered by a specific set
# of query constraints.
def all(constraints = {})
def all(constraints = {}, &block)
q = query({ limit: :max }.merge(constraints))
if block_given?
# if we have a query, then use the Proc with it (more efficient)
return q.present? ? q.results(&Proc.new) : collection.each(&Proc.new)
return q.present? ? q.results(&block) : collection.each(&block)
end
# if no block given, get all the results
q.present? ? q.results : collection
Expand Down
8 changes: 4 additions & 4 deletions lib/parse/model/core/actions.rb
Expand Up @@ -15,9 +15,9 @@ class Query

# Supporting the `all` class method to be used in scope chaining with queries.
# @!visibility private
def all(expressions = { limit: :max })
def all(expressions = { limit: :max }, &block)
conditions(expressions)
return results(&Proc.new) if block_given?
return results(&block) if block_given?
results
end

Expand All @@ -35,15 +35,15 @@ def first_or_create(query_attrs = {}, resource_attrs = {})

# Supporting the `save_all` method to be used in scope chaining with queries.
# @!visibility private
def save_all(expressions = {})
def save_all(expressions = {}, &block)
conditions(expressions)
klass = Parse::Model.find_class self.table
if klass.blank?
raise ArgumentError, "Parse model with class name #{self.table} is not registered."
end
hash_constraints = constraints(true)

klass.save_all(hash_constraints, &Proc.new) if block_given?
klass.save_all(hash_constraints, &block) if block_given?
klass.save_all(hash_constraints)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/parse/model/core/querying.rb
Expand Up @@ -207,10 +207,10 @@ def each(constraints = {}, &block)
# by the server.
# @return [Array<Parse::Object>] an array of matching objects. If a block is passed,
# an empty array is returned.
def all(constraints = { limit: :max })
def all(constraints = { limit: :max }, &block)
constraints = constraints.reverse_merge({ limit: :max })
prepared_query = query(constraints)
return prepared_query.results(&Proc.new) if block_given?
return prepared_query.results(&block) if block_given?
prepared_query.results
end

Expand Down
26 changes: 13 additions & 13 deletions lib/parse/query.rb
Expand Up @@ -662,25 +662,25 @@ def count
# @yield a block yield for each object in the result
# @return [Array]
# @see Array#each
def each
def each(&block)
return results.enum_for(:each) unless block_given? # Sparkling magic!
results.each(&Proc.new)
results.each(&block)
end

# @yield a block yield for each object in the result
# @return [Array]
# @see Array#map
def map
def map(&block)
return results.enum_for(:map) unless block_given? # Sparkling magic!
results.map(&Proc.new)
results.map(&block)
end

# @yield a block yield for each object in the result
# @return [Array]
# @see Array#select
def select
def select(&block)
return results.enum_for(:select) unless block_given? # Sparkling magic!
results.select(&Proc.new)
results.select(&block)
end

# @return [Array]
Expand All @@ -700,7 +700,7 @@ def first(limit = 1)
# max_results is used to iterate through as many API requests as possible using
# :skip and :limit paramter.
# @!visibility private
def max_results(raw: false, on_batch: nil, discard_results: false)
def max_results(raw: false, on_batch: nil, discard_results: false, &block)
compiled_query = compile
batch_size = 1_000
results = []
Expand All @@ -725,7 +725,7 @@ def max_results(raw: false, on_batch: nil, discard_results: false)
items = decode(items) unless raw
# if a block is provided, we do not keep the results after processing.
if block_given?
items.each(&Proc.new)
items.each(&block)
else
# concat results unless discard_results is true
results += items unless discard_results
Expand Down Expand Up @@ -796,15 +796,15 @@ def fetch!(compiled_query)
# @yield a block to iterate for each object that matched the query.
# @return [Array<Hash>] if raw is set to true, a set of Parse JSON hashes.
# @return [Array<Parse::Object>] if raw is set to false, a list of matching Parse::Object subclasses.
def results(raw: false)
def results(raw: false, &block)
if @results.nil?
if block_given?
max_results(raw: raw, &Proc.new)
max_results(raw: raw, &block)
elsif @limit.is_a?(Numeric)
response = fetch!(compile)
return [] if response.error?
items = raw ? response.results : decode(response.results)
return items.each(&Proc.new) if block_given?
return items.each(&block) if block_given?
@results = items
else
@results = max_results(raw: raw)
Expand All @@ -822,9 +822,9 @@ def results(raw: false)
# @return [Array<Hash>] if raw is set to true, a set of Parse JSON hashes.
# @return [Array<Parse::Object>] if raw is set to false, a list of matching Parse::Object subclasses.
# @see #results
def all(expressions = { limit: :max })
def all(expressions = { limit: :max }, &block)
conditions(expressions)
return results(&Proc.new) if block_given?
return results(&block) if block_given?
results
end

Expand Down
3 changes: 1 addition & 2 deletions lib/parse/webhooks.rb
Expand Up @@ -125,13 +125,12 @@ def routes
# name to register with Parse server.
# @yield the block that will handle of the webhook trigger or function.
# @return (see routes)
def route(type, className, block = nil)
def route(type, className, &block)
type = type.to_s.underscore.to_sym #support camelcase
if type != :function && className.respond_to?(:parse_class)
className = className.parse_class
end
className = className.to_s
block = Proc.new if block_given?
if routes[type].nil? || block.respond_to?(:call) == false
raise ArgumentError, "Invalid Webhook registration trigger #{type} #{className}"
end
Expand Down

0 comments on commit 946c311

Please sign in to comment.