Skip to content
tom statter edited this page Aug 6, 2015 · 5 revisions

Mapping

Build a thorough and usable picture of the operators which can be used to import/export data to objects of supplied type

collection = ModelMethods::Manager.catalog_class(MyModel)

The collection returned, is of type ModelMethods::Collection

The collection will contain a list of all the possible ModelMethods for the class MyModel

You can operators_for_type = collection.for_type(op_type) Binder : map_inbound_headers(load_object_class, headers, options )

OUT OF DATE WITH NEW BRANCH

A core feature of DataShift is the MethodDictionary and MethodMapper, which provides features for collecting reflection information from ActiveRecord models including all different associations, including join tables with many-to-many relationships).

This is the core of mapping between inbound fields, as defined in the CSV or Excel headers, to the actual Model's attributes or associations

A full picture of all possible operations on a class can be created very easily, for example on a Blog model :

MethodDictionary.find_operators( Blog )

This then allows Import/Export to be achieved, by mapping the file's header and column data to the found operators. i.e. maps a column in human readbale form to a methods that sets data (attribute or association) on the model.

Here we retrieve the method details for a column name from a file, "Blog Date"

MethodDictionary.find_method_detail( Blog, "Blog Date" )

Loaders can use this method lookup functionality, to find the correct association for a column heading, and populate AR object with row data.

This means data can be mapped to any model without any further coding. Generators are also supplied to export a models attributes and associations to files, thus providing template spreadsheets that any user can fill out.

MethodMapper also stores column type information so the raw file data can be provided as is, and whenever possible, under the bonnet the data will be cast to correct DB type.

Here we show how a column name from a file, "Blog Date", can be mapped to Assign a stringified date, to the blog_date column, on a new Blog object :

MethodDictionary.find_method_detail( Blog, "Blog Date" ).assign( Blog.new, "Sat Jul 23 2011" )

Because it's based on reflection against the model, can build complex relationships between tables during import/export, and extending data files with new columns need not require any additional Ruby coding.

New columns can simply be added to the Excel/Open Office spreadsheet, or CSV file, setting the new attribute or association name in the header row.

The Loader attempts to handle various human read-able forms of column names.

For example, given an association on the model called, product_properties, will successfully load from columns with headings such as 'product_properties', 'Product Properties', 'ProductProperties' 'product properties' etc

For has_many associations, either multiple columns can be used, or multiple values can be specified in a single column using suitable delimiters.

Modular - so complex associations/mappings that require non generic lookups, can be handled by extending the loader engine.

Clone this wiki locally