csvdb 1.0.0 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01b591779bbeda29a4abd006ff1c9e8497435081
4
- data.tar.gz: 10446b54f89b929d518bcc37592a90ce611d1d17
3
+ metadata.gz: c89ea1821aaa5b1728600ac8b404796ac1c4dc93
4
+ data.tar.gz: 89d7d20acea0bdca8b7ec695ea6bc7897267e761
5
5
  SHA512:
6
- metadata.gz: 630ff17554d73ecee364aa64f7b85c51e04f0746d93a34030f179ac38ac7c9bb2bfbbb4ef190432ccf2409bf5e772dbe9518fbc680a3616031e3814d498c1384
7
- data.tar.gz: f3f6eebeb7e6969f24e7dafcf1062fbdc00cfd8c3ce8080ab4b7f1a17ce3ea0f789d41b34bfc903ee8c4bfc111a99a9806aeab4c66ff24deaddda246a4a85789
6
+ metadata.gz: 0a5553377d18108ca657d41a3514fdcf46c405259256f9a5e87aeaaf911826cc6587dc1506154a9cc38680689220e13ecf8c85e04f891187723e9fdff5006031
7
+ data.tar.gz: cd14b88f02430cdc95dbd71d4c5b0bb0edde4a5c068fbf1438fb2e6499ca927fefe2110bcfe9fb5c864456c0257966c4fdffece39bddfe4848d29ca1b19d3b3e
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Csvdb
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/csvdb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Really simple object relational mapping for CSV spreadsheets. Loads all data into a ruby array first so not appropriate for very large datasets. However, it's just as fast as working with a 2d ruby array, so it's very fast for reasonably sized datasets.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,17 +20,51 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ #### CRUD Functions
24
+ ```ruby
25
+ table = Csvdb.new(file: 'table.csv')
26
+ table.cols.keys #=> [:id, :name] The table headings (from row[0] in file)
27
+ table.create(
28
+ id: 1, name: 'foobar'
29
+ ) #=> [1, 'foobar'] Newly created row
30
+ row = table.find(0) #=> [1, 'foobar'] find by index in array
31
+ row.id #=> 1
32
+ row.update(id: 2) #=> [2, 'foobar']
33
+ row.delete
34
+ row.find(0) #=> nil
35
+ ```
36
+
37
+ #### Querying
38
+ ```ruby
39
+ table = Csvdv.new(file: 'table.csv')
40
+ table.count #=> 5
41
+
42
+ # queries always return new table objects, with the same headings,
43
+ # just filtered by the block
44
+ query = table.where { |row| row[table.id] == 1 }
45
+ query.count #=> 1
26
46
 
27
- ## Development
47
+ # joins also always return table objects.
48
+ # the join joins on the same column name in each table.
49
+ joined = table.join(another_table, :column)
50
+ ```
28
51
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
52
+ #### Printing
53
+ ```ruby
54
+ table = Csvdb.new(file: 'table.csv')
55
+ table.pretty
56
+ +---------+-------+--------+--------+----------+
57
+ | version | type | counts | resets | time |
58
+ +---------+-------+--------+--------+----------+
59
+ | 1.1.0 | build | 741 | 6 | 0.046694 |
60
+ ...
61
+
62
+ ```
30
63
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
64
 
33
65
  ## Contributing
34
66
 
35
- 1. Fork it ( https://github.com/[my-github-username]/csvdb/fork )
67
+ 1. Fork it ( https://github.com/ColDog/csvdb/fork )
36
68
  2. Create your feature branch (`git checkout -b my-new-feature`)
37
69
  3. Commit your changes (`git commit -am 'Add some feature'`)
38
70
  4. Push to the branch (`git push origin my-new-feature`)
@@ -3,4 +3,6 @@ module Csvdb
3
3
  end
4
4
  class ParseError < StandardError
5
5
  end
6
+ class TableError < StandardError
7
+ end
6
8
  end
@@ -3,10 +3,20 @@ require 'csvdb/row'
3
3
  require 'csvdb/errors'
4
4
 
5
5
  module Csvdb
6
+
7
+ class Join
8
+
9
+ end
10
+
6
11
  class Table
7
- attr_accessor :cols, :table
12
+ attr_accessor :cols, :table, :table_name
8
13
 
9
14
  def initialize(opts = {})
15
+ if opts[:name]
16
+ @table_name = opts[:name]
17
+ else
18
+ raise TableError, 'No name on that table'
19
+ end
10
20
 
11
21
  if opts[:file]
12
22
  @file = opts[:file]
@@ -17,7 +27,7 @@ module Csvdb
17
27
  table = opts[:ary]
18
28
  @cols = opts[:cols]
19
29
  else
20
- raise ParseError, 'No table or file to parse.'
30
+ raise TableError, 'No table or file to parse.'
21
31
  end
22
32
 
23
33
  @cols.each do |col, val|
@@ -32,6 +42,7 @@ module Csvdb
32
42
  end
33
43
 
34
44
  def write(file = @file)
45
+ File.open(file, 'w') unless File.file?(file)
35
46
  CSV.open(file, 'wb') do |csv|
36
47
  csv << @cols.keys
37
48
  @table.each do |row|
@@ -56,8 +67,38 @@ module Csvdb
56
67
  @table << Row.new(new_row, self, @table.length)
57
68
  end
58
69
 
59
- def where
60
- Table.new(ary: @table.select { |row| yield(row) }, cols: self.cols)
70
+ def add(attrs)
71
+ @table << Row.new(attrs, self, @table.length)
72
+ end
73
+
74
+ def where(search_name = 'search')
75
+ Table.new(
76
+ ary: @table.select { |row| yield(row) },
77
+ cols: self.cols,
78
+ name: search_name
79
+ )
80
+ end
81
+
82
+ def join(table, opts = {})
83
+ col1 = opts[:col1] ; idx1 = self.send(col1)
84
+ col2 = opts[:col2] ; idx2 = self.send(col2)
85
+ cols = ([col1] + (self.cols.keys - [col1])
86
+ .map { |k| "#{k}_#{self.table_name}".to_sym } +
87
+ ((table.cols.keys - [col1])
88
+ .map { |k| "#{k}_#{table.table_name}".to_sym }))
89
+ .map.with_index {|head,idx| [head.to_sym,idx] }.to_h
90
+
91
+ joined = Table.new(ary: [], cols: cols, name: "#{self.table_name}.#{cols[:col1]} join #{table.table_name}.#{cols[:col2]}")
92
+ ids = (self.pluck(col1) + table.pluck(col2)).uniq
93
+ ids.each do |id|
94
+ self.where { |row| row[idx1] == id }.table.each do |outer_row|
95
+ table.where { |row| row[idx2] == id }.table.each do |inner_row|
96
+ inner_row.delete_at(idx2)
97
+ joined.add(outer_row + inner_row)
98
+ end
99
+ end
100
+ end
101
+ return joined
61
102
  end
62
103
 
63
104
  def find(idx = nil)
@@ -1,3 +1,3 @@
1
1
  module Csvdb
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Walker
@@ -81,7 +81,6 @@ files:
81
81
  - Rakefile
82
82
  - bin/console
83
83
  - bin/setup
84
- - csvdb-0.1.0.gem
85
84
  - csvdb.gemspec
86
85
  - lib/csvdb.rb
87
86
  - lib/csvdb/errors.rb
Binary file