Skip to content

Repository files navigation

ReciteCSV

ReciteCSV assists to implement a class for CSV reader.
A reader class implemented by ReciteCSV iterate each row as PORO(Plain Old Ruby Object).

Gem VersionBuildMaintainability

Installation

Add this line to your application's Gemfile:

gem'recite_csv'

And then execute:

$ bundle

Or install it yourself as:

$ gem install recite_csv

Usage

The following is a example csv file.

COL1,COL2VALUE1,VALUE2VALUE3,VALUE4

Specify header definition using hash object.

classFooincludeReciteCSV::Reader::Builder.new(col1: "COL1",col2: "COL2")endFoo.new("./example.csv").eachdo |row|
row.class# => Foo::Rowrow.col1row.col2end

Specify header definition using array object.

classBarincludeReciteCSV::Reader::Builder.new(%w[col1col2])endBar.new("./example.csv").eachdo |row|
row.class# => Bar::Rowrow.col1row.col2end

Define 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_methodend

Define 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_methodend

Specify file mode and encoding.

classQuuxincludeReciteCSV::Reader::Builder.new(col1: "COL1",col2: "COL2")endQuux.new("./example.csv",file_options: "rb:UTF-8").eachdo |row|
# do somethingend

Convert 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 somethingend

Development

After 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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/yujideveloper/recite_csv.

License

The gem is available as open source under the terms of the MIT License.

About

ReciteCSV assists to implement a class for csv reader.

Topics

Resources

Stars

7 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages