Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. class GenerateSendGridHeaders
  2. # (uses the attr_extras gem)
  3. method_object :call,
  4. [ :mailer!, :action!, :args!, :env ]
  5.  
  6. def call
  7. {
  8. category: [ mailer_name, "#{mailer_name}##{action}" ],
  9. unique_args: {
  10. environment: env,
  11. arguments: args_hash.to_json,
  12. },
  13. }
  14. end
  15.  
  16. private
  17.  
  18. def args_hash
  19. method_params = mailer.method(action).parameters
  20. param_names = method_params.map(&:last)
  21.  
  22. rest_param_count = method_params.count { |(type, _)| type == :rest }
  23. raise "Only handles a single splat!" if rest_param_count > 1
  24.  
  25. non_rest_param_count = method_params.length - rest_param_count
  26. non_rest_param_names = param_names.first(non_rest_param_count)
  27. non_rest_values = args.first(non_rest_param_count)
  28.  
  29. # NOTE: This assumes the splat is always the last argument.
  30. # That is likely but not guaranteed.
  31. rest_values = args.from(non_rest_param_count)
  32.  
  33. hash = Hash[ non_rest_param_names.zip(non_rest_values) ]
  34.  
  35. if rest_values.any?
  36. rest_param_name = param_names.last
  37. hash.merge!(rest_param_name => rest_values)
  38. end
  39.  
  40. hash
  41. end
  42.  
  43. def mailer_name
  44. mailer.class.name
  45. end
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement