ruby on rails - Can I get a symbol for a member from an argument name? -


i validating child objects of parent this:

validate :overdue_tasks_valid validate :created_overdue_tasks_valid 

i have methods this:

private def overdue_tasks_valid   validate_notification(overdue_tasks, :overdue_tasks) end  def created_overdue_tasks_valid   validate_notification(created_overdue_tasks, :created_overdue_tasks) end  def validate_notification(notification, key)   return if notification.valid?    notification.errors.full_messages.each |message|     errors.add key, message   end end 

what want know, is, can pass 1 argument validate_notfication , somehow determine symbol in method?

so validate notification called this:

validate_notification(overdue_tasks) 

and method this:

def validate_notification(notification)   return if notification.valid?    notification.errors.full_messages.each |message|     errors.add [somehow symbol], message   end end 

no, can send symbol instead:

def overdue_tasks_valid   validate_notification :overdue_tasks end  def created_overdue_tasks_valid   validate_notification :created_overdue_tasks end  def validate_notification(method)   notification = send method   return if notification.valid?    notification.errors.full_messages.each |message|     errors.add method, message   end end 

the secret sauce is, of course, object#send.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -