conformist icon indicating copy to clipboard operation
conformist copied to clipboard

Make already evaluated columns accessible to preprocessing block

Open cblock opened this issue 11 years ago • 1 comments

For example a boolean columnis_company is defined, which sets the column's content to be truthy or falsy:

#map salutation column to :is_company
column :is_company, 1 do |value|
      value.downcase == 'firma'
end

Now another column is defined that sets its content based on the evaluation result of :is_company column.

Currently my workaround for this is:

#map phone office dependent on  whether record is a company record or not
column :phone_office, 1, 2, 3 do |values|
        #raw data:
        #   values[0] == saluation
        #   values[1] == phone 1
        #   values[2] == phone 2
      if values[0].downcase == 'firma'
        values[1]
      else
        values[2]
      end
    end

But it would be mucher nicer to if one could access the already evaluated columns inside the preprocessing block:

column :phone_office, 2, 3 do |values, already_defined_columns|
        #raw data:
        #   values[1] == phone 1
        #   values[2] == phone 2
      if already_defined_columns[:is_company]
        values[1]
      else
        values[2]
      end
    end

cblock avatar Nov 05 '12 11:11 cblock

Hey,

Thanks for the feedback. I had not thought of this use case before.

At the time each column's preprocessor is evaluated, all of the columns are known, but not necessarily evaluated. We'd need to do something like build a lookup table which evaluates the column the first time, and returns a cached value thereafter. This type of behaviour could lead to circular dependencies if we weren't careful, so the README would need to caution.

The columns are evaluated in Conformist::Builder and the preprocessor is an attribute of Conformist::Column.

I'm too busy to dive into it now, but might work on it at RailsCamp.

tatey avatar Nov 05 '12 12:11 tatey