Permalink
Browse files

Rename parallel_map to pmap

  • Loading branch information...
1 parent d26fb7c commit 302a9d8a600b31cafb592074b106f4aac5fc4d5a @sferik committed Mar 1, 2014
@@ -4,7 +4,7 @@ module Enumerable
# @return [Enumerator]
def each(start = 0, &block)
- return to_enum(:each) unless block_given?
+ return to_enum(:each, start) unless block_given?
Array(@collection[start..-1]).each do |element|
yield element
end
@@ -90,7 +90,7 @@ def direct_messages(*args)
if arguments.empty?
direct_messages_received(arguments.options)
else
- parallel_map(arguments) do |id|
+ pmap(arguments) do |id|
direct_message(id, arguments.options)
end
end
@@ -68,7 +68,7 @@ def unfavorite(*args)
# @param options [Hash] A customizable set of options.
def favorite(*args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |tweet|
+ pmap(arguments) do |tweet|
begin
perform_with_object(:post, '/1.1/favorites/create.json', arguments.options.merge(:id => extract_id(tweet)), Twitter::Tweet)
rescue Twitter::Error::AlreadyFavorited
@@ -95,7 +95,7 @@ def favorite(*args)
# @param options [Hash] A customizable set of options.
def favorite!(*args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |tweet|
+ pmap(arguments) do |tweet|
perform_with_object(:post, '/1.1/favorites/create.json', arguments.options.merge(:id => extract_id(tweet)), Twitter::Tweet)
end
end
@@ -131,7 +131,7 @@ def follow(*args)
# @option options [Boolean] :follow (false) Enable notifications for the target user.
def follow!(*args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |user|
+ pmap(arguments) do |user|
perform_with_object(:post, '/1.1/friendships/create.json', merge_user(arguments.options, user), Twitter::User)
end.compact
end
@@ -438,7 +438,7 @@ def list_from_response_with_users(request_method, path, args)
members = arguments.pop
merge_list!(options, arguments.pop)
merge_owner!(options, arguments.pop)
- parallel_map(members.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users|
+ pmap(members.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users|
perform_with_object(request_method, path, merge_users(options, users), Twitter::List)
end.last
end
@@ -35,7 +35,7 @@ def saved_searches(*args)
if arguments.empty?
perform_with_objects(:get, '/1.1/saved_searches/list.json', arguments.options, Twitter::SavedSearch)
else
- parallel_map(arguments) do |id|
+ pmap(arguments) do |id|
saved_search(id, arguments.options)
end
end
@@ -83,7 +83,7 @@ def create_saved_search(query, options = {})
# @param options [Hash] A customizable set of options.
def destroy_saved_search(*args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |id|
+ pmap(arguments) do |id|
perform_with_object(:post, "/1.1/saved_searches/destroy/#{id}.json", arguments.options, Twitter::SavedSearch)
end
end
@@ -163,7 +163,7 @@ def update!(status, options = {})
# @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
def retweet(*args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |tweet|
+ pmap(arguments) do |tweet|
begin
post_retweet(extract_id(tweet), arguments.options)
rescue Twitter::Error::AlreadyRetweeted
@@ -188,7 +188,7 @@ def retweet(*args)
# @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
def retweet!(*args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |tweet|
+ pmap(arguments) do |tweet|
post_retweet(extract_id(tweet), arguments.options)
end.compact
end
@@ -262,7 +262,7 @@ def oembed(tweet, options = {})
# @option options [String] :lang Language code for the rendered embed. This will affect the text and localization of the rendered HTML.
def oembeds(*args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |tweet|
+ pmap(arguments) do |tweet|
oembed(extract_id(tweet), arguments.options)
end
end
@@ -293,7 +293,7 @@ def retweeters_ids(*args)
# @return [Array<Twitter::Tweet>]
def parallel_tweets_from_response(request_method, path, args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |tweet|
+ pmap(arguments) do |tweet|
perform_with_object(request_method, path + "/#{extract_id(tweet)}.json", arguments.options, Twitter::Tweet)
end
end
@@ -227,7 +227,7 @@ def unblock(*args)
def users(*args)
arguments = Twitter::Arguments.new(args)
request_method = arguments.options.delete(:method) || :post
- parallel_map(arguments.each_slice(MAX_USERS_PER_REQUEST)) do |users|
+ pmap(arguments.each_slice(MAX_USERS_PER_REQUEST)) do |users|
perform_with_objects(request_method, '/1.1/users/lookup.json', merge_users(arguments.options, users), Twitter::User)
end.flatten
end
@@ -64,7 +64,7 @@ def perform_with_cursor(request_method, path, options, collection_name, klass =
# @return [Array<Twitter::User>]
def parallel_user_objects_from_response(request_method, path, args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |user|
+ pmap(arguments) do |user|
perform_with_object(request_method, path, merge_user(arguments.options, user), Twitter::User)
end
end
@@ -97,7 +97,7 @@ def objects_from_response_with_user(klass, request_method, path, args)
# @return [Array]
def parallel_objects_from_response(klass, request_method, path, args)
arguments = Twitter::Arguments.new(args)
- parallel_map(arguments) do |object|
+ pmap(arguments) do |object|
perform_with_object(request_method, path, arguments.options.merge(:id => extract_id(object)), klass)
end
end
View
@@ -15,14 +15,20 @@ def deprecate_alias(new_name, old_name)
end
end
- def parallel_map(enumerable)
+ # Returns a new array with the results of running block once for every element in enumerable.
+ # If no block is given, an enumerator is returned instead.
+ #
+ # @param enumerable [Enumerable]
+ # @return [Array, Enumerator]
+ def pmap(enumerable)
+ return to_enum(:pmap, enumerable) unless block_given?
# Don't bother spawning a new thread if there's only one item
if enumerable.count == 1
enumerable.collect { |object| yield object }
else
enumerable.collect { |object| Thread.new { yield object } }.collect(&:value)
end
end
- module_function :parallel_map
+ module_function :pmap
end
end

0 comments on commit 302a9d8

Please sign in to comment.