active_tsv 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/README.md +17 -5
- data/active_tsv.gemspec +2 -2
- data/lib/active_tsv.rb +6 -1
- data/lib/active_tsv/base.rb +15 -17
- data/lib/active_tsv/condition.rb +3 -0
- data/lib/active_tsv/relation.rb +14 -6
- data/lib/active_tsv/version.rb +1 -1
- data/lib/active_tsv/where_chain.rb +12 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af77a62bc25c6e3e5b36a915debd71d55df551ac
|
4
|
+
data.tar.gz: a941cc26593c0ef2c5058e4d039a94bee1a4fcef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6402289d3d87766384feb42444b15a148f1ead18c46a537490fc1266d518a234586b8b13b1318f166ac143450519c7753fbd06013bed16ed988c2c5e21349e0
|
7
|
+
data.tar.gz: fe61cb6f4067928d7a0d7e5222ffcd1ff39abc70c627cd396777831305bc51da61761e33378da07c9568f5d1f4aa588c20ac7aecb96627c877cdffb160e73f41
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# ActiveTsv
|
2
2
|
|
3
|
+
[](https://travis-ci.org/ksss/active_tsv)
|
4
|
+
|
5
|
+
A Class of Active record pattern for TSV/CSV
|
6
|
+
|
3
7
|
## Usage
|
4
8
|
|
5
9
|
```tsv
|
@@ -14,16 +18,24 @@ class User < ActiveTsv::Base
|
|
14
18
|
self.table_path = "data/users.tsv"
|
15
19
|
end
|
16
20
|
|
17
|
-
User.first
|
18
|
-
|
19
|
-
User.
|
20
|
-
|
21
|
-
User.where(age: 30).
|
21
|
+
User.first
|
22
|
+
#=> #<User {:id=>"1", :name=>"ksss", :age=>"30"}>
|
23
|
+
User.last
|
24
|
+
#=> #<User {:id=>"3", :name=>"bar", :age=>"30"}>
|
25
|
+
User.where(age: 30).to_a
|
26
|
+
#=> [#<User {:id=>"1", :name=>"ksss", :age=>"30"}>, #<User {:id=>"3", :name=>"bar", :age=>"30"}>]
|
27
|
+
User.where(age: 30).last
|
28
|
+
#=> #<User {:id=>"3", :name=>"bar", :age=>"30"}>
|
29
|
+
User.where(age: 30).where(name: "ksss").first
|
30
|
+
#=> #<User {:id=>"1", :name=>"ksss", :age=>"30"}>
|
31
|
+
User.where.not(name: "ksss").first
|
32
|
+
#=> #<User {:id=>"2", :name=>"foo", :age=>"29"}>
|
22
33
|
```
|
23
34
|
|
24
35
|
Also Supported **CSV**.
|
25
36
|
|
26
37
|
```ruby
|
38
|
+
require 'active_csv'
|
27
39
|
class User < ActiveCsv::Base
|
28
40
|
self.table_path = "data/users.csv"
|
29
41
|
end
|
data/active_tsv.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["ksss"]
|
10
10
|
spec.email = ["co000ri@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = "Class of Active record pattern for TSV/CSV"
|
13
|
-
spec.description = "Class of Active record pattern for TSV/CSV"
|
12
|
+
spec.summary = "A Class of Active record pattern for TSV/CSV"
|
13
|
+
spec.description = "A Class of Active record pattern for TSV/CSV"
|
14
14
|
spec.homepage = "https://github.com/ksss/active_tsv"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
data/lib/active_tsv.rb
CHANGED
data/lib/active_tsv/base.rb
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'csv'
|
4
|
-
require "active_tsv/relation"
|
5
|
-
|
6
3
|
module ActiveTsv
|
7
4
|
# @example
|
8
5
|
# class User < ActiveTsv::Base
|
@@ -39,9 +36,9 @@ module ActiveTsv
|
|
39
36
|
end
|
40
37
|
|
41
38
|
def each
|
42
|
-
open do |
|
43
|
-
|
44
|
-
|
39
|
+
open do |csv|
|
40
|
+
csv.gets
|
41
|
+
csv.each do |i|
|
45
42
|
yield new(keys.zip(i).to_h)
|
46
43
|
end
|
47
44
|
end
|
@@ -52,7 +49,7 @@ module ActiveTsv
|
|
52
49
|
end
|
53
50
|
|
54
51
|
def last
|
55
|
-
last_values = open { |
|
52
|
+
last_values = open { |csv| csv.to_a.last }
|
56
53
|
new(keys.zip(last_values).to_h)
|
57
54
|
end
|
58
55
|
|
@@ -61,23 +58,24 @@ module ActiveTsv
|
|
61
58
|
end
|
62
59
|
|
63
60
|
def keys
|
64
|
-
@keys ||= open { |
|
61
|
+
@keys ||= open { |csv| csv.gets }.map(&:to_sym)
|
65
62
|
end
|
66
63
|
|
67
|
-
def where(
|
68
|
-
|
64
|
+
def where(condition = nil)
|
65
|
+
if condition
|
66
|
+
Relation.new(self, [Condition.new(:==, condition)])
|
67
|
+
else
|
68
|
+
WhereChain.new(self, [])
|
69
|
+
end
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
72
|
-
def initialize(
|
73
|
-
|
74
|
-
|
75
|
-
values
|
76
|
-
else
|
77
|
-
raise ArgumentError, "#{values.class} is not supported value"
|
73
|
+
def initialize(attrs = {})
|
74
|
+
unless attrs.kind_of?(Hash)
|
75
|
+
raise ArgumentError, "#{attrs.class} is not supported value"
|
78
76
|
end
|
79
77
|
|
80
|
-
|
78
|
+
@attrs = attrs
|
81
79
|
end
|
82
80
|
|
83
81
|
def inspect
|
data/lib/active_tsv/relation.rb
CHANGED
@@ -7,8 +7,12 @@ module ActiveTsv
|
|
7
7
|
@conditions = conditions
|
8
8
|
end
|
9
9
|
|
10
|
-
def where(
|
11
|
-
|
10
|
+
def where(condition = nil)
|
11
|
+
if condition
|
12
|
+
self.class.new(@table, @conditions << Condition.new(:==, condition))
|
13
|
+
else
|
14
|
+
WhereChain.new(@table, @conditions)
|
15
|
+
end
|
12
16
|
end
|
13
17
|
|
14
18
|
def exist?
|
@@ -17,8 +21,10 @@ module ActiveTsv
|
|
17
21
|
|
18
22
|
def first
|
19
23
|
@table.find do |i|
|
20
|
-
@conditions.all? do |
|
21
|
-
|
24
|
+
@conditions.all? do |cond|
|
25
|
+
cond.values.all? do |k, v|
|
26
|
+
i[k].__send__(cond.method_name, v.to_s)
|
27
|
+
end
|
22
28
|
end
|
23
29
|
end
|
24
30
|
end
|
@@ -29,8 +35,10 @@ module ActiveTsv
|
|
29
35
|
|
30
36
|
def to_a
|
31
37
|
@table.select do |i|
|
32
|
-
@conditions.all? do |
|
33
|
-
|
38
|
+
@conditions.all? do |cond|
|
39
|
+
cond.values.all? do |k, v|
|
40
|
+
i[k].__send__(cond.method_name, v.to_s)
|
41
|
+
end
|
34
42
|
end
|
35
43
|
end
|
36
44
|
end
|
data/lib/active_tsv/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_tsv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: Class of Active record pattern for TSV/CSV
|
55
|
+
description: A Class of Active record pattern for TSV/CSV
|
56
56
|
email:
|
57
57
|
- co000ri@gmail.com
|
58
58
|
executables: []
|
@@ -60,6 +60,7 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
63
64
|
- Gemfile
|
64
65
|
- LICENSE.txt
|
65
66
|
- README.md
|
@@ -73,8 +74,10 @@ files:
|
|
73
74
|
- lib/active_csv.rb
|
74
75
|
- lib/active_tsv.rb
|
75
76
|
- lib/active_tsv/base.rb
|
77
|
+
- lib/active_tsv/condition.rb
|
76
78
|
- lib/active_tsv/relation.rb
|
77
79
|
- lib/active_tsv/version.rb
|
80
|
+
- lib/active_tsv/where_chain.rb
|
78
81
|
homepage: https://github.com/ksss/active_tsv
|
79
82
|
licenses:
|
80
83
|
- MIT
|
@@ -98,5 +101,5 @@ rubyforge_project:
|
|
98
101
|
rubygems_version: 2.5.1
|
99
102
|
signing_key:
|
100
103
|
specification_version: 4
|
101
|
-
summary: Class of Active record pattern for TSV/CSV
|
104
|
+
summary: A Class of Active record pattern for TSV/CSV
|
102
105
|
test_files: []
|