Sha256: f940dd5daa2fc92aa1479466f339bebe2f9b05b04ae044ed625f28dff089a7f0

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

module CsvRowModel
  module Export
    module DynamicColumns
      extend ActiveSupport::Concern

      included do
        self.dynamic_column_names.each { |*args| define_dynamic_attribute_method(*args) }
      end

      # @return [Array] an array of public_send(column_name) of the CSV model
      def to_row
        super.flatten
      end

      # See Model::Columns#formatted_attributes
      def formatted_attributes
        super.merge(formatted_attributes_from_column_names(self.class.dynamic_column_names))
      end

      class_methods do
        protected

        # See {Model::DynamicColumns#dynamic_column}
        def dynamic_column(column_name, options={})
          super
          define_dynamic_attribute_method(column_name)
        end

        # Define default attribute method for a dynamic_column
        # @param column_name [Symbol] the cell's column_name
        def define_dynamic_attribute_method(column_name)
          define_method(column_name) do
            context.public_send(column_name).map do |header_model|
              self.class.format_cell(
                public_send(self.class.singular_dynamic_attribute_method_name(column_name), header_model),
                column_name,
                self.class.dynamic_index(column_name),
                context
              )
            end
          end

          define_method(singular_dynamic_attribute_method_name(column_name)) { |header_model| header_model }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
csv_row_model-0.3.9 lib/csv_row_model/export/dynamic_columns.rb
csv_row_model-0.3.8 lib/csv_row_model/export/dynamic_columns.rb
csv_row_model-0.3.7 lib/csv_row_model/export/dynamic_columns.rb
csv_row_model-0.3.6 lib/csv_row_model/export/dynamic_columns.rb
csv_row_model-0.3.5 lib/csv_row_model/export/dynamic_columns.rb
csv_row_model-0.3.4 lib/csv_row_model/export/dynamic_columns.rb