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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af77a62bc25c6e3e5b36a915debd71d55df551ac
4
- data.tar.gz: a941cc26593c0ef2c5058e4d039a94bee1a4fcef
3
+ metadata.gz: 9aed9addfc8d5c3e3edfc4ba6bc47b4bc3da51f1
4
+ data.tar.gz: 25dd4eb7b93d1967c361fff185576ba589acb9f6
5
5
  SHA512:
6
- metadata.gz: b6402289d3d87766384feb42444b15a148f1ead18c46a537490fc1266d518a234586b8b13b1318f166ac143450519c7753fbd06013bed16ed988c2c5e21349e0
7
- data.tar.gz: fe61cb6f4067928d7a0d7e5222ffcd1ff39abc70c627cd396777831305bc51da61761e33378da07c9568f5d1f4aa588c20ac7aecb96627c877cdffb160e73f41
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
@@ -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 each
39
- open do |csv|
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 all
48
- to_a
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
- last_values = open { |csv| csv.to_a.last }
53
- new(keys.zip(last_values).to_h)
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)
@@ -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 exist?
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 to_a
37
- @table.select do |i|
38
- @conditions.all? do |cond|
39
- cond.values.all? do |k, v|
40
- i[k].__send__(cond.method_name, v.to_s)
41
- end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveTsv
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
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.2
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-29 00:00:00.000000000 Z
11
+ date: 2016-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler