How do I turn something into a Ruby Proc? -
if j.job_type_name j.type = j.job_type_name elsif j.type.length > 1 # jty = jobtype.find_by_id(j.type) # don't want relentlessly hit db jobtypes, colors, priorities, etc. use stored 'all' variable jty = alljobtypes.find { |h| h['_id'] == bson::objectid(j.type) } j.type = jty && jty.job_type ? jty.job_type : 'n/a' end if j.priority_name j.priority = j.priority_name elsif j.priority pri = jobpriority.find_by_id(j.priority) j.priority = !pri.blank? && !pri.job_priority.blank? ? pri.job_priority : 'n/a' end
i have these 2 if else blocks , think better off procs, i'm still wrapping head around proc is. it's piece of code want call often, , put in 1 place have update in 1 place. it's not method on object per-se, repetitious code.
so what's syntax above?
the code don't see (where 'j' set) .each loop on database cursor.
so job.where(stuff).each |j|
etc.
the syntax making proc just:
my_proc = proc |arglist| # code here end
you can use proc.new
instead of proc
if want.
you should able make proc , pass in block .each, (but don't see you):
def process_jobs(stuff) process_job = proc |j| if j.job_type_name #... end end job.where(stuff).each(&process_job) end
Comments
Post a Comment