active_tsv 0.0.1 → 0.0.2

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: 20859e6d4107b8e9e3fb7b9ab8021f3b6e7513fa
4
- data.tar.gz: fdbbeb75a1cc931b1e0f71a6ed52feaf2ebba8c6
3
+ metadata.gz: af77a62bc25c6e3e5b36a915debd71d55df551ac
4
+ data.tar.gz: a941cc26593c0ef2c5058e4d039a94bee1a4fcef
5
5
  SHA512:
6
- metadata.gz: 7b54f0040676810d9a45c1e90bb1e4660ae839a47572b6ca2978564b34ff6b9d1000b4515c45552c84a9503310c054a5df97d05cdf4cb3896f51b99fff7437b3
7
- data.tar.gz: f1834382d585677b4d3c4a5b1b4258064e3430984896d61e940d44aa6748829e1ae6f7d4f1fc3adef517c33cdcd13fc1d311aee527961aed191e7b1ead08de2b
6
+ metadata.gz: b6402289d3d87766384feb42444b15a148f1ead18c46a537490fc1266d518a234586b8b13b1318f166ac143450519c7753fbd06013bed16ed988c2c5e21349e0
7
+ data.tar.gz: fe61cb6f4067928d7a0d7e5222ffcd1ff39abc70c627cd396777831305bc51da61761e33378da07c9568f5d1f4aa588c20ac7aecb96627c877cdffb160e73f41
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.10
4
+ - 2.2.5
5
+ - 2.3.1
6
+ notifications:
7
+ email: false
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # ActiveTsv
2
2
 
3
+ [![Build Status](https://travis-ci.org/ksss/active_tsv.svg?branch=master)](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 #=> #<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"}>
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
@@ -1,4 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_tsv/version"
3
+ require 'csv'
4
+
4
5
  require "active_tsv/base"
6
+ require "active_tsv/relation"
7
+ require "active_tsv/where_chain"
8
+ require "active_tsv/condition"
9
+ require "active_tsv/version"
@@ -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 |f|
43
- f.gets
44
- f.each do |i|
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 { |f| f.to_a.last }
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 { |f| f.gets }.map(&:to_sym)
61
+ @keys ||= open { |csv| csv.gets }.map(&:to_sym)
65
62
  end
66
63
 
67
- def where(conditions)
68
- Relation.new(self, conditions)
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(values = {})
73
- @attrs = case values
74
- when Hash
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
- self.class.keys.each { |k| __send__ "#{k}=", @attrs[k] }
78
+ @attrs = attrs
81
79
  end
82
80
 
83
81
  def inspect
@@ -0,0 +1,3 @@
1
+ module ActiveTsv
2
+ Condition = Struct.new(:method_name, :values)
3
+ end
@@ -7,8 +7,12 @@ module ActiveTsv
7
7
  @conditions = conditions
8
8
  end
9
9
 
10
- def where(conditions)
11
- self.class.new(@table, @conditions.merge(conditions))
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 |k, v|
21
- i[k] == v.to_s
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 |k, v|
33
- i[k] == v.to_s
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveTsv
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
@@ -0,0 +1,12 @@
1
+ module ActiveTsv
2
+ class WhereChain
3
+ def initialize(table, conditions)
4
+ @table = table
5
+ @conditions = conditions
6
+ end
7
+
8
+ def not(condition)
9
+ Relation.new(@table, @conditions << Condition.new(:!=, condition))
10
+ end
11
+ end
12
+ 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.1
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-27 00:00:00.000000000 Z
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: []