csvdb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/csvdb.gemspec +27 -0
- data/lib/csvdb.rb +14 -0
- data/lib/csvdb/errors.rb +6 -0
- data/lib/csvdb/row.rb +62 -0
- data/lib/csvdb/table.rb +94 -0
- data/lib/csvdb/version.rb +3 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 78fd484e62c882e5772ea58b9e65f699653223d2
|
4
|
+
data.tar.gz: 3174d4df49aea83032b0a9922b4f1f4441ef595b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ebd3953cc471345f9f9f6d269578814823e845bc83c82c0924a07632abd8d468045c9e22061062ab0c97ad5f81c4eb4ec80e7dbb74de9453025b2e1918e83513
|
7
|
+
data.tar.gz: 4681c1ce9a8458e3bc25c81c25ab1263feb88a476809fabb64e8792158174d35cf65d8f7365a56f6864696179f3e8fdc29f70eeef4e9a584aff336bb83a49f75
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Csvdb
|
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
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'csvdb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install csvdb
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
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.
|
30
|
+
|
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
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/csvdb/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "csvdb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/csvdb.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csvdb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csvdb"
|
8
|
+
spec.version = Csvdb::VERSION
|
9
|
+
spec.licenses = ['MIT']
|
10
|
+
spec.authors = ["Colin Walker"]
|
11
|
+
spec.email = ["cjwalker@sfu.ca"]
|
12
|
+
|
13
|
+
spec.summary = %q{Object relational mapping for CSV files. Holds the entire database as a table of rows in memory.}
|
14
|
+
spec.homepage = "https://github.com/ColDog/csvdb"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_dependency 'terminal-table', '1.5.2'
|
25
|
+
spec.add_dependency 'stat_sugar', '1.0.0'
|
26
|
+
|
27
|
+
end
|
data/lib/csvdb.rb
ADDED
data/lib/csvdb/errors.rb
ADDED
data/lib/csvdb/row.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module Csvdb
|
2
|
+
class Row < Array
|
3
|
+
attr_accessor :row, :attrs, :table
|
4
|
+
|
5
|
+
def initialize(attrs, table, row)
|
6
|
+
@table = table
|
7
|
+
@row = row
|
8
|
+
if attrs.is_a? Hash
|
9
|
+
cols = @table.cols ; ary = []
|
10
|
+
attrs.each do |key, val|
|
11
|
+
ary[cols[key]] = convert(val)
|
12
|
+
add_attr(key, convert(val)) if @table.cols.keys.include?(key)
|
13
|
+
end
|
14
|
+
super(ary)
|
15
|
+
elsif attrs.is_a? Array
|
16
|
+
super(attrs.map { |att| convert(att) })
|
17
|
+
@table.cols.each { |col, idx| add_attr( col, convert(attrs[idx]) ) }
|
18
|
+
else
|
19
|
+
raise ParseError, "Cannot create a row out of a #{attrs.class}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def update(attrs)
|
24
|
+
cols = @table.cols
|
25
|
+
attrs.each do |att, new_val|
|
26
|
+
@table.table[@row][cols[att.to_sym]] = new_val
|
27
|
+
add_attr(att.to_sym, new_val) if @table.cols.keys.include?(att)
|
28
|
+
end
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete
|
33
|
+
@table.table[@row] = nil
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_hash
|
38
|
+
head = @table.cols.keys ; hash = {}
|
39
|
+
self.map.with_index { |a, i| hash[head[i]] = a }
|
40
|
+
hash
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def add_attr(name, value)
|
45
|
+
self.class.send(:attr_accessor, name)
|
46
|
+
instance_variable_set("@#{name}", value)
|
47
|
+
end
|
48
|
+
|
49
|
+
def convert(str)
|
50
|
+
begin
|
51
|
+
Integer(str)
|
52
|
+
rescue ArgumentError
|
53
|
+
begin
|
54
|
+
Float(str)
|
55
|
+
rescue ArgumentError
|
56
|
+
return str
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
data/lib/csvdb/table.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'csvdb/row'
|
3
|
+
require 'csvdb/errors'
|
4
|
+
|
5
|
+
module Csvdb
|
6
|
+
class Table
|
7
|
+
attr_accessor :cols, :table
|
8
|
+
|
9
|
+
def initialize(opts = {})
|
10
|
+
|
11
|
+
if opts[:file]
|
12
|
+
@file = opts[:file]
|
13
|
+
table = CSV.read(@file)
|
14
|
+
@cols = table[0].map.with_index {|head,idx| [head.to_sym,idx] }.to_h
|
15
|
+
table.delete_at(0)
|
16
|
+
elsif opts[:ary]
|
17
|
+
table = opts[:ary]
|
18
|
+
@cols = opts[:cols]
|
19
|
+
else
|
20
|
+
raise ParseError, 'No table or file to parse.'
|
21
|
+
end
|
22
|
+
|
23
|
+
@cols.each do |col, val|
|
24
|
+
add_attr(col, val)
|
25
|
+
end
|
26
|
+
|
27
|
+
@table = []
|
28
|
+
table.each_with_index do |row, idx|
|
29
|
+
@table[idx] = Row.new(row, self, idx)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def write(file = @file)
|
35
|
+
CSV.open(file, 'wb') do |csv|
|
36
|
+
csv << @cols.keys
|
37
|
+
@table.each do |row|
|
38
|
+
csv << row
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def all
|
44
|
+
@table
|
45
|
+
end
|
46
|
+
|
47
|
+
def count
|
48
|
+
@table.count
|
49
|
+
end
|
50
|
+
|
51
|
+
def create(attrs)
|
52
|
+
new_row = []
|
53
|
+
attrs.each do |att, new_val|
|
54
|
+
new_row[@cols[att]] = new_val
|
55
|
+
end
|
56
|
+
@table << Row.new(new_row, self, @table.length)
|
57
|
+
end
|
58
|
+
|
59
|
+
def where
|
60
|
+
Table.new(ary: @table.select { |row| yield(row) }, cols: self.cols)
|
61
|
+
end
|
62
|
+
|
63
|
+
def find(idx = nil)
|
64
|
+
if block_given?
|
65
|
+
@table.select { |row| yield(row) }.first
|
66
|
+
elsif idx
|
67
|
+
@table[idx]
|
68
|
+
else
|
69
|
+
raise ArgumentError, 'Must pass either an index or a block.'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def pluck(header)
|
74
|
+
col = @cols[header.to_sym] ; vals = []
|
75
|
+
raise SearchError, "Column #{header} does not exist." unless col
|
76
|
+
@table.each do |row|
|
77
|
+
vals << row[col]
|
78
|
+
end
|
79
|
+
vals
|
80
|
+
end
|
81
|
+
|
82
|
+
def pretty
|
83
|
+
table = Terminal::Table.new headings: @cols.keys, rows: @table
|
84
|
+
puts table
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
def add_attr(name, value)
|
89
|
+
self.class.send(:attr_accessor, name)
|
90
|
+
instance_variable_set("@#{name}", value)
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csvdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin Walker
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: terminal-table
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.5.2
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.5.2
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: stat_sugar
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.0.0
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- cjwalker@sfu.ca
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- csvdb.gemspec
|
85
|
+
- lib/csvdb.rb
|
86
|
+
- lib/csvdb/errors.rb
|
87
|
+
- lib/csvdb/row.rb
|
88
|
+
- lib/csvdb/table.rb
|
89
|
+
- lib/csvdb/version.rb
|
90
|
+
homepage: https://github.com/ColDog/csvdb
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.6
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Object relational mapping for CSV files. Holds the entire database as a table
|
114
|
+
of rows in memory.
|
115
|
+
test_files: []
|