Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter/core_ext/hash.rb

Direct Known Subclasses

Twitter::IdentityMap

Instance Method Summary collapse

Instance Method Details

#except(*keys) ⇒ Hash

Return a hash that includes everything but the given keys.

Parameters:

Returns:



7
8
9
# File 'lib/twitter/core_ext/hash.rb', line 7

def except(*keys)
  self.dup.except!(*keys)
end

#except!(*keys) ⇒ Hash

Replaces the hash without the given keys.

Parameters:

Returns:



15
16
17
18
# File 'lib/twitter/core_ext/hash.rb', line 15

def except!(*keys)
  keys.each{|key| delete(key)}
  self
end

#merge_list!(list) ⇒ Hash

Take a list and merge it into the hash with the correct key

Parameters:

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/twitter/core_ext/hash.rb', line 24

def merge_list!(list)
  case list
  when Integer
    self[:list_id] = list
  when String
    self[:slug] = list
  when Twitter::List
    self[:list_id] = list.id
    self.merge_owner!(list.user)
  end
  self
end

#merge_owner!(user) ⇒ Hash

Take an owner and merge it into the hash with the correct key

Parameters:

Returns:



41
42
43
44
45
# File 'lib/twitter/core_ext/hash.rb', line 41

def merge_owner!(user)
  self.merge_user!(user, "owner")
  self[:owner_id] = self.delete(:owner_user_id) unless self[:owner_user_id].nil?
  self
end

#merge_user(user, prefix = nil) ⇒ Hash

Take a user and merge it into the hash with the correct key

Parameters:

Returns:



51
52
53
# File 'lib/twitter/core_ext/hash.rb', line 51

def merge_user(user, prefix=nil)
  self.dup.merge_user!(user, prefix)
end

#merge_user!(user, prefix = nil) ⇒ Hash

Take a user and merge it into the hash with the correct key

Parameters:

Returns:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/twitter/core_ext/hash.rb', line 59

def merge_user!(user, prefix=nil)
  case user
  when Integer
    self[[prefix, "user_id"].compact.join("_").to_sym] = user
  when String
    self[[prefix, "screen_name"].compact.join("_").to_sym] = user
  when Twitter::User
    self[[prefix, "user_id"].compact.join("_").to_sym] = user.id
  end
  self
end

#merge_users(*users) ⇒ Hash

Take a multiple users and merge them into the hash with the correct keys

Parameters:

Returns:



75
76
77
# File 'lib/twitter/core_ext/hash.rb', line 75

def merge_users(*users)
  self.dup.merge_users!(*users)
end

#merge_users!(*users) ⇒ Hash

Take a multiple users and merge them into the hash with the correct keys

Parameters:

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/twitter/core_ext/hash.rb', line 83

def merge_users!(*users)
  user_ids, screen_names = [], []
  users.flatten.each do |user|
    case user
    when Integer
      user_ids << user
    when String
      screen_names << user
    when Twitter::User
      user_ids << user.id
    end
  end
  self[:user_id] = user_ids.join(',') unless user_ids.empty?
  self[:screen_name] = screen_names.join(',') unless screen_names.empty?
  self
end