Skip to content

ksss/active_tsv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ActiveTsv

Build Status

A Class of Active record pattern for TSV/CSV

Usage

data/users.tsv

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

data/nicknames.tsv

id	user_id	nickname
1	1	yuki
2	1	kuri
3	1	k
4	2	f
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
=> #<ActiveTsv::Relation [#<User id: "1", name: "ksss", age: "30">, #<User id: "2", name: "foo", age: "29">, #<User id: "3", name: "bar", age: "30">]>
User.all.to_a
=> [#<User id: "1", name: "ksss", age: "30">, #<User id: "2", name: "foo", age: "29">, #<User id: "3", name: "bar", age: "30">]

User.first
#=> #<User id: "1", name: "ksss", age: "30">
User.last
#=> #<User id: "3", name: "bar", age: "30">

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

User.where(age: 30).to_a
#=> [#<User id: "1", name: "ksss", age: "30">, #<User id: "3", name: "bar", age: "30">]

User.where(age: 30).last
#=> #<User id: "3", name: "bar", age: "30">

User.where(age: 30).where(name: "ksss").first
#=> #<User id: "1", name: "ksss", age: "30">

User.where(id: [1, 2]).to_a
#=> [#<User id: "1", name: "ksss", age: "30">, #<User id: "2", name: "foo", age: "29">]

User.where.not(name: "ksss").first
#=> #<User id: "2", name: "foo", age: "29">

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

User.order(:name).to_a
#=> [#<User id: "3", name: "bar", age: "30">, #<User id: "2", name: "foo", age: "29">, #<User id: "1", name: "ksss", age: "30">]

User.order(name: :desc).to_a
=> [#<User id: "1", name: "ksss", age: "30">, #<User id: "2", name: "foo", age: "29">, #<User id: "3", name: "bar", age: "30">]

User.first.nicknames
#=> #<ActiveTsv::Relation [#<Nickname id: "1", user_id: "1", nickname: "yuki">, #<Nickname id: "2", user_id: "1", nickname: "kuri">, #<Nickname id: "3", user_id: "1", nickname: "k">]>

Nickname.last.user
#=> #<User id: "2", name: "foo", age: "29">

Also Supported CSV.

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:

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.

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published