Sha256: d6d8fd25de9a32f121e2ac8d23326dbe92fc1ffdbaf50452fa0709eba52c4e80

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require "csvbuilder/dynamic/columns/core/concerns/model/dynamic_columns"

# Shared between Import and Export, see test fixture for basic setup
module Csvbuilder
  module DynamicColumnsBase
    extend ActiveSupport::Concern
    include Model::DynamicColumns

    def attribute_objects
      @attribute_objects ||= super.merge(dynamic_column_attribute_objects)
    end

    def attributes
      super.merge!(attributes_from_method_names(self.class.dynamic_column_names))
    end

    ATTRIBUTE_METHODS = {
      original_attributes: :value, # a map of `column_name => original_attribute(column_name)`
      formatted_attributes: :formatted_cells # a map of `column_name => format_cell(column_name, ...)`
    }.freeze
    ATTRIBUTE_METHODS.each do |method_name, attribute_method|
      define_method(method_name) do
        super().merge!(
          column_names_to_attribute_value(
            self.class.dynamic_column_names, attribute_method
          )
        )
      end
    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_proxy_method(column_name) do
          original_attribute(column_name)
        end

        dynamic_attribute_class.define_process_cell(self, column_name)
      end

      def ensure_define_dynamic_attribute_method
        dynamic_column_names.each { |*args| define_dynamic_attribute_method(*args) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csvbuilder-dynamic-columns-core-0.1.1 lib/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base.rb