Module: ResqueSafe::ConvertToHash

Included in:
ResqueSafe
Defined in:
lib/resque_safe/convert_to_hash.rb

Instance Method Summary collapse

Instance Method Details

#translate_to_hash(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/resque_safe/convert_to_hash.rb', line 3

def translate_to_hash(*args)
  th = {}

  args = args.flatten if args.respond_to?(:flatten)

  args.each do |arg|
    next if arg.blank?
    arg_class = arg.class.name
    if arg.is_a?(Hash)
      arg.each do|k,v|
        if v.respond_to?(:gsub)
          th[k.to_sym] = CGI.escapeHTML(v)
        else
          th[k.to_sym] = v
        end
      end
    elsif known_models.include?(arg_class) || arg.class.respond_to?(:column_names)
      th[arg_class.underscore.to_sym] = arg.id
    else
      raise ArgumentError.new("Unknown #{arg_class} passed to resque safe. \n Object #{arg.inspect} \n\n Args #{args} \n\n Known Models #{known_models}")
    end
  end
  th
end