diff --git a/lib/parse/client.rb b/lib/parse/client.rb index 167cc9d..4384dab 100644 --- a/lib/parse/client.rb +++ b/lib/parse/client.rb @@ -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 @@ -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 diff --git a/lib/parse/client/batch.rb b/lib/parse/client/batch.rb index de696e5..7c04132 100644 --- a/lib/parse/client/batch.rb +++ b/lib/parse/client/batch.rb @@ -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. @@ -125,7 +125,7 @@ def error? # @param segment [Integer] the number of requests to send in each batch. Default 50. # @return [Array] 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| @@ -133,7 +133,7 @@ def submit(segment = 50) 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 diff --git a/lib/parse/client/response.rb b/lib/parse/client/response.rb index 1f6a3ee..161f005 100644 --- a/lib/parse/client/response.rb +++ b/lib/parse/client/response.rb @@ -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 diff --git a/lib/parse/model/associations/collection_proxy.rb b/lib/parse/model/associations/collection_proxy.rb index 6b56529..561614a 100644 --- a/lib/parse/model/associations/collection_proxy.rb +++ b/lib/parse/model/associations/collection_proxy.rb @@ -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 diff --git a/lib/parse/model/associations/relation_collection_proxy.rb b/lib/parse/model/associations/relation_collection_proxy.rb index 41d31c2..2fb719c 100644 --- a/lib/parse/model/associations/relation_collection_proxy.rb +++ b/lib/parse/model/associations/relation_collection_proxy.rb @@ -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 diff --git a/lib/parse/model/core/actions.rb b/lib/parse/model/core/actions.rb index 8b62384..6840f96 100644 --- a/lib/parse/model/core/actions.rb +++ b/lib/parse/model/core/actions.rb @@ -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 @@ -35,7 +35,7 @@ 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? @@ -43,7 +43,7 @@ def save_all(expressions = {}) 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 diff --git a/lib/parse/model/core/querying.rb b/lib/parse/model/core/querying.rb index 56598b1..502ff62 100644 --- a/lib/parse/model/core/querying.rb +++ b/lib/parse/model/core/querying.rb @@ -207,10 +207,10 @@ def each(constraints = {}, &block) # by the server. # @return [Array] 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 diff --git a/lib/parse/query.rb b/lib/parse/query.rb index d53e52e..a27b4cc 100644 --- a/lib/parse/query.rb +++ b/lib/parse/query.rb @@ -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] @@ -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 = [] @@ -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 @@ -796,15 +796,15 @@ def fetch!(compiled_query) # @yield a block to iterate for each object that matched the query. # @return [Array] if raw is set to true, a set of Parse JSON hashes. # @return [Array] 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) @@ -822,9 +822,9 @@ def results(raw: false) # @return [Array] if raw is set to true, a set of Parse JSON hashes. # @return [Array] 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 diff --git a/lib/parse/webhooks.rb b/lib/parse/webhooks.rb index 8bb2a4d..23724ee 100644 --- a/lib/parse/webhooks.rb +++ b/lib/parse/webhooks.rb @@ -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