{"id":20923705,"url":"https://github.com/cloverinteractive/csvision","last_synced_at":"2025-05-13T16:30:41.023Z","repository":{"id":56843949,"uuid":"3979945","full_name":"cloverinteractive/csvision","owner":"cloverinteractive","description":"Convert Hash into CSV","archived":false,"fork":false,"pushed_at":"2012-04-14T18:54:46.000Z","size":1012,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-25T02:23:34.596Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"cloverinteractive.github.com/csvision","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/cloverinteractive.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-04-10T07:26:50.000Z","updated_at":"2014-02-06T20:00:28.000Z","dependencies_parsed_at":"2022-08-26T12:51:43.158Z","dependency_job_id":null,"html_url":"https://github.com/cloverinteractive/csvision","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloverinteractive%2Fcsvision","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloverinteractive%2Fcsvision/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloverinteractive%2Fcsvision/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloverinteractive%2Fcsvision/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloverinteractive","download_url":"https://codeload.github.com/cloverinteractive/csvision/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253981658,"owners_count":21994313,"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-11-18T20:17:28.833Z","updated_at":"2025-05-13T16:30:40.655Z","avatar_url":"https://github.com/cloverinteractive.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSVision\n\n[![Build Status](https://secure.travis-ci.org/cloverinteractive/csvision.png?branch=master)](http://travis-ci.org/cloverinteractive/csvision)\n\nConvert a Hash into a CSV the easy way.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n`gem 'csvision'`\n\nAnd then execute:\n\n`bundle`\n\nOr install it yourself as:\n\n`gem install csvision`\n\n## Usage\n\nCSVision has no prerequisites other than ruby itself, which is pretty cool just include the `csvision` gem in your app and you'll be good to go.\n\n### Hash\n\nCSVision adds to `Hash` the `to_csv` method, anything that inherits from `Hash` will inherit this method as well, you do not need to open `Hash` and include `CSVision` in it, just require the library in your code.\n\n```ruby\nrequire 'csvision'\nsample = { :name =\u003e 'foo', :last_name =\u003e 'bar', :age =\u003e 10 }\nsample.to_csv #=\u003e \"\\\"last_name\\\",\\\"name\\\",\\\"age\\\"\\n\\\"bar\\\",\\\"foo\\\",\\\"10\\\"\"\n```\n### Rails\n\nCSVision adds the following methods to Rails:\n\n1. `to_csv` at the model instance\n2. `to_csv` at the model class\n3. `add_csvision` at the model class\n\nA rails model might look something like this:\n\n```ruby\nclass Product \u003c ActiveRecord::Base\n  validates_presence_of :name, :permalink, :description\n  add_csvision :except =\u003e %w/updated_at created_at/\nend\n```\n\nCSVision lets you customize the way your CSV is formed, you can use any of the following options in the `add_csvision` method:\n\n1. `:only` to set only those parameters you wish to include.\n2. `:except` this does the opposite it excludes options from the list.\n3. `:delimeter` this set the field delimeter and it defaults to `\"`\n4. `:separator` this set the field separator and it defults to `,`\n\nThat's good and all but, you never want to just convert a single object to csv but collections instead, so imagine this model instead:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  add_csvision :csv_headers =\u003e %w/street_cred nickname incognito/, :body =\u003e lambda { |u| [ u.street_cred, u.nickname, u.incognito ] }\n\n  scope :flunky, where( 'points \u003c= 5' )\n\n  def incognito\n    nickname.reverse\n  end\n\n  def street_cred\n    points * 100 + ( nickname_cred )\n  end\n\n  private\n  def nickname_cred\n    nickname.unpack( 'U' * nickname.length ).sum # good nickname equals more street cred\n  end\nend\n```\n\nYou can use the `:headers` option to set your header names, and the `:body` option to specify the methods or properties from the option you want\ncalled in your csv, this gives you flexibility to choose how your csv is formed and from what it is formed, so this is an example of how this works.\n\n```ruby\n5.times { |i| User.create( :nickname =\u003e \"user#{ i * 3 }\", :points =\u003e i * 5 ) }\nputs User.to_csv\n```\n\nthis will print:\n\n```\n\"street_cred\",\"nickname\",\"incognito\"\n\"495\",\"user0\",\"0resu\"\n\"998\",\"user3\",\"3resu\"\n\"1501\",\"user6\",\"6resu\"\n\"2004\",\"user9\",\"9resu\"\n\"2546\",\"user12\",\"21resu\"\n```\n\nYou can chain active record calls and even scopes in to your mix, so consider:\n\n```ruby\n5.times { |i| User.create( :nickname =\u003e \"user#{ i * 3 }\", :points =\u003e i *5 ) }\nputs User.flunky.to_csv\n```\nwill print:\n\n```\n\"street_cred\",\"nickname\",\"incognito\"\n\"495\",\"user0\",\"0resu\"\n\"998\",\"user3\",\"3resu\"\n\"1501\",\"user6\",\"6resu\"\n```\n\nLastly, with the exception of `:except` and `:only` which will be ignore because you're setting the body manually all other option work:\n\n```ruby\n5.times { |i| User.create( :nickname =\u003e \"user#{ i * 3 }\", :points =\u003e i *5 ) }\nputs User.to_csv( :headers =\u003e false )\n```\n\nWill print:\n\n```\n\"495\",\"user0\",\"0resu\"\n\"998\",\"user3\",\"3resu\"\n\"1501\",\"user6\",\"6resu\"\n\"2004\",\"user9\",\"9resu\"\n\"2546\",\"user12\",\"21resu\"\n```\n\n### Outside of Rails\n\nAnything that inherits from `Hash` will `respond_to? :to_csv` however if you want to use this in any other regular class make sure you do the following:\n\n1. Create a `Hash` attribute named `attributes`.\n2. You'll need to have class `count` and `find_each` methods if you want support for setting the `body`.\n3. `include CSVision` in your class.\n\n```ruby\nclass SampleStack\n  include CSVision\n  attr_accessor :attributes\n\n  add_csvision :csv_headers =\u003e %w/price name/, :body =\u003e lambda { |s| [ s[:name], s[:price] ] }\n\n  def initialize( attributes={}, inventory = [] )\n    @attributes = attributes\n    @@inventory = inventory\n  end\n\n  def self.count\n    @@inventory.size\n  end\n\n  def self.find_each( opts={} )\n    @@inventory.each do |item|\n      yield item\n    end\n  end\nend\n```\n\nAfter wards imagine the following:\n\n```ruby\nsample_stack = SampleStack.new( :foo =\u003e 'foo', :bar =\u003e 'bar' )\nputs sample_stack.to_csv\n```\n\nWill print:\n\n```\n\"foo\",\"bar\"\n\"foo\",\"bar\"\n```\n\nBut more importantly `to_csv` at the class level will:\n\n```ruby\ninventory = [ { :name =\u003e 'beer', :price =\u003e 9 }, { :name =\u003e 'milk', :price =\u003e 5 }, { :name =\u003e 'tuna', :price =\u003e 3 } ]\nsample_stack = SampleStack.new({ :name =\u003e 'groceries'}, inventory)\nputs SampleStack.to_csv\n```\n\nPrints:\n\n```\n\"price\",\"name\"\n\"beer\",\"9\"\n\"milk\",\"5\"\n\"tuna\",\"3\"\n```\n\n## TODO:\n\n1. Add support for other ORM's\n2. Rails 3.x responder.\n\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloverinteractive%2Fcsvision","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloverinteractive%2Fcsvision","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloverinteractive%2Fcsvision/lists"}