Class: ActiveRecordCSVImporter::ColumnDefinition
- Inherits:
-
Object
- Object
- ActiveRecordCSVImporter::ColumnDefinition
- Defined in:
- lib/activerecord_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
rubocop:disable all Return true if column definition matches the column name passed in.
Instance Method Details
#attribute ⇒ Object
The model attribute that this column targets
36 37 38 39 40 41 42 |
# File 'lib/activerecord_csv_importer/column_definition.rb', line 36 def attribute if to.is_a?(Symbol) to else name end end |
#match?(column_name, search_query = (as || name)) ⇒ Boolean
rubocop:disable all Return true if column definition matches the column name passed in.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/activerecord_csv_importer/column_definition.rb', line 46 def match?(column_name, search_query = (as || name)) return false if column_name.nil? column_name.gsub!("\xEF\xBB\xBF", '') #strip BOM 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 |