module CsvRowModel::Model::Columns
Constants
- VALID_OPTIONS_KEYS
Public Instance Methods
@return [Hash] a map of ‘column_name => public_send(column_name)`
# File lib/csv_row_model/model/columns.rb, line 10 def attributes attributes_from_column_names self.class.column_names end
# File lib/csv_row_model/model/columns.rb, line 18 def headers self.class.headers(context) end
# File lib/csv_row_model/model/columns.rb, line 14 def to_json attributes.to_json end
Protected Instance Methods
# File lib/csv_row_model/model/columns.rb, line 28 def array_to_block_hash(array, &block) array .zip( array.map { |column_name| block.call(column_name) } ).to_h end
# File lib/csv_row_model/model/columns.rb, line 24 def attributes_from_column_names(column_names) array_to_block_hash(column_names) { |column_name| public_send(column_name) } end
Adds column to the row model
@param [Symbol] column_name name of column to add @param options [Hash]
@option options [class] :type class you want to automatically parse to (by default does nothing, equivalent to String) @option options [Lambda, Proc] :parse for parsing the cell @option options [Boolean] :validate_type adds a validations within a {::csv_string_model} call. if true, it will add the default validation for the given :type (if applicable)
@option options [Object] :default default value of the column if it is blank?, can pass Proc @option options [String] :header human friendly string of the column name, by default format_header
(column_name) @option options [Hash] :header_matchs array with string to match cell to find in the row, by default column name
# File lib/csv_row_model/model/columns.rb, line 89 def column(column_name, options={}) column_name = column_name.to_sym extra_keys = options.keys - VALID_OPTIONS_KEYS raise ArgumentError.new("invalid options #{extra_keys}") unless extra_keys.empty? merge_columns(column_name => options) end
@return [Array<Symbol>] column names for the row model
# File lib/csv_row_model/model/columns.rb, line 37 def column_names columns.keys end
Safe to override
@return [String] formatted header
# File lib/csv_row_model/model/columns.rb, line 68 def format_header(column_name, context={}) column_name end
@param [Symbol] column_name name of column to find index @return [Integer] index of the column_name
# File lib/csv_row_model/model/columns.rb, line 49 def index(column_name) column_names.index column_name end
@param [Symbol] column_name name of column to check @return [Boolean] true if it’s a column name
# File lib/csv_row_model/model/columns.rb, line 55 def is_column_name? column_name column_name.is_a?(Symbol) && index(column_name) end
# File lib/csv_row_model/model/columns.rb, line 98 def merge_options(column_name, options={}) column_name = column_name.to_sym column(column_name, (options(column_name) || {}).merge(options)) end
@param [Symbol] column_name name of column to find option @return [Hash] options for the column_name
# File lib/csv_row_model/model/columns.rb, line 43 def options(column_name) columns[column_name] end