Sha256: 629d40f036bedc992d30415460e2161ba2c74c5a20716d4de4ddadeabafe4cb0

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'csv_row_model/internal/attribute_base'
require 'csv_row_model/internal/concerns/dynamic_column_shared'

module CsvRowModel
  class DynamicColumnAttributeBase < AttributeBase
    include DynamicColumnShared

    def value
      @value ||= row_model_class.format_dynamic_column_cells(unformatted_value, column_name, column_index, row_model.context)
    end

    def formatted_cells
      source_cells.map.with_index.map do |cell, index|
        row_model_class.format_cell(cell, column_name, column_index + index, row_model.context)
      end
    end

    protected

    def process_cell_method_name
      self.class.process_cell_method_name(column_name)
    end

    # Calls the process_cell to return the value of a Attribute given the args
    def call_process_cell(*args)
      row_model.public_send(process_cell_method_name, *args)
    end

    class << self
      def process_cell_method_name(column_name)
        column_name.to_s.singularize.to_sym
      end

      # define a method to process each cell of the attribute method
      # process_cell = one cell
      # attribute_method = many cells = [process_cell(), process_cell()...]
      def define_process_cell(row_model_class, column_name, &block)
        row_model_class.define_proxy_method(process_cell_method_name(column_name), &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_row_model-1.0.0.beta2 lib/csv_row_model/internal/dynamic_column_attribute_base.rb