An open API service indexing awesome lists of open source software.

https://github.com/ksss/active_tsv


https://github.com/ksss/active_tsv

csv ruby tsv

Last synced: 6 months ago
JSON representation

Awesome Lists containing this project

README

          

# ActiveTsv

[![Build Status](https://travis-ci.org/ksss/active_tsv.svg?branch=master)](https://travis-ci.org/ksss/active_tsv)

A Class of Active record pattern for TSV/CSV

## Usage

data/users.tsv

```tsv
id name age
1 ksss 30
2 foo 29
3 bar 30
```

data/nicknames.tsv

```tsv
id user_id nickname
1 1 yuki
2 1 kuri
3 1 k
4 2 f
```

```ruby
require 'active_tsv'

class User < ActiveTsv::Base
self.table_path = "data/users.tsv" # required
# self.encoding = Encoding::Shift_JIS # optional
# self.primary_key = "uid" # optional
has_many :nicknames
end

class Nickname < ActiveTsv::Base
self.table_path = "data/nicknames.tsv"
belongs_to :user
end

User.all
=> #, #, #]>
User.all.to_a
=> [#, #, #]

User.first
#=> #
User.last
#=> #

User.where(age: 30).each do |user|
user.name #=> "ksss", "bar"
end

User.where(age: 30).to_a
#=> [#, #]

User.where(age: 30).last
#=> #

User.where(age: 30).where(name: "ksss").first
#=> #

User.where(id: [1, 2]).to_a
#=> [#, #]

User.where.not(name: "ksss").first
#=> #

User.group(:age).count
#=> {"30"=>2, "29"=>1}

User.order(:name).to_a
#=> [#, #, #]

User.order(name: :desc).to_a
=> [#, #, #]

User.first.nicknames
#=> #, #, #]>

Nickname.last.user
#=> #
```

Also Supported **CSV**.

```ruby
require 'active_csv'
class User < ActiveCsv::Base
self.table_path = "data/users.csv"
end
```

## Goal

Support all methods of ActiveRecord

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'active_tsv'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_tsv

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).