active_tsv 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/active_tsv/base.rb +7 -12
- data/lib/active_tsv/relation.rb +16 -17
- data/lib/active_tsv/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9aed9addfc8d5c3e3edfc4ba6bc47b4bc3da51f1
|
4
|
+
data.tar.gz: 25dd4eb7b93d1967c361fff185576ba589acb9f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3ebbfd04906f7a62897a3affe6640c12d610e2bf6ebb012f48280e6334b9a4a870ef930117b29111e32ae11dac63cb44a9fa070dfc9d567c17c62d8819ebca4
|
7
|
+
data.tar.gz: a14b51ff040d8a55cf1cbc3540852775119083424926368a906d502db0fc23dc1d3786f45d1a33543a3453daa1be1cf5c6dba254e17860a76a5fb71129749b7f
|
data/README.md
CHANGED
@@ -22,6 +22,9 @@ User.first
|
|
22
22
|
#=> #<User {:id=>"1", :name=>"ksss", :age=>"30"}>
|
23
23
|
User.last
|
24
24
|
#=> #<User {:id=>"3", :name=>"bar", :age=>"30"}>
|
25
|
+
User.where(age: 30).each do |user|
|
26
|
+
user.name #=> "ksss", "bar"
|
27
|
+
end
|
25
28
|
User.where(age: 30).to_a
|
26
29
|
#=> [#<User {:id=>"1", :name=>"ksss", :age=>"30"}>, #<User {:id=>"3", :name=>"bar", :age=>"30"}>]
|
27
30
|
User.where(age: 30).last
|
data/lib/active_tsv/base.rb
CHANGED
@@ -8,7 +8,6 @@ module ActiveTsv
|
|
8
8
|
class Base
|
9
9
|
SEPARATER = "\t"
|
10
10
|
class << self
|
11
|
-
include Enumerable
|
12
11
|
attr_reader :table_path
|
13
12
|
|
14
13
|
def table_path=(path)
|
@@ -35,22 +34,18 @@ module ActiveTsv
|
|
35
34
|
raise
|
36
35
|
end
|
37
36
|
|
38
|
-
def
|
39
|
-
|
40
|
-
csv.gets
|
41
|
-
csv.each do |i|
|
42
|
-
yield new(keys.zip(i).to_h)
|
43
|
-
end
|
44
|
-
end
|
37
|
+
def all
|
38
|
+
Relation.new(self, [])
|
45
39
|
end
|
46
40
|
|
47
|
-
def
|
48
|
-
|
41
|
+
def first
|
42
|
+
first_value = open { |csv| csv.gets; csv.gets }
|
43
|
+
new(keys.zip(first_value).to_h)
|
49
44
|
end
|
50
45
|
|
51
46
|
def last
|
52
|
-
|
53
|
-
new(keys.zip(
|
47
|
+
last_value = open { |csv| csv.to_a.last }
|
48
|
+
new(keys.zip(last_value).to_h)
|
54
49
|
end
|
55
50
|
|
56
51
|
def open(&block)
|
data/lib/active_tsv/relation.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module ActiveTsv
|
4
4
|
class Relation
|
5
|
+
include Enumerable
|
6
|
+
|
5
7
|
def initialize(table, conditions)
|
6
8
|
@table = table
|
7
9
|
@conditions = conditions
|
@@ -15,30 +17,27 @@ module ActiveTsv
|
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
18
|
-
def
|
20
|
+
def exists?
|
19
21
|
!first.nil?
|
20
22
|
end
|
21
23
|
|
22
|
-
def first
|
23
|
-
@table.find do |i|
|
24
|
-
@conditions.all? do |cond|
|
25
|
-
cond.values.all? do |k, v|
|
26
|
-
i[k].__send__(cond.method_name, v.to_s)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
24
|
def last
|
33
25
|
to_a.last
|
34
26
|
end
|
35
27
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
28
|
+
def each
|
29
|
+
return to_enum(:each) unless block_given?
|
30
|
+
|
31
|
+
keys = @table.keys
|
32
|
+
key_to_value_index = keys.each_with_index.map { |k, index| [k, index] }.to_h
|
33
|
+
@table.open do |csv|
|
34
|
+
csv.gets
|
35
|
+
csv.each do |value|
|
36
|
+
yield @table.new(@table.keys.zip(value).to_h) if @conditions.all? { |cond|
|
37
|
+
cond.values.all? do |k, v|
|
38
|
+
value[key_to_value_index[k]].__send__(cond.method_name, v.to_s)
|
39
|
+
end
|
40
|
+
}
|
42
41
|
end
|
43
42
|
end
|
44
43
|
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.3
|
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-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|