active_tsv 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/active_tsv.gemspec +23 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/data/names.tsv +4 -0
- data/data/users.csv +4 -0
- data/data/users.tsv +4 -0
- data/lib/active_csv.rb +9 -0
- data/lib/active_tsv.rb +4 -0
- data/lib/active_tsv/base.rb +101 -0
- data/lib/active_tsv/relation.rb +38 -0
- data/lib/active_tsv/version.rb +5 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 20859e6d4107b8e9e3fb7b9ab8021f3b6e7513fa
|
4
|
+
data.tar.gz: fdbbeb75a1cc931b1e0f71a6ed52feaf2ebba8c6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b54f0040676810d9a45c1e90bb1e4660ae839a47572b6ca2978564b34ff6b9d1000b4515c45552c84a9503310c054a5df97d05cdf4cb3896f51b99fff7437b3
|
7
|
+
data.tar.gz: f1834382d585677b4d3c4a5b1b4258064e3430984896d61e940d44aa6748829e1ae6f7d4f1fc3adef517c33cdcd13fc1d311aee527961aed191e7b1ead08de2b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 ksss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# ActiveTsv
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
```tsv
|
6
|
+
id name age
|
7
|
+
1 ksss 30
|
8
|
+
2 foo 29
|
9
|
+
3 bar 30
|
10
|
+
```
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
class User < ActiveTsv::Base
|
14
|
+
self.table_path = "data/users.tsv"
|
15
|
+
end
|
16
|
+
|
17
|
+
User.first #=> #<User {:id=>"1", :name=>"ksss", :age=>"30"}>
|
18
|
+
User.last #=> #<User {:id=>"3", :name=>"bar", :age=>"30"}>
|
19
|
+
User.where(age: 30).to_a #=> [#<User {:id=>"1", :name=>"ksss", :age=>"30"}>, #<User {:id=>"3", :name=>"bar", :age=>"30"}>]
|
20
|
+
User.where(age: 30).last #=> #<User {:id=>"3", :name=>"bar", :age=>"30"}>
|
21
|
+
User.where(age: 30).where(name: "ksss").first #=> #<User {:id=>"1", :name=>"ksss", :age=>"30"}>
|
22
|
+
```
|
23
|
+
|
24
|
+
Also Supported **CSV**.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class User < ActiveCsv::Base
|
28
|
+
self.table_path = "data/users.csv"
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
## Goal
|
33
|
+
|
34
|
+
Support all methods that like ActiveRecord
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
gem 'active_tsv'
|
42
|
+
```
|
43
|
+
|
44
|
+
And then execute:
|
45
|
+
|
46
|
+
$ bundle
|
47
|
+
|
48
|
+
Or install it yourself as:
|
49
|
+
|
50
|
+
$ gem install active_tsv
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/active_tsv.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_tsv/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_tsv"
|
8
|
+
spec.version = ActiveTsv::VERSION
|
9
|
+
spec.authors = ["ksss"]
|
10
|
+
spec.email = ["co000ri@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Class of Active record pattern for TSV/CSV"
|
13
|
+
spec.description = "Class of Active record pattern for TSV/CSV"
|
14
|
+
spec.homepage = "https://github.com/ksss/active_tsv"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{_test.rb}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rgot"
|
23
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_tsv"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/data/names.tsv
ADDED
data/data/users.csv
ADDED
data/data/users.tsv
ADDED
data/lib/active_csv.rb
ADDED
data/lib/active_tsv.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require "active_tsv/relation"
|
5
|
+
|
6
|
+
module ActiveTsv
|
7
|
+
# @example
|
8
|
+
# class User < ActiveTsv::Base
|
9
|
+
# self.table_path = "table/product_masters.tsv"
|
10
|
+
# end
|
11
|
+
class Base
|
12
|
+
SEPARATER = "\t"
|
13
|
+
class << self
|
14
|
+
include Enumerable
|
15
|
+
attr_reader :table_path
|
16
|
+
|
17
|
+
def table_path=(path)
|
18
|
+
reload(path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def reload(path)
|
22
|
+
old_table_path = table_path
|
23
|
+
if @keys
|
24
|
+
keys.each do |k|
|
25
|
+
remove_method(k)
|
26
|
+
remove_method("#{k}=")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
@keys = nil
|
31
|
+
@table_path = path
|
32
|
+
keys.each do |k|
|
33
|
+
define_method(k) { @attrs[k] }
|
34
|
+
define_method("#{k}=") { |v| @attrs[k] = v }
|
35
|
+
end
|
36
|
+
rescue
|
37
|
+
reload(old_table_path)
|
38
|
+
raise
|
39
|
+
end
|
40
|
+
|
41
|
+
def each
|
42
|
+
open do |f|
|
43
|
+
f.gets
|
44
|
+
f.each do |i|
|
45
|
+
yield new(keys.zip(i).to_h)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def all
|
51
|
+
to_a
|
52
|
+
end
|
53
|
+
|
54
|
+
def last
|
55
|
+
last_values = open { |f| f.to_a.last }
|
56
|
+
new(keys.zip(last_values).to_h)
|
57
|
+
end
|
58
|
+
|
59
|
+
def open(&block)
|
60
|
+
CSV.open(table_path, col_sep: self::SEPARATER, &block)
|
61
|
+
end
|
62
|
+
|
63
|
+
def keys
|
64
|
+
@keys ||= open { |f| f.gets }.map(&:to_sym)
|
65
|
+
end
|
66
|
+
|
67
|
+
def where(conditions)
|
68
|
+
Relation.new(self, conditions)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def initialize(values = {})
|
73
|
+
@attrs = case values
|
74
|
+
when Hash
|
75
|
+
values
|
76
|
+
else
|
77
|
+
raise ArgumentError, "#{values.class} is not supported value"
|
78
|
+
end
|
79
|
+
|
80
|
+
self.class.keys.each { |k| __send__ "#{k}=", @attrs[k] }
|
81
|
+
end
|
82
|
+
|
83
|
+
def inspect
|
84
|
+
"#<#{self.class} #{to_h}>"
|
85
|
+
end
|
86
|
+
|
87
|
+
def [](key)
|
88
|
+
__send__ key
|
89
|
+
end
|
90
|
+
|
91
|
+
def []=(key, value)
|
92
|
+
__send__ "#{key}=", value
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_h
|
96
|
+
h = {}
|
97
|
+
self.class.keys.map { |k| h[k] = __send__ k }
|
98
|
+
h
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveTsv
|
4
|
+
class Relation
|
5
|
+
def initialize(table, conditions)
|
6
|
+
@table = table
|
7
|
+
@conditions = conditions
|
8
|
+
end
|
9
|
+
|
10
|
+
def where(conditions)
|
11
|
+
self.class.new(@table, @conditions.merge(conditions))
|
12
|
+
end
|
13
|
+
|
14
|
+
def exist?
|
15
|
+
!first.nil?
|
16
|
+
end
|
17
|
+
|
18
|
+
def first
|
19
|
+
@table.find do |i|
|
20
|
+
@conditions.all? do |k, v|
|
21
|
+
i[k] == v.to_s
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def last
|
27
|
+
to_a.last
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_a
|
31
|
+
@table.select do |i|
|
32
|
+
@conditions.all? do |k, v|
|
33
|
+
i[k] == v.to_s
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_tsv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rgot
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Class of Active record pattern for TSV/CSV
|
56
|
+
email:
|
57
|
+
- co000ri@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- active_tsv.gemspec
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- data/names.tsv
|
71
|
+
- data/users.csv
|
72
|
+
- data/users.tsv
|
73
|
+
- lib/active_csv.rb
|
74
|
+
- lib/active_tsv.rb
|
75
|
+
- lib/active_tsv/base.rb
|
76
|
+
- lib/active_tsv/relation.rb
|
77
|
+
- lib/active_tsv/version.rb
|
78
|
+
homepage: https://github.com/ksss/active_tsv
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.5.1
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Class of Active record pattern for TSV/CSV
|
102
|
+
test_files: []
|