Sha256: 1a013a4c370c1e6acd6189fe195927576f1e545811a38c0efe6b7929d5a8a542

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module CsvRowModel
  # Include this to with {Model} to have a RowModel for importing csvs that
  # represents just one model.
  # It needs CsvRowModel::Import
  module Import
    module SingleModel
      extend ActiveSupport::Concern

      class_methods do

        # @return [Symbol] returns type of import
        def type
          :single_model
        end

        # Safe to override
        #
        # @param cell [String] the cell's string
        # @return [Integer] returns index of the header_match that cell match
        def index_header_match(cell)
          match = header_matchers.each_with_index.select do |matcher, index|
            cell.match(matcher)
          end.first
          match ? match[1] : nil
        end

        # @return [Array] header_matchs matchers for the row model
        def header_matchers
          @header_matchers ||= begin
            columns.map do |name, options|
              matchers = options[:header_matchs] || [name.to_s]
              Regexp.new(matchers.join('|'),Regexp::IGNORECASE)
            end
          end
        end
      end
    end
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_row_model-0.1.0 lib/csv_row_model/import/single_model.rb