Class: CSVImporter::ColumnDefinition
- Inherits:
-
Object
- Object
- CSVImporter::ColumnDefinition
- Defined in:
- lib/csv_importer/column_definition.rb
Overview
Define a column. Called from the DSL via ‘column.
Examples
# the csv column "email" will be assigned to the `email` attribute
column :email
# the csv column matching /email/i will be assigned to the `email` attribute
column :email, as: /email/i
# the csv column matching "First name" or "Prénom" will be assigned to the `first_name` attribute
column :first_name, as: [/first ?name/i, /pr(é|e)nom/i]
# the csv column "first_name" will be assigned to the `f_name` attribute
column :first_name, to: :f_name
# email will be downcased
column :email, to: ->(email) { email.downcase }
# transform `confirmed` to `confirmed_at`
column :confirmed, to: ->(confirmed, model) do
model.confirmed_at = confirmed == "true" ? Time.new(2012) : nil
end
Instance Method Summary collapse
-
#attribute ⇒ Object
The model attribute that this column targets.
-
#match?(column_name, search_query = (as || name)) ⇒ Boolean
Return true if column definition matches the column name passed in.
Instance Method Details
#attribute ⇒ Object
The model attribute that this column targets
35 36 37 38 39 40 41 |
# File 'lib/csv_importer/column_definition.rb', line 35 def attribute if to.is_a?(Symbol) to else name end end |
#match?(column_name, search_query = (as || name)) ⇒ Boolean
Return true if column definition matches the column name passed in.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/csv_importer/column_definition.rb', line 44 def match?(column_name, search_query=(as || name)) return false if column_name.nil? downcased_column_name = column_name.downcase underscored_column_name = downcased_column_name.gsub(/\s+/, '_') case search_query when Symbol underscored_column_name == search_query.to_s when String downcased_column_name == search_query.downcase when Regexp column_name =~ search_query when Array search_query.any? { |query| match?(column_name, query) } else raise Error, "Invalid `as`. Should be a Symbol, String, Regexp or Array - was #{as.inspect}" end end |