Guest User

Untitled

a guest
Feb 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. module Anonymizer
  2. def anonymize_attribute(attribute)
  3. self.send(:include, Module.new do
  4. define_method attribute do
  5. a = @attributes[attribute.to_s]
  6. a && attribute.to_s.ends_with?("_id") ? a.to_i : a
  7. end
  8. define_method "#{attribute}_with_anonymous" do
  9. send("#{attribute}_without_anonymous") unless anonymous?
  10. end
  11. end)
  12. alias_method_chain attribute, :anonymous
  13. end
  14.  
  15. def anonymize_method(method, options = {})
  16. method_name, ending = split_method_name(method)
  17. self.send(:include, Module.new do
  18. if options[:superclass]
  19. define_method method do |*arguments|
  20. super(*arguments)
  21. end
  22. end
  23. define_method "#{method_name}_with_anonymous#{ending}" do |*arguments|
  24. send("#{method_name}_without_anonymous#{ending}", arguments) unless anonymous?
  25. end
  26. end)
  27. alias_method_chain method, :anonymous
  28. end
  29.  
  30. private
  31. def split_method_name(method)
  32. if ['?', '!'].include?(method.to_s.last)
  33. [method.to_s[0..-2], method.to_s.last]
  34. else
  35. [method, nil]
  36. end
  37. end
  38. end
Add Comment
Please, Sign In to add comment