Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Anonymizer
- def anonymize_attribute(attribute)
- self.send(:include, Module.new do
- define_method attribute do
- a = @attributes[attribute.to_s]
- a && attribute.to_s.ends_with?("_id") ? a.to_i : a
- end
- define_method "#{attribute}_with_anonymous" do
- send("#{attribute}_without_anonymous") unless anonymous?
- end
- end)
- alias_method_chain attribute, :anonymous
- end
- def anonymize_method(method, options = {})
- method_name, ending = split_method_name(method)
- self.send(:include, Module.new do
- if options[:superclass]
- define_method method do |*arguments|
- super(*arguments)
- end
- end
- define_method "#{method_name}_with_anonymous#{ending}" do |*arguments|
- send("#{method_name}_without_anonymous#{ending}", arguments) unless anonymous?
- end
- end)
- alias_method_chain method, :anonymous
- end
- private
- def split_method_name(method)
- if ['?', '!'].include?(method.to_s.last)
- [method.to_s[0..-2], method.to_s.last]
- else
- [method, nil]
- end
- end
- end
Add Comment
Please, Sign In to add comment