Module: MandrillQueue::Message::Recipient::Helpers

Defined in:
lib/mandrill_queue/message/recipient/helpers.rb

Class Method Summary collapse

Class Method Details

.objects_to_hashes(list, fields = nil, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mandrill_queue/message/recipient/helpers.rb', line 5

def self.objects_to_hashes(list, fields = nil, options = {})
	try_methods = options[:try_methods] || [:attributes, :to_hash, :to_h]
	hashes = []
	list.each do |obj|
		hashes << if obj.is_a?(Hash)
			fields.nil? ? obj.dup : obj.select { |o| fields.include?(o) }
		elsif fields.nil?
			meth = try_methods.find { |m| obj.respond_to?(m) }
			# Add object result or nil
			unless meth.nil?
				obj.send(meth)
			end
		else
			hash = {}
			fields.each do |f|
				hash[f] = obj.send(f)
			end
			hash
		end
	end
	hashes
end