datashift 0.16.0 → 0.40.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{LICENSE.txt → LICENSE} +0 -0
- data/Rakefile +1 -20
- data/datashift.thor +125 -0
- data/lib/applications/apache_poi_extensions.rb +21 -52
- data/lib/applications/excel.rb +64 -57
- data/lib/applications/hssf_row_extensions.rb +66 -0
- data/lib/applications/jexcel_file.rb +99 -95
- data/lib/applications/jexcel_file_extensions.rb +76 -83
- data/lib/applications/jruby/word.rb +36 -36
- data/lib/applications/ruby_poi_translations.rb +34 -32
- data/lib/applications/spreadsheet_extensions.rb +21 -19
- data/lib/datashift.rb +49 -59
- data/lib/datashift/binder.rb +217 -0
- data/lib/datashift/column_packer.rb +21 -72
- data/lib/datashift/configuration.rb +317 -0
- data/lib/datashift/context_factory.rb +88 -0
- data/lib/datashift/core_ext/array.rb +15 -0
- data/lib/datashift/core_ext/csv_ext.rb +46 -0
- data/lib/datashift/core_ext/string.rb +49 -0
- data/lib/datashift/core_ext/to_b.rb +11 -0
- data/lib/datashift/delimiters.rb +55 -61
- data/lib/datashift/doc_context.rb +137 -0
- data/lib/datashift/excel_base.rb +93 -81
- data/lib/datashift/exceptions.rb +30 -28
- data/lib/datashift/file_definitions.rb +44 -39
- data/lib/datashift/guards.rb +5 -5
- data/lib/datashift/header.rb +25 -0
- data/lib/datashift/headers.rb +94 -0
- data/lib/datashift/inbound_data/column.rb +44 -0
- data/lib/datashift/inbound_data/lookup_support.rb +33 -0
- data/lib/datashift/inbound_data/method_binding.rb +139 -0
- data/lib/datashift/load_object.rb +37 -12
- data/lib/datashift/logging.rb +54 -27
- data/lib/datashift/mandatory.rb +39 -0
- data/lib/datashift/mapping/data_flow_schema.rb +198 -0
- data/lib/datashift/{model_mapper.rb → mapping/mapper_utils.rb} +30 -10
- data/lib/datashift/model_methods/catalogue.rb +183 -0
- data/lib/datashift/model_methods/collection.rb +140 -0
- data/lib/datashift/model_methods/model_method.rb +162 -0
- data/lib/datashift/model_methods/model_methods_manager.rb +76 -0
- data/lib/datashift/model_methods/operator.rb +62 -0
- data/lib/datashift/node_collection.rb +26 -0
- data/lib/datashift/node_context.rb +68 -0
- data/lib/datashift/populator.rb +308 -282
- data/lib/datashift/progress_monitor.rb +91 -0
- data/lib/datashift/querying.rb +110 -52
- data/lib/datashift/templates/import_export_config.erb +55 -0
- data/lib/datashift/transformation/factory.rb +219 -0
- data/lib/datashift/transformation/remove.rb +44 -0
- data/lib/datashift/version.rb +3 -0
- data/lib/exporters/configuration.rb +84 -0
- data/lib/exporters/csv_exporter.rb +54 -52
- data/lib/exporters/excel_exporter.rb +80 -61
- data/lib/exporters/exporter_base.rb +8 -8
- data/lib/generators/config_generator.rb +218 -0
- data/lib/generators/csv_generator.rb +13 -70
- data/lib/generators/excel_generator.rb +23 -111
- data/lib/generators/generator_base.rb +15 -70
- data/lib/loaders/configuration.rb +90 -0
- data/lib/loaders/csv_loader.rb +63 -101
- data/lib/loaders/excel_loader.rb +71 -156
- data/lib/loaders/failure_data.rb +40 -0
- data/lib/loaders/file_loader.rb +16 -0
- data/lib/loaders/loader_base.rb +82 -410
- data/lib/loaders/loader_factory.rb +42 -0
- data/lib/loaders/paperclip/attachment_loader.rb +157 -140
- data/lib/loaders/paperclip/datashift_paperclip.rb +18 -35
- data/lib/loaders/paperclip/image_loading.rb +40 -35
- data/lib/loaders/reporters/basic_stdout_reporter.rb +40 -0
- data/lib/loaders/reporters/reporter.rb +26 -0
- data/lib/tasks/config.thor +65 -0
- data/{tasks → lib/tasks}/config/seed_fu_product_template.erb +0 -0
- data/{tasks → lib/tasks}/config/tidy_config.txt +0 -0
- data/lib/tasks/export.thor +192 -0
- data/lib/tasks/generate.thor +190 -0
- data/lib/tasks/import.thor +142 -0
- data/lib/{thor → tasks}/paperclip.thor +69 -69
- data/{tasks → lib/tasks/to_convert_to_thor}/db_tasks.rake +20 -20
- data/lib/tasks/tools.thor +109 -0
- data/spec/MissingAttachmentRecords/DEMO_001_ror_bag.jpeg +0 -0
- data/spec/MissingAttachmentRecords/DEMO_002_Powerstation.jpeg +0 -0
- data/spec/MissingAttachmentRecords/DEMO_003_ror_mug.jpeg +0 -0
- data/spec/MissingAttachmentRecords/DEMO_004_ror_ringer.jpeg +0 -0
- data/spec/datashift/binder_spec.rb +266 -0
- data/spec/datashift/config_generator_spec.rb +186 -0
- data/spec/datashift/configuration.rb +66 -0
- data/spec/datashift/context_factory_spec.rb +63 -0
- data/spec/datashift/data_flow_schema_spec.rb +150 -0
- data/spec/datashift/datashift_spec.rb +52 -0
- data/spec/datashift/excel_base_spec.rb +57 -0
- data/spec/datashift/excel_spec.rb +188 -0
- data/spec/datashift/failure_data_spec.rb +27 -0
- data/spec/{file_definitions.rb → datashift/file_definitions.rb} +9 -10
- data/spec/datashift/headers_spec.rb +56 -0
- data/spec/datashift/inbound_data_spec.rb +47 -0
- data/spec/datashift/mapper_utils_spec.rb +38 -0
- data/spec/datashift/method_binding_spec.rb +60 -0
- data/spec/datashift/model_method_spec.rb +109 -0
- data/spec/datashift/model_methods_catalogue.rb +111 -0
- data/spec/datashift/model_methods_collection_spec.rb +138 -0
- data/spec/datashift/model_methods_manager_spec.rb +329 -0
- data/spec/datashift/populator_spec.rb +117 -0
- data/spec/datashift/thor_spec.rb +314 -0
- data/spec/datashift/transformation/factory_spec.rb +195 -0
- data/spec/datashift/transformation/transformer_remove_spec.rb +43 -0
- data/spec/dummy/Gemfile +53 -0
- data/spec/dummy/Gemfile.lock +197 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +16 -0
- data/spec/dummy/app/assets/javascripts/categories.js +2 -0
- data/spec/dummy/app/assets/javascripts/digitals.js +2 -0
- data/spec/dummy/app/assets/javascripts/empties.js +2 -0
- data/spec/dummy/app/assets/javascripts/loader_releases.js +2 -0
- data/spec/dummy/app/assets/javascripts/long_and_complex_table_linked_to_versions.js +2 -0
- data/spec/dummy/app/assets/javascripts/milestones.js +2 -0
- data/spec/dummy/app/assets/javascripts/owners.js +2 -0
- data/spec/dummy/app/assets/javascripts/projects.js +2 -0
- data/spec/dummy/app/assets/javascripts/users.js +2 -0
- data/spec/dummy/app/assets/javascripts/versions.js +2 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/assets/stylesheets/categories.css +4 -0
- data/spec/dummy/app/assets/stylesheets/digitals.css +4 -0
- data/spec/dummy/app/assets/stylesheets/empties.css +4 -0
- data/spec/dummy/app/assets/stylesheets/loader_releases.css +4 -0
- data/spec/dummy/app/assets/stylesheets/long_and_complex_table_linked_to_versions.css +4 -0
- data/spec/dummy/app/assets/stylesheets/milestones.css +4 -0
- data/spec/dummy/app/assets/stylesheets/owners.css +4 -0
- data/spec/dummy/app/assets/stylesheets/projects.css +4 -0
- data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
- data/spec/dummy/app/assets/stylesheets/users.css +4 -0
- data/spec/dummy/app/assets/stylesheets/versions.css +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/categories_controller.rb +58 -0
- data/spec/dummy/app/controllers/digitals_controller.rb +58 -0
- data/spec/dummy/app/controllers/empties_controller.rb +58 -0
- data/spec/dummy/app/controllers/loader_releases_controller.rb +58 -0
- data/spec/dummy/app/controllers/long_and_complex_table_linked_to_versions_controller.rb +58 -0
- data/spec/dummy/app/controllers/milestones_controller.rb +58 -0
- data/spec/dummy/app/controllers/owners_controller.rb +58 -0
- data/spec/dummy/app/controllers/projects_controller.rb +58 -0
- data/spec/dummy/app/controllers/users_controller.rb +58 -0
- data/spec/dummy/app/controllers/versions_controller.rb +58 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/categories_helper.rb +2 -0
- data/spec/dummy/app/helpers/digitals_helper.rb +2 -0
- data/spec/dummy/app/helpers/empties_helper.rb +2 -0
- data/spec/dummy/app/helpers/loader_releases_helper.rb +2 -0
- data/spec/dummy/app/helpers/long_and_complex_table_linked_to_versions_helper.rb +2 -0
- data/spec/dummy/app/helpers/milestones_helper.rb +2 -0
- data/spec/dummy/app/helpers/owners_helper.rb +2 -0
- data/spec/dummy/app/helpers/projects_helper.rb +2 -0
- data/spec/dummy/app/helpers/users_helper.rb +2 -0
- data/spec/dummy/app/helpers/versions_helper.rb +2 -0
- data/spec/dummy/app/models/category.rb +6 -0
- data/spec/dummy/app/models/digital.rb +22 -0
- data/spec/dummy/app/models/empty.rb +2 -0
- data/spec/dummy/app/models/loader_release.rb +10 -0
- data/spec/dummy/app/models/long_and_complex_table_linked_to_version.rb +6 -0
- data/spec/dummy/app/models/milestone.rb +15 -0
- data/spec/dummy/app/models/owner.rb +13 -0
- data/spec/dummy/app/models/project.rb +53 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/models/version.rb +7 -0
- data/spec/dummy/app/views/categories/_form.html.erb +17 -0
- data/spec/dummy/app/views/categories/edit.html.erb +6 -0
- data/spec/dummy/app/views/categories/index.html.erb +25 -0
- data/spec/dummy/app/views/categories/new.html.erb +5 -0
- data/spec/dummy/app/views/categories/show.html.erb +4 -0
- data/spec/dummy/app/views/digitals/_form.html.erb +17 -0
- data/spec/dummy/app/views/digitals/edit.html.erb +6 -0
- data/spec/dummy/app/views/digitals/index.html.erb +25 -0
- data/spec/dummy/app/views/digitals/new.html.erb +5 -0
- data/spec/dummy/app/views/digitals/show.html.erb +4 -0
- data/spec/dummy/app/views/empties/_form.html.erb +17 -0
- data/spec/dummy/app/views/empties/edit.html.erb +6 -0
- data/spec/dummy/app/views/empties/index.html.erb +25 -0
- data/spec/dummy/app/views/empties/new.html.erb +5 -0
- data/spec/dummy/app/views/empties/show.html.erb +4 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/loader_releases/_form.html.erb +17 -0
- data/spec/dummy/app/views/loader_releases/edit.html.erb +6 -0
- data/spec/dummy/app/views/loader_releases/index.html.erb +25 -0
- data/spec/dummy/app/views/loader_releases/new.html.erb +5 -0
- data/spec/dummy/app/views/loader_releases/show.html.erb +4 -0
- data/spec/dummy/app/views/long_and_complex_table_linked_to_versions/_form.html.erb +17 -0
- data/spec/dummy/app/views/long_and_complex_table_linked_to_versions/edit.html.erb +6 -0
- data/spec/dummy/app/views/long_and_complex_table_linked_to_versions/index.html.erb +25 -0
- data/spec/dummy/app/views/long_and_complex_table_linked_to_versions/new.html.erb +5 -0
- data/spec/dummy/app/views/long_and_complex_table_linked_to_versions/show.html.erb +4 -0
- data/spec/dummy/app/views/milestones/_form.html.erb +17 -0
- data/spec/dummy/app/views/milestones/edit.html.erb +6 -0
- data/spec/dummy/app/views/milestones/index.html.erb +25 -0
- data/spec/dummy/app/views/milestones/new.html.erb +5 -0
- data/spec/dummy/app/views/milestones/show.html.erb +4 -0
- data/spec/dummy/app/views/owners/_form.html.erb +17 -0
- data/spec/dummy/app/views/owners/edit.html.erb +6 -0
- data/spec/dummy/app/views/owners/index.html.erb +25 -0
- data/spec/dummy/app/views/owners/new.html.erb +5 -0
- data/spec/dummy/app/views/owners/show.html.erb +4 -0
- data/spec/dummy/app/views/projects/_form.html.erb +17 -0
- data/spec/dummy/app/views/projects/edit.html.erb +6 -0
- data/spec/dummy/app/views/projects/index.html.erb +25 -0
- data/spec/dummy/app/views/projects/new.html.erb +5 -0
- data/spec/dummy/app/views/projects/show.html.erb +4 -0
- data/spec/dummy/app/views/users/_form.html.erb +17 -0
- data/spec/dummy/app/views/users/edit.html.erb +6 -0
- data/spec/dummy/app/views/users/index.html.erb +25 -0
- data/spec/dummy/app/views/users/new.html.erb +5 -0
- data/spec/dummy/app/views/users/show.html.erb +4 -0
- data/spec/dummy/app/views/versions/_form.html.erb +17 -0
- data/spec/dummy/app/views/versions/edit.html.erb +6 -0
- data/spec/dummy/app/views/versions/index.html.erb +25 -0
- data/spec/dummy/app/views/versions/new.html.erb +5 -0
- data/spec/dummy/app/views/versions/show.html.erb +4 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +9 -0
- data/spec/dummy/bin/rake +9 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/bin/spring +16 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +26 -0
- data/spec/dummy/config/boot.rb +3 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +76 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20110803201325_create_test_bed.rb +98 -0
- data/spec/dummy/db/migrate/20121009161700_add_digitals.rb +24 -0
- data/spec/dummy/db/migrate/20161005123106_create_digitals.rb +8 -0
- data/spec/dummy/db/migrate/20161005123106_create_long_and_complex_table_linked_to_versions.rb +8 -0
- data/spec/dummy/db/migrate/20161005123107_create_loader_releases.rb +8 -0
- data/spec/dummy/db/migrate/20161005123108_create_owners.rb +8 -0
- data/spec/dummy/db/migrate/20161005123109_create_empties.rb +8 -0
- data/spec/dummy/db/migrate/20161005123110_create_projects.rb +8 -0
- data/spec/dummy/db/migrate/20161005123111_create_users.rb +8 -0
- data/spec/dummy/db/migrate/20161005123111_create_versions.rb +8 -0
- data/spec/dummy/db/migrate/20161005123112_create_milestones.rb +8 -0
- data/spec/dummy/db/migrate/20161005123113_create_categories.rb +8 -0
- data/spec/dummy/db/schema.rb +93 -0
- data/spec/dummy/db/seeds.rb +9 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +69 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/robots.txt +5 -0
- data/spec/dummy/sandbox_example.thor +4 -0
- data/spec/dummy/test/controllers/categories_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/digitals_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/empties_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/loader_releases_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/long_and_complex_table_linked_to_versions_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/milestones_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/owners_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/projects_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/users_controller_test.rb +49 -0
- data/spec/dummy/test/controllers/versions_controller_test.rb +49 -0
- data/spec/dummy/test/factories/categories.rb +5 -0
- data/spec/dummy/test/factories/digitals.rb +5 -0
- data/spec/dummy/test/factories/empties.rb +5 -0
- data/spec/dummy/test/factories/loader_releases.rb +5 -0
- data/spec/dummy/test/factories/long_and_complex_table_linked_to_versions.rb +5 -0
- data/spec/dummy/test/factories/milestones.rb +5 -0
- data/spec/dummy/test/factories/owners.rb +5 -0
- data/spec/dummy/test/factories/projects.rb +5 -0
- data/spec/dummy/test/factories/users.rb +5 -0
- data/spec/dummy/test/factories/versions.rb +5 -0
- data/spec/dummy/test/models/category_test.rb +7 -0
- data/spec/dummy/test/models/digital_test.rb +7 -0
- data/spec/dummy/test/models/empty_test.rb +7 -0
- data/spec/dummy/test/models/loader_release_test.rb +7 -0
- data/spec/dummy/test/models/long_and_complex_table_linked_to_version_test.rb +7 -0
- data/spec/dummy/test/models/milestone_test.rb +7 -0
- data/spec/dummy/test/models/owner_test.rb +7 -0
- data/spec/dummy/test/models/project_test.rb +7 -0
- data/spec/dummy/test/models/user_test.rb +7 -0
- data/spec/dummy/test/models/version_test.rb +7 -0
- data/spec/dummy/test/test_helper.rb +10 -0
- data/spec/exporters/csv_exporter_spec.rb +240 -0
- data/spec/exporters/csv_generator_spec.rb +139 -0
- data/spec/exporters/excel_exporter_spec.rb +193 -0
- data/spec/exporters/excel_generator_spec.rb +181 -0
- data/spec/exporters/generator_base_spec.rb +45 -0
- data/spec/factories/categories.rb +7 -0
- data/spec/factories/factories.rb +18 -0
- data/spec/factories/milestone.rb +16 -0
- data/spec/factories/projects.rb +41 -0
- data/spec/fixtures/BadAssociationName.xls +0 -0
- data/spec/fixtures/DemoNegativeTesting.xls +0 -0
- data/spec/fixtures/ProjectConfiguration.yml +18 -0
- data/spec/fixtures/ProjectsMultiCategories.xls +0 -0
- data/spec/fixtures/ProjectsMultiCategoriesHeaderLookup.xls +0 -0
- data/spec/fixtures/ProjectsSingleCategories.xls +0 -0
- data/spec/fixtures/ProjectsSingleCategories.xlsx +0 -0
- data/spec/fixtures/SimpleProjects.xls +0 -0
- data/spec/fixtures/config/database.yml +28 -0
- data/spec/fixtures/csv/BadAssociationName.csv +6 -0
- data/spec/fixtures/csv/DemoNegativeTesting.csv +6 -0
- data/spec/fixtures/csv/ProjectsMultiCategories.csv +5 -0
- data/spec/fixtures/csv/ProjectsMultiCategoriesHeaderLookup.csv +5 -0
- data/spec/fixtures/csv/ProjectsSingleCategories.csv +5 -0
- data/spec/fixtures/csv/SimpleProjects.csv +4 -0
- data/spec/fixtures/db/migrate/20110803201325_create_test_bed.rb +98 -0
- data/spec/fixtures/db/migrate/20121009161700_add_digitals.rb +24 -0
- data/spec/fixtures/db/seeds.rb +9 -0
- data/spec/fixtures/images/DEMO_001_ror_bag.jpeg +0 -0
- data/spec/fixtures/images/DEMO_002_Powerstation.jpeg +0 -0
- data/spec/fixtures/images/DEMO_003_ror_mug.jpeg +0 -0
- data/spec/fixtures/images/DEMO_004_ror_ringer.jpeg +0 -0
- data/spec/fixtures/load_datashift.thor +3 -0
- data/spec/fixtures/models/category.rb +6 -0
- data/spec/fixtures/models/digital.rb +22 -0
- data/spec/fixtures/models/empty.rb +2 -0
- data/spec/fixtures/models/loader_release.rb +10 -0
- data/spec/fixtures/models/long_and_complex_table_linked_to_version.rb +6 -0
- data/spec/fixtures/models/milestone.rb +15 -0
- data/spec/fixtures/models/owner.rb +13 -0
- data/spec/fixtures/models/project.rb +53 -0
- data/spec/fixtures/models/user.rb +5 -0
- data/spec/fixtures/models/version.rb +7 -0
- data/spec/fixtures/results/exp_project_assoc_headers.xls +0 -0
- data/spec/fixtures/results/exp_project_collection_spec.csv +2 -0
- data/spec/fixtures/results/exp_project_export.xls +0 -0
- data/spec/fixtures/results/exp_project_first_export.xls +0 -0
- data/spec/fixtures/results/exp_project_plus_assoc.xls +0 -0
- data/spec/fixtures/results/exp_project_plus_assoc_export_spec.csv +9 -0
- data/spec/fixtures/results/gen_project_plus_assoc_template.xls +0 -0
- data/spec/fixtures/results/gen_project_plus_some_assoc_template.xls +0 -0
- data/spec/fixtures/results/gen_project_template.xls +0 -0
- data/spec/fixtures/results/project_and_assoc_in_hash_export.xls +0 -0
- data/spec/fixtures/results/project_and_assoc_in_json_export.csv +9 -0
- data/spec/fixtures/results/project_and_assoc_in_json_export.xls +0 -0
- data/spec/fixtures/results/project_export_spec_with_custom_delim_,.csv +2 -0
- data/spec/fixtures/results/project_export_spec_with_custom_delim_/302/243.csv +2 -0
- data/spec/fixtures/results/project_export_spec_with_custom_delim_/302/247.csv +2 -0
- data/spec/fixtures/results/project_plus_assoc_template.csv +1 -0
- data/spec/fixtures/results/project_plus_some_assoc_template.csv +1 -0
- data/spec/fixtures/results/project_remove_export_spec.csv +2 -0
- data/spec/fixtures/results/project_template.csv +1 -0
- data/spec/fixtures/results/project_with_methods_export_spec.csv +2 -0
- data/spec/fixtures/results/thor_spec_gen_project.csv +1 -0
- data/spec/fixtures/sandbox_example.thor +4 -0
- data/spec/fixtures/simple_export_spec.xls +0 -0
- data/spec/fixtures/simple_template_spec.xls +0 -0
- data/spec/fixtures/test_model_defs.rb +7 -0
- data/spec/loaders/csv_loader_spec.rb +206 -0
- data/spec/loaders/data_flow_excel_loader_spec.rb +290 -0
- data/spec/loaders/excel_loader_failures_spec.rb +67 -0
- data/spec/loaders/excel_loader_spec.rb +294 -0
- data/spec/loaders/loader_base_spec.rb +29 -0
- data/spec/loaders/paperclip_loader_spec.rb +106 -0
- data/spec/log/datashift.log +14930 -0
- data/spec/private/digitals/1/DEMO_003_ror_mug.jpeg +0 -0
- data/spec/private/digitals/2/DEMO_002_Powerstation.jpeg +0 -0
- data/spec/private/digitals/3/DEMO_004_ror_ringer.jpeg +0 -0
- data/spec/private/digitals/4/DEMO_001_ror_bag.jpeg +0 -0
- data/spec/spec_helper.rb +26 -230
- data/spec/support/clear_and_manage_contexts.rb +25 -0
- data/spec/support/database_cleaner.rb +32 -0
- data/spec/support/datashift_test_helpers.rb +153 -0
- data/spec/support/files_paths_helper.rb +13 -0
- data/spec/support/fixtures/results/mapping_template.yaml +15 -0
- data/spec/support/sandbox.rb +136 -0
- metadata +804 -85
- data/README.markdown +0 -274
- data/README.rdoc +0 -19
- data/VERSION +0 -1
- data/datashift.gemspec +0 -48
- data/lib/applications/jruby/old_pre_proxy_jexcel_file.rb +0 -437
- data/lib/datashift/data_transforms.rb +0 -83
- data/lib/datashift/mapping_file_definitions.rb +0 -88
- data/lib/datashift/mapping_service.rb +0 -91
- data/lib/datashift/method_detail.rb +0 -165
- data/lib/datashift/method_details_manager.rb +0 -95
- data/lib/datashift/method_dictionary.rb +0 -281
- data/lib/datashift/method_mapper.rb +0 -174
- data/lib/datashift/thor_base.rb +0 -38
- data/lib/generators/mapping_generator.rb +0 -112
- data/lib/helpers/core_ext/csv_file.rb +0 -33
- data/lib/helpers/core_ext/to_b.rb +0 -24
- data/lib/loaders/reporter.rb +0 -58
- data/lib/thor/export.thor +0 -175
- data/lib/thor/generate.thor +0 -191
- data/lib/thor/import.thor +0 -110
- data/lib/thor/mapping.thor +0 -65
- data/lib/thor/tools.thor +0 -84
- data/spec/Gemfile +0 -31
- data/spec/Gemfile.lock +0 -134
- data/spec/csv_exporter_spec.rb +0 -144
- data/spec/csv_generator_spec.rb +0 -159
- data/spec/csv_loader_spec.rb +0 -212
- data/spec/datashift_spec.rb +0 -55
- data/spec/excel_exporter_spec.rb +0 -199
- data/spec/excel_generator_spec.rb +0 -203
- data/spec/excel_loader_spec.rb +0 -237
- data/spec/excel_spec.rb +0 -203
- data/spec/loader_base_spec.rb +0 -166
- data/spec/mapping_spec.rb +0 -117
- data/spec/method_dictionary_spec.rb +0 -300
- data/spec/method_mapper_spec.rb +0 -100
- data/spec/model_mapper_spec.rb +0 -41
- data/spec/paperclip_loader_spec.rb +0 -92
- data/spec/populator_spec.rb +0 -128
- data/spec/thor_spec.rb +0 -90
- data/tasks/file_tasks.rake +0 -37
- data/tasks/word_to_seedfu.rake +0 -167
data/lib/datashift/thor_base.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2012
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Dec 2014
|
4
|
-
# License:: MIT.
|
5
|
-
#
|
6
|
-
# Note, not DataShift, case sensitive, create namespace for command line : datashift
|
7
|
-
require 'thor'
|
8
|
-
|
9
|
-
module DataShift
|
10
|
-
|
11
|
-
class DSThorBase < Thor
|
12
|
-
|
13
|
-
include DataShift::Logging
|
14
|
-
|
15
|
-
no_commands do
|
16
|
-
|
17
|
-
def start_connections()
|
18
|
-
|
19
|
-
# TODO - We're assuming run from a rails app/top level dir...
|
20
|
-
|
21
|
-
if(File.exists?(File.expand_path('config/environment.rb')))
|
22
|
-
begin
|
23
|
-
require File.expand_path('config/environment.rb')
|
24
|
-
rescue => e
|
25
|
-
logger.error("Failed to initialise ActiveRecord : #{e.message}")
|
26
|
-
raise ConnectionError.new("No config/environment.rb found - cannot initialise ActiveRecord")
|
27
|
-
end
|
28
|
-
|
29
|
-
else
|
30
|
-
raise PathError.new("No config/environment.rb found - cannot initialise ActiveRecord")
|
31
|
-
# TODO make this more robust ? e.g what about when using active record but not in Rails app, Sinatra etc
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
@@ -1,112 +0,0 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2015
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2015
|
4
|
-
# License:: MIT
|
5
|
-
#
|
6
|
-
# Details:: Create mappings between systems
|
7
|
-
#
|
8
|
-
require 'generator_base'
|
9
|
-
|
10
|
-
module DataShift
|
11
|
-
|
12
|
-
class MappingGenerator < GeneratorBase
|
13
|
-
|
14
|
-
include DataShift::Logging
|
15
|
-
include ExcelBase
|
16
|
-
|
17
|
-
def initialize(filename)
|
18
|
-
super(filename)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Create an YAML template for mapping headers
|
22
|
-
#
|
23
|
-
# Options:
|
24
|
-
#
|
25
|
-
# * <tt>:model_as_dest</tt> - Override default treatment of using model as the SOURCE
|
26
|
-
#
|
27
|
-
# * <tt>:remove</tt> - Array of header names to remove
|
28
|
-
#
|
29
|
-
# Rails columns like id, created_at etc are added to the remove list by default
|
30
|
-
#
|
31
|
-
# * <tt>:include_rails</tt> - Specify to keep Rails columns in mappings
|
32
|
-
#
|
33
|
-
# * <tt>:associations</tt> - Additionally include all Associations
|
34
|
-
#
|
35
|
-
# * <tt>:exclude</tt> - Association TYPE(s) to exclude.
|
36
|
-
#
|
37
|
-
# Possible association_type values are given by MethodDetail::supported_types_enum
|
38
|
-
# ... [:assignment, :belongs_to, :has_one, :has_many]
|
39
|
-
#
|
40
|
-
# * <tt>:file</tt> - Write mappings direct to file name provided
|
41
|
-
#
|
42
|
-
def generate(model = nil, options = {})
|
43
|
-
|
44
|
-
mappings = "mappings:\n"
|
45
|
-
|
46
|
-
if(model)
|
47
|
-
|
48
|
-
klass = DataShift::ModelMapper.class_from_string_or_raise( model )
|
49
|
-
|
50
|
-
MethodDictionary.find_operators( klass )
|
51
|
-
|
52
|
-
MethodDictionary.build_method_details( klass )
|
53
|
-
|
54
|
-
prepare_model_headers(MethodDictionary.method_details_mgrs[klass], options)
|
55
|
-
|
56
|
-
if(options[:model_as_dest])
|
57
|
-
headers.each_with_index do |s, i| mappings += " #srcs_column_heading_#{i}: #{s}\n" end
|
58
|
-
else
|
59
|
-
headers.each_with_index do |s, i| mappings += " #{s}: #dest_column_heading_#{i}\n" end
|
60
|
-
end
|
61
|
-
else
|
62
|
-
mappings += <<EOS
|
63
|
-
##source_column_heading_0: #dest_column_heading_0
|
64
|
-
##source_column_heading_1: #dest_column_heading_1
|
65
|
-
##source_column_heading_2: #dest_column_heading_2
|
66
|
-
|
67
|
-
EOS
|
68
|
-
end
|
69
|
-
|
70
|
-
File.open(options[:file], 'w') do |f| f << mappings end if(options[:file])
|
71
|
-
|
72
|
-
mappings
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
# Create an YAML template from a Excel spreadsheet for mapping headers
|
77
|
-
#
|
78
|
-
# * <tt>:model_as_dest</tt> - Override default treatment of using model as the SOURCE
|
79
|
-
#
|
80
|
-
# * <tt>:file</tt> - Write mappings direct to file name provided
|
81
|
-
#
|
82
|
-
def generate_from_excel(excel_file_name, options = {})
|
83
|
-
|
84
|
-
excel = Excel.new
|
85
|
-
|
86
|
-
puts "\n\n\nGenerating mapping from Excel file: #{excel_file_name}"
|
87
|
-
|
88
|
-
excel.open(excel_file_name)
|
89
|
-
|
90
|
-
sheet_number = options[:sheet_number] || 0
|
91
|
-
|
92
|
-
sheet = excel.worksheet( sheet_number )
|
93
|
-
|
94
|
-
parse_headers(sheet, options[:header_row])
|
95
|
-
|
96
|
-
mappings = "mappings:\n"
|
97
|
-
|
98
|
-
if(options[:model_as_dest])
|
99
|
-
excel_headers.each_with_index do |s, i| mappings += " #srcs_column_heading_#{i}: #{s}\n" end
|
100
|
-
else
|
101
|
-
excel_headers.each_with_index do |s, i| mappings += " #{s}: #dest_column_heading_#{i}\n" end
|
102
|
-
end
|
103
|
-
|
104
|
-
File.open(options[:file], 'w') do |f| f << mappings end if(options[:file])
|
105
|
-
|
106
|
-
mappings
|
107
|
-
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
end # DataShift
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# Copyright:: Autotelik Media Ltd
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: July 2010
|
4
|
-
# License::
|
5
|
-
#
|
6
|
-
# Details:: Simple internal representation of Csv File
|
7
|
-
|
8
|
-
require 'csv'
|
9
|
-
|
10
|
-
class CSV
|
11
|
-
|
12
|
-
include DataShift::ColumnPacker
|
13
|
-
|
14
|
-
# Helpers for dealing with Active Record models and collections
|
15
|
-
# Specify array of operators/associations to include - possible values are :
|
16
|
-
# [:assignment, :belongs_to, :has_one, :has_many]
|
17
|
-
|
18
|
-
def ar_to_headers( records, associations = nil, options = {} )
|
19
|
-
add_row( to_headers(records, associations, options) )
|
20
|
-
end
|
21
|
-
|
22
|
-
# Convert an AR instance to a set of CSV columns
|
23
|
-
# Additional non instance data can be included by supplying list of methods to call
|
24
|
-
# on the record
|
25
|
-
def ar_to_csv(record, options = {})
|
26
|
-
csv_data = record.serializable_hash.values.collect { |c| escape_for_csv(c) }
|
27
|
-
|
28
|
-
[*options[:methods]].each { |x| csv_data << escape_for_csv(record.send(x)) if(record.respond_to?(x)) } if(options[:methods])
|
29
|
-
|
30
|
-
add_row(csv_data)
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
class Object
|
2
|
-
def to_b
|
3
|
-
case self
|
4
|
-
when true, false then self
|
5
|
-
when nil then false
|
6
|
-
else
|
7
|
-
to_i != 0
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
class String
|
13
|
-
TRUE_REGEXP = /^(yes|true|on|t|1|\-1)$/i.freeze
|
14
|
-
FALSE_REGEXP = /^(no|false|off|f|0)$/i.freeze
|
15
|
-
|
16
|
-
def to_b
|
17
|
-
case self
|
18
|
-
when TRUE_REGEXP then true
|
19
|
-
when FALSE_REGEXP then false
|
20
|
-
else
|
21
|
-
to_i != 0
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/lib/loaders/reporter.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Dec 2012
|
4
|
-
# License:: MIT
|
5
|
-
#
|
6
|
-
# Details:: Store and report stats
|
7
|
-
#
|
8
|
-
module DataShift
|
9
|
-
|
10
|
-
|
11
|
-
class Reporter
|
12
|
-
|
13
|
-
include DataShift::Logging
|
14
|
-
|
15
|
-
# actual data rows/objects inbound
|
16
|
-
attr_accessor :processed_object_count
|
17
|
-
alias_method :processed_inbound_count, :processed_object_count
|
18
|
-
|
19
|
-
# DB objects created, updated etc
|
20
|
-
attr_accessor :loaded_objects, :failed_objects
|
21
|
-
|
22
|
-
# actual data rows/objects inbound
|
23
|
-
attr_accessor :success_inbound_count, :failed_inbound_count
|
24
|
-
|
25
|
-
def initialize()
|
26
|
-
reset
|
27
|
-
end
|
28
|
-
|
29
|
-
def reset()
|
30
|
-
@processed_object_count = 0
|
31
|
-
@loaded_objects, @failed_objects = [], []
|
32
|
-
@success_inbound_count, @failed_inbound_count = 0,0
|
33
|
-
end
|
34
|
-
|
35
|
-
def add_loaded_object(object)
|
36
|
-
@loaded_objects << object.id unless(object.nil? || @loaded_objects.include?(object))
|
37
|
-
end
|
38
|
-
|
39
|
-
def add_failed_object(object)
|
40
|
-
@failed_objects << object unless( object.nil? || @failed_objects.include?(object))
|
41
|
-
end
|
42
|
-
|
43
|
-
def report
|
44
|
-
loaded_objects.compact! if(loaded_objects)
|
45
|
-
|
46
|
-
puts "\nProcessing Summary Report"
|
47
|
-
puts ">>>>>>>>>>>>>>>>>>>>>>>>>\n"
|
48
|
-
puts "Processed total of #{processed_object_count} inbound #{processed_object_count > 1 ? 'entries' : 'entry'}"
|
49
|
-
puts "#{loaded_objects.size}\tdatabase objects were successfully processed."
|
50
|
-
puts "#{success_inbound_count}\tinbound rows were successfully processed."
|
51
|
-
|
52
|
-
puts "There were NO failures." if failed_objects.empty?
|
53
|
-
|
54
|
-
puts "WARNING : Check logs : #{failed_objects.size} rows contained errors" unless failed_objects.empty?
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
data/lib/thor/export.thor
DELETED
@@ -1,175 +0,0 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2012
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: April 2012
|
4
|
-
# License:: MIT.
|
5
|
-
#
|
6
|
-
# Usage::
|
7
|
-
#
|
8
|
-
# To pull Datashift commands into your main application :
|
9
|
-
#
|
10
|
-
# require 'datashift'
|
11
|
-
#
|
12
|
-
# DataShift::load_commands
|
13
|
-
#
|
14
|
-
# Cmd Line:
|
15
|
-
#
|
16
|
-
# => bundle exec thor datashift:export:excel -m <active record class> -r <output_template.xls> -a
|
17
|
-
#
|
18
|
-
require 'thor_base'
|
19
|
-
|
20
|
-
|
21
|
-
require 'datashift'
|
22
|
-
|
23
|
-
# Note, for thor not DataShift, case sensitive, want namespace for cmd line to be : datashift
|
24
|
-
module Datashift
|
25
|
-
|
26
|
-
|
27
|
-
class Export < DataShift::DSThorBase
|
28
|
-
|
29
|
-
desc "excel", "export any active record model (with optional associations)"
|
30
|
-
method_option :model, :aliases => '-m', :required => true, :desc => "The active record model to export"
|
31
|
-
method_option :result, :aliases => '-r', :required => true, :desc => "Create template of model in supplied file"
|
32
|
-
method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
|
33
|
-
method_option :exclude, :aliases => '-e', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
|
34
|
-
|
35
|
-
def excel()
|
36
|
-
|
37
|
-
start_connections
|
38
|
-
|
39
|
-
require 'excel_exporter'
|
40
|
-
|
41
|
-
model = options[:model]
|
42
|
-
result = options[:result]
|
43
|
-
|
44
|
-
logger.info "Datashift: Start Excel export to #{result}"
|
45
|
-
|
46
|
-
klass = DataShift::ModelMapper::class_from_string(model) #Kernel.const_get(model)
|
47
|
-
|
48
|
-
raise "ERROR: No such Model [#{model}] found - check valid model supplied via -model <Class>" if(klass.nil?)
|
49
|
-
|
50
|
-
begin
|
51
|
-
gen = DataShift::ExcelExporter.new(result)
|
52
|
-
|
53
|
-
if(options[:assoc])
|
54
|
-
opts = (options[:exclude]) ? {:exclude => options[:exclude]} : {}
|
55
|
-
logger.info("Datashift: Exporting with associations")
|
56
|
-
gen.export_with_associations(klass, klass.all, opts)
|
57
|
-
else
|
58
|
-
gen.export(klass.all, :sheet_name => klass.name)
|
59
|
-
end
|
60
|
-
rescue => e
|
61
|
-
puts e
|
62
|
-
puts e.backtrace
|
63
|
-
puts "Warning: Error during export, data may be incomplete"
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
desc "csv", "export any active record model (with optional associations)"
|
69
|
-
method_option :model, :aliases => '-m', :required => true, :desc => "The active record model to export"
|
70
|
-
method_option :result, :aliases => '-r', :required => true, :desc => "Create template of model in supplied file"
|
71
|
-
method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
|
72
|
-
method_option :exclude, :aliases => '-e', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
|
73
|
-
method_option :methods, :aliases => '-c', :type => :array, :desc => "List of additional methods to call on model, useful for situations like delegated methods"
|
74
|
-
|
75
|
-
def csv()
|
76
|
-
|
77
|
-
# TODO - We're assuming run from a rails app/top level dir...
|
78
|
-
# ...can we make this more robust ? e.g what about when using active record but not in Rails app,
|
79
|
-
require File.expand_path('config/environment.rb')
|
80
|
-
|
81
|
-
require 'csv_exporter'
|
82
|
-
|
83
|
-
model = options[:model]
|
84
|
-
result = options[:result]
|
85
|
-
|
86
|
-
logger.info "Datashift: Start CSV export to #{result}"
|
87
|
-
|
88
|
-
klass = DataShift::ModelMapper::class_from_string(model) #Kernel.const_get(model)
|
89
|
-
|
90
|
-
raise "ERROR: No such Model [#{model}] found - check valid model supplied via -model <Class>" if(klass.nil?)
|
91
|
-
|
92
|
-
begin
|
93
|
-
gen = DataShift::CsvExporter.new(result)
|
94
|
-
|
95
|
-
if(options[:assoc])
|
96
|
-
logger.info("Datashift: Exporting with associations")
|
97
|
-
gen.export_with_associations(klass, klass.all, options)
|
98
|
-
else
|
99
|
-
gen.export(klass.all, options)
|
100
|
-
end
|
101
|
-
rescue => e
|
102
|
-
puts e
|
103
|
-
puts e.backtrace
|
104
|
-
puts "Warning: Error during export, data may be incomplete"
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
desc "db", "Export every Active Record model"
|
110
|
-
|
111
|
-
method_option :result, :aliases => '-r', :required => true, :desc => "Path in which to create excel files"
|
112
|
-
method_option :csv, :aliases => '-c', :desc => "Export to CSV instead - Excel is default."
|
113
|
-
method_option :prefix, :aliases => '-p', :desc => "For namespaced tables/models specify the table prefix e.g spree_"
|
114
|
-
method_option :module, :aliases => '-m', :desc => "For namespaced tables/models specify the Module name e.g Spree"
|
115
|
-
method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
|
116
|
-
method_option :exclude, :aliases => '-e', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
|
117
|
-
|
118
|
-
def db()
|
119
|
-
|
120
|
-
require File.expand_path('config/environment.rb')
|
121
|
-
|
122
|
-
require 'excel_exporter'
|
123
|
-
require 'csv_exporter'
|
124
|
-
|
125
|
-
exporter = options[:csv] ? DataShift::CsvExporter.new(nil) : DataShift::ExcelExporter.new(nil)
|
126
|
-
|
127
|
-
ext = options[:csv] ? '.csv' : '.xls'
|
128
|
-
|
129
|
-
# Hmmm not many models appear - Rails uses autoload !
|
130
|
-
#ActiveRecord::Base.send(:subclasses).each do |model|
|
131
|
-
# puts model.name
|
132
|
-
#end
|
133
|
-
|
134
|
-
parent = options[:module] ? Object.const_get(options[:module]) : Object
|
135
|
-
|
136
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
137
|
-
|
138
|
-
table.sub!(options[:prefix],'') if(options[:prefix])
|
139
|
-
|
140
|
-
@result = File.join(options[:result], "#{table}#{ext}")
|
141
|
-
|
142
|
-
begin
|
143
|
-
@klass = parent.const_get(table.classify)
|
144
|
-
rescue => e
|
145
|
-
puts e.inspect
|
146
|
-
puts "WARNING: Could not find an AR model for Table #{table}"
|
147
|
-
next
|
148
|
-
end
|
149
|
-
|
150
|
-
puts "Datashift: Start export to #{@result}"
|
151
|
-
|
152
|
-
exporter.filename = @result
|
153
|
-
|
154
|
-
raise "ERROR: No such Model [#{@klass}] found - check valid model supplied via -model <Class>" if(@klass.nil?)
|
155
|
-
|
156
|
-
begin
|
157
|
-
|
158
|
-
if(options[:assoc])
|
159
|
-
opts = (options[:exclude]) ? {:exclude => options[:exclude]} : {}
|
160
|
-
logger.info("Datashift: Exporting with associations")
|
161
|
-
exporter.export_with_associations(@klass, @klass.all, opts)
|
162
|
-
else
|
163
|
-
exporter.export(@klass.all, :sheet_name => @klass.name)
|
164
|
-
end
|
165
|
-
rescue => e
|
166
|
-
puts e
|
167
|
-
puts e.backtrace
|
168
|
-
puts "Warning: Error during export, data may be incomplete"
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
end
|
174
|
-
|
175
|
-
end
|
data/lib/thor/generate.thor
DELETED
@@ -1,191 +0,0 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2012
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Mar 2012
|
4
|
-
# License:: MIT.
|
5
|
-
#
|
6
|
-
# Usage::
|
7
|
-
#
|
8
|
-
# To pull Datashift commands into your main application :
|
9
|
-
#
|
10
|
-
# require 'datashift'
|
11
|
-
#
|
12
|
-
# DataShift::load_commands
|
13
|
-
#
|
14
|
-
# Cmd Line:
|
15
|
-
#
|
16
|
-
# => bundle exec thor list datashift
|
17
|
-
# bundle exec thor help datashift:generate:excel
|
18
|
-
#
|
19
|
-
require 'datashift'
|
20
|
-
require 'generators/excel_generator'
|
21
|
-
|
22
|
-
# Note, not DataShift, case sensitive, create namespace for command line : datashift
|
23
|
-
module Datashift
|
24
|
-
|
25
|
-
|
26
|
-
class Generate < Thor
|
27
|
-
|
28
|
-
include DataShift::Logging
|
29
|
-
|
30
|
-
desc "excel", "generate a template from an active record model (with optional associations)"
|
31
|
-
method_option :model, :aliases => '-m', :required => true, :desc => "The active record model to export"
|
32
|
-
method_option :result, :aliases => '-r', :required => true, :desc => "Create template of model in supplied file"
|
33
|
-
method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
|
34
|
-
method_option :exclude, :aliases => '-x', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
|
35
|
-
method_option :remove, :aliases => '-e', :type => :array, :desc => "Don't generate in template the supplied fields"
|
36
|
-
method_option :remove_rails, :type => :boolean, :desc => "Don't generate in template the standard Rails fields: #{DataShift::GeneratorBase::rails_columns.inspect}"
|
37
|
-
|
38
|
-
|
39
|
-
def excel()
|
40
|
-
|
41
|
-
# TODO - We're assuming run from a rails app/top level dir...
|
42
|
-
# ...can we make this more robust ? e.g what about when using active record but not in Rails app,
|
43
|
-
require File.expand_path('config/environment.rb')
|
44
|
-
|
45
|
-
model = options[:model]
|
46
|
-
result = options[:result]
|
47
|
-
|
48
|
-
logger.info "Datashift: Start Excel template generation in #{result}"
|
49
|
-
|
50
|
-
klass = DataShift::ModelMapper.class_from_string_or_raise( model )
|
51
|
-
|
52
|
-
begin
|
53
|
-
gen = DataShift::ExcelGenerator.new(result)
|
54
|
-
|
55
|
-
opts = { :remove => options[:remove],
|
56
|
-
:remove_rails => options[:remove_rails]
|
57
|
-
}
|
58
|
-
|
59
|
-
if(options[:assoc])
|
60
|
-
|
61
|
-
opts[:exclude] = options[:exclude]
|
62
|
-
|
63
|
-
logger.info("Datashift: Generating with associations")
|
64
|
-
gen.generate_with_associations(klass, opts)
|
65
|
-
else
|
66
|
-
gen.generate(klass, opts)
|
67
|
-
end
|
68
|
-
rescue => e
|
69
|
-
puts e
|
70
|
-
puts e.backtrace
|
71
|
-
puts "Warning: Error during generation, template may be incomplete"
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
desc "csv", "generate a template from an active record model (with optional associations)"
|
78
|
-
method_option :model, :aliases => '-m', :required => true, :desc => "The active record model to export"
|
79
|
-
method_option :result, :aliases => '-r', :required => true, :desc => "Create template of model in supplied file"
|
80
|
-
method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
|
81
|
-
method_option :exclude, :aliases => '-x', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
|
82
|
-
|
83
|
-
def csv()
|
84
|
-
|
85
|
-
# TODO - We're assuming run from a rails app/top level dir...
|
86
|
-
# ...can we make this more robust ? e.g what about when using active record but not in Rails app,
|
87
|
-
require File.expand_path('config/environment.rb')
|
88
|
-
|
89
|
-
require 'csv_generator'
|
90
|
-
|
91
|
-
model = options[:model]
|
92
|
-
result = options[:result]
|
93
|
-
|
94
|
-
logger.info "Datashift: Start CSV template generation in #{result}"
|
95
|
-
|
96
|
-
begin
|
97
|
-
# support modules e.g "Spree::Property")
|
98
|
-
klass = ModelMapper::class_from_string(model) #Kernel.const_get(model)
|
99
|
-
rescue NameError => e
|
100
|
-
puts e
|
101
|
-
raise Thor::Error.new("ERROR: No such Model [#{model}] found - check valid model supplied")
|
102
|
-
end
|
103
|
-
|
104
|
-
raise Thor::Error.new("ERROR: No such Model [#{model}] found - check valid model supplied") unless(klass)
|
105
|
-
|
106
|
-
begin
|
107
|
-
gen = DataShift::CsvGenerator.new(result)
|
108
|
-
|
109
|
-
if(options[:assoc])
|
110
|
-
opts = (options[:exclude]) ? {:exclude => options[:exclude]} : {}
|
111
|
-
logger.info("Datashift: Generating with associations")
|
112
|
-
gen.generate_with_associations(klass, opts)
|
113
|
-
else
|
114
|
-
gen.generate(klass)
|
115
|
-
end
|
116
|
-
rescue => e
|
117
|
-
puts e
|
118
|
-
puts e.backtrace
|
119
|
-
puts "Warning: Error during generation, template may be incomplete"
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
desc "db", "Generate a template for every Active Record model"
|
125
|
-
|
126
|
-
method_option :result, :aliases => '-r', :required => true, :desc => "Path in which to create excel files"
|
127
|
-
method_option :csv, :aliases => '-c', :desc => "Export to CSV instead - Excel is default."
|
128
|
-
method_option :prefix, :aliases => '-p', :desc => "For namespaced tables/models specify the table prefix e.g spree_"
|
129
|
-
method_option :module, :aliases => '-m', :desc => "For namespaced tables/models specify the Module name e.g Spree"
|
130
|
-
method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
|
131
|
-
method_option :exclude, :aliases => '-x', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
|
132
|
-
method_option :remove, :aliases => '-e', :type => :array, :desc => "Don't generate the user supplied fields"
|
133
|
-
method_option :remove_rails, :type => :boolean, :desc => "Don't generate the standard Rails fields: #{DataShift::GeneratorBase::rails_columns.inspect}"
|
134
|
-
|
135
|
-
def db()
|
136
|
-
|
137
|
-
require File.expand_path('config/environment.rb')
|
138
|
-
|
139
|
-
require 'excel_exporter'
|
140
|
-
require 'csv_exporter'
|
141
|
-
|
142
|
-
exporter = options[:csv] ? DataShift::CsvGenerator.new(nil) : DataShift::ExcelGenerator.new(nil)
|
143
|
-
|
144
|
-
ext = options[:csv] ? '.csv' : '.xls'
|
145
|
-
|
146
|
-
parent = options[:module] ? Object.const_get(options[:module]) : Object
|
147
|
-
|
148
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
149
|
-
|
150
|
-
table.sub!(options[:prefix],'') if(options[:prefix])
|
151
|
-
|
152
|
-
@result = File.join(options[:result], "#{table}#{ext}")
|
153
|
-
|
154
|
-
begin
|
155
|
-
@klass = parent.const_get(table.classify)
|
156
|
-
rescue => e
|
157
|
-
puts e.inspect
|
158
|
-
puts "WARNING: Could not find an AR model for Table #{table}"
|
159
|
-
next
|
160
|
-
end
|
161
|
-
|
162
|
-
puts "Datashift: Start template generation to #{@result}"
|
163
|
-
|
164
|
-
raise "ERROR: No such Model [#{@klass}] found - check valid model supplied via -model <Class>" if(@klass.nil?)
|
165
|
-
|
166
|
-
begin
|
167
|
-
opts = { :filename => @result,
|
168
|
-
:remove => options[:remove],
|
169
|
-
:remove_rails => options[:remove_rails],
|
170
|
-
:sheet_name => @klass.name
|
171
|
-
}
|
172
|
-
|
173
|
-
if(options[:assoc])
|
174
|
-
opts[:exclude] = options[:exclude]
|
175
|
-
logger.info("Datashift: Generating with associations")
|
176
|
-
exporter.generate_with_associations(@klass, opts)
|
177
|
-
else
|
178
|
-
exporter.generate(@klass, opts)
|
179
|
-
end
|
180
|
-
rescue => e
|
181
|
-
puts e
|
182
|
-
puts e.backtrace
|
183
|
-
puts "Warning: Error during export, data may be incomplete"
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
|
189
|
-
end
|
190
|
-
|
191
|
-
end
|