2016-04-07 4 views
1

Kann mir jemand sagen, was hier los ist:einen Lambda im Wert Teil eines Hash-Speicherung

value: -> f { view_context.fr_user_column(f) } 

Dies ist aus einem größeren hash:

def self.columns 
{ 
    user: { 
    title: "Applicant", 
    value: -> f { view_context.fr_user_column(f) } 
    }, 
    ch_rep: { 
    title: "CH Rep", 
    value: -> f { view_context.fr_ch_rep_column(f) } 
    } 
} 

, die in einem Verfahren verwendet werden, um erstellen Sie eine Tabelle:

def self.render_zable_columns(context, options = {}) 
self.columns.each do |key, col| 
    next if options[:except] && options[:except].include?(key) 
    col_options = {title: col[:title]} 
    col_options[:class] = "franchise_#{key}" 
    col_options[:sort] = col.key?(:sort) ? col[:sort] : true 
    if col[:value].present? 
    context.send(:column, key, col_options, &col[:value]) 
    else 
    context.send(:column, key, col_options) 
    end 
end 
end 

der Grund, warum ich frage ist, weil ich auch die CH rep, die nur ein anderer Name für das User-Modell ist ein d Ich versuche, den Tisch zu bekommen, die CH rep (Benutzer) Namen angezeigt werden, aber das funktioniert nicht:

value: -> f { view_context.fr_ch_rep_column(f) } 

Antwort

2

Dies ist ein Lambda im Wert Teil eines Hash zu speichern. f entspricht der Variablen, die innerhalb des Lambda beim Aufruf des Lambda zur Verfügung steht.

if col[:value].present? Kontrollen für das Lambda

context.send(:column, key, col_options, &col[:value]) sendet (in der Reihenfolge)

  • column als Symbol
  • den Schlüssel (dh ch_rep oder `user)
  • column_options die den Titel enthält, und andere Nicht-Lambda-Schlüssel/Werte
  • das Lambda alsangegebenSchlüssel, zu context
Verwandte Themen