ReciteCSV assists to implement a class for CSV reader.
A reader class implemented by ReciteCSV iterate each row as PORO(Plain Old Ruby Object).
Add this line to your application's Gemfile:
gem'recite_csv'And then execute:
$ bundle
Or install it yourself as:
$ gem install recite_csv
The following is a example csv file.
COL1,COL2VALUE1,VALUE2VALUE3,VALUE4Specify header definition using hash object.
classFooincludeReciteCSV::Reader::Builder.new(col1: "COL1",col2: "COL2")endFoo.new("./example.csv").eachdo |row|
row.class# => Foo::Rowrow.col1row.col2endSpecify header definition using array object.
classBarincludeReciteCSV::Reader::Builder.new(%w[col1col2])endBar.new("./example.csv").eachdo |row|
row.class# => Bar::Rowrow.col1row.col2endDefine custom methods of row object.
classBazinclude(ReciteCSV::Reader::Builder.new(col1: "COL1",col2: "COL2")do# define methods of Row classdefcol1"override #{super}"enddefcustom_method# do somethings..endend)endBaz.new("./example.csv").eachdo |row|
row.class# => Baz::Rowrow.col1row.col2row.custom_methodendDefine custom methods of row object using row_methods.
classQuxincludeReciteCSV::Reader::Builder.new(col1: "COL1",col2: "COL2")row_methodsdodefcol1"override #{super}"enddefcustom_method# do somethings..endendendQux.new("./example.csv").eachdo |row|
row.class# => Qux::Rowrow.col1row.col2row.custom_methodendSpecify file mode and encoding.
classQuuxincludeReciteCSV::Reader::Builder.new(col1: "COL1",col2: "COL2")endQuux.new("./example.csv",file_options: "rb:UTF-8").eachdo |row|
# do somethingendConvert encoding.
classCorgeincludeReciteCSV::Reader::Builder.new(col1: "COL1",col2: "COL2")endCorge.new("./example.csv",file_options: ["rb:Shift_JIS:UTF-8",invalid: :replace,undef: :replace]).eachdo |row|
# do somethingendAfter checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/yujideveloper/recite_csv.
The gem is available as open source under the terms of the MIT License.