{"id":22703801,"url":"https://github.com/maceto/file_generator","last_synced_at":"2025-03-29T20:11:36.422Z","repository":{"id":2390888,"uuid":"3356944","full_name":"maceto/file_generator","owner":"maceto","description":"Export information from your application as text files, generating these text files using a string format description.","archived":false,"fork":false,"pushed_at":"2012-02-23T16:04:43.000Z","size":112,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-05T20:54:25.812Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maceto.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-02-05T02:17:28.000Z","updated_at":"2017-12-06T12:23:56.000Z","dependencies_parsed_at":"2022-09-09T01:01:10.790Z","dependency_job_id":null,"html_url":"https://github.com/maceto/file_generator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maceto%2Ffile_generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maceto%2Ffile_generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maceto%2Ffile_generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maceto%2Ffile_generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maceto","download_url":"https://codeload.github.com/maceto/file_generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246237437,"owners_count":20745348,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-10T08:12:45.664Z","updated_at":"2025-03-29T20:11:36.404Z","avatar_url":"https://github.com/maceto.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# file_generator\n\n## Description\n\nEvery time we need to export information from your application as text files, we are\nwriting code for every format we need to generate.\n\nFileGenerator tries to minimize the ammount of code needed to perform this task.\n\n## How does it work?\n\nFileGenerator takes the header, body, and footer file formats as input\nand additionally a hash with the data we want to export.\n\nThe format is expressed as string as follows:\n\neach column has the next attributes\n\n* Name: this name would match with the key in the hash\n* Length: size of the column\n* Value: dafult value in case that name doesn't match with any hash key\n* Fill: for completing the space of length\n* Align: align to Left or Right\n\nas for instance:\n\n\u003cpre\u003e\n\"id:3:0:0:D,name:30:: :I,region_id:3:0:0:D\"\n\u003c/pre\u003e\n\nand the data we want to export is:\n\n\u003cpre\u003e\n[{\"id\"=\u003e1, \"region_id\"=\u003e7, \"name\"=\u003e\"les Escaldes\"},{\"id\"=\u003e2, \"region_id\"=\u003e7, \"name\"=\u003e\"Lima\"}]\n\u003c/pre\u003e\n\nFileGenerator will try to match the attribute names with the format\nnames, if they dont match, it will assign the value to the line of the\nfile to export, otherwise it will take the default value defined in the\nformat.\n\nIt also has names for the format which stablishes a special field as for\ninstance:\n\ntime: puts the current date with the format \"YYYYMMDD\"·\n\nnreg : put the sum of records in the file in the body\n\n## Usage\n\nFor example if you have a Ruby on Rails application and you need to export\ndiferents models like City, Region, Localities, Contacts, etc... you could define a controller Exports and for each model a method to export. Like the next example:\n\nAdd file_generator to your Gemfile.\n\n\u003cpre\u003e\n  gem 'file_generation'\n\u003c/pre\u003e\n\nCreate the method in the Exports controller for generate the file\n\n\u003cpre\u003e\nclass ExportsController\n  def cities\n    headerformat = \"treg:2:CC::I,csuc:3:193::I,time:8:0::I\"\n    bodyformat = \"id:3:0:0:D,name:30:: :I,region_id:3:0:0:D\"\n    footerformat = \"pie:2:CC::I,csuc:3:193::I,nreg:10:0:0:D\"\n    cities = [{\"id\"=\u003e1, \"region_id\"=\u003e7, \"name\"=\u003e\"les Escaldes\"},{\"id\"=\u003e2, \"region_id\"=\u003e7, \"name\"=\u003e\"Lima\"}]\n    content = FileGenerator::Base.generate_file(cities, headerformat, bodyformat, footerformat)\n    send_data content, :filename =\u003e \"cities.txt\"\n  end\n\n  def contacts\n    ...\n  end\n\nend\n\u003c/pre\u003e\n\nAdd the routes in config/routes.rb\n\n\u003cpre\u003e\nresources :exports do\n  get 'cities', :on =\u003e :collection\nend\n\u003c/pre\u003e\n\nand put the link in the view\n\n\u003cpre\u003e\nlink_to(\"Export Cities\", cities_exports_path)\n\u003c/pre\u003e\n\nwith this example you would get a file like this\n\n\u003cpre\u003e\nCC19320120206\n001les Escaldes                  007\n002Lima                          007\nCC1930000000002\n\u003c/pre\u003e\n\n\n# what next ?\n\n* Make header and footer optional\n* Add .csv format like a option\n* Add more pre defined columns like \"nreg\" or \"time\"\n* Add more format for the pre defined column \"time\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaceto%2Ffile_generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaceto%2Ffile_generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaceto%2Ffile_generator/lists"}