Class: Array

Inherits:
Object show all
Defined in:
lib/socializer/scraper/extensions.rb

Instance Method Summary collapse

Instance Method Details

#accumulateObject



14
15
16
# File 'lib/socializer/scraper/extensions.rb', line 14

def accumulate
  flatten.compact.uniq
end

#collect_as_hashObject

Raises:

  • (StandardError)


7
8
9
10
11
12
# File 'lib/socializer/scraper/extensions.rb', line 7

def collect_as_hash
  raise StandardError, "Array is not a hash collection!" unless hash_collection?
  flatten.compact.each_with_object(Hash.new([])) do |h1,h|
    h1.each{|k,v| h[k] = (h[k] | [v]).accumulate }
  end
end

#extract_options!Object



22
23
24
# File 'lib/socializer/scraper/extensions.rb', line 22

def extract_options!
  last.is_a?(Hash) && last.instance_of?(Hash) ? pop : {}
end

#hash_collection?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/socializer/scraper/extensions.rb', line 3

def hash_collection?
  flatten.compact.reject{|v| v.is_a?(Hash)}.empty?
end

#hashify_or_collectObject



18
19
20
# File 'lib/socializer/scraper/extensions.rb', line 18

def hashify_or_collect
  hash_collection? ? collect_as_hash : accumulate
end