csv_orm 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/.DS_Store +0 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/csv_orm.gemspec +30 -0
- data/lib/csv_orm/ingestor.rb +24 -0
- data/lib/csv_orm/query.rb +39 -0
- data/lib/csv_orm/version.rb +3 -0
- data/lib/csv_orm.rb +9 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9bb9dcf9aaa806b3e787579a8c0d3d058ef95ac4
|
4
|
+
data.tar.gz: 1ea60c99b878d4b63de6ec17666e0ac90f309759
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d63c9a5774ec628d26fd88b823dfdb136efa7d4a875fbe69538fa79c46654fe125ea05e153c2889cd3b118f786bb5d6c076b30d17caba3e869d0a2fdcb645b2
|
7
|
+
data.tar.gz: d1ae0d13d08b6ccfbc89bf262cbc16eabacbf4d8fb55e9784ff73c7e2a304cecd78566d3d2e343e0c77e435a12dd0b73e4776bd3f490c4a42e44ae4e0f30e13b
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Mike Lerner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# CsvOrm
|
2
|
+
|
3
|
+
Sometimes you get a csv file and are asked to do some basic query type stuff with it. If you're like me and hate excel, and would rather pretend that this is in a db and you're using Activerecord as your ORM, then this gem will be of use to you.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'csv_orm'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install csv_orm
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
my_csv = CsvOrm::Ingestor.new('path/to/csv.csv')
|
25
|
+
my_data = CsvOrm::Query.new(my_csv)
|
26
|
+
```
|
27
|
+
|
28
|
+
Now you can do activerecord like queries on the dataset. Currently it supports 3 methods:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
#where_any({key: 'value', other_key: 'other_value'})
|
32
|
+
#where_all({key: 'value', other_key: 'other_value'})
|
33
|
+
#aggregate(:field1, :field2)
|
34
|
+
```
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# show users who have have any admin access
|
38
|
+
my_data.where_any({admin: true, super_admin: true})
|
39
|
+
|
40
|
+
# show users who are admin and named 'Mike'
|
41
|
+
my_data.where_all({admin: true, first_name: 'Mike'})
|
42
|
+
|
43
|
+
# give me a break down of orders by their delivery status for users named 'Mike'
|
44
|
+
my_data.where_all({first_name: 'Mike'}).aggregate(:delivery_status)
|
45
|
+
#=> {delivery_status: {placed: 10, processing: 22, shipped: 43, delivered: 8}}
|
46
|
+
```
|
47
|
+
|
48
|
+
Maybe more will come...
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
53
|
+
|
54
|
+
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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Mike Lerner/csv_orm.
|
59
|
+
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
64
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "csv_orm"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/csv_orm.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csv_orm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csv_orm"
|
8
|
+
spec.version = CsvOrm::VERSION
|
9
|
+
spec.authors = ["Mike Lerner"]
|
10
|
+
spec.email = ["cheeseandpepper@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A quick and dirty activerecord like orm for csv files.}
|
13
|
+
spec.description = %q{Because I hate excel, I'd rather query my csv files like I would query a db}
|
14
|
+
spec.homepage = "http://www.google.com"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency 'csv'
|
28
|
+
spec.add_development_dependency 'ostruct'
|
29
|
+
spec.add_development_dependency 'pry'
|
30
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CsvOrm
|
2
|
+
class Ingestor
|
3
|
+
attr_accessor :file, :path, :headers, :headers_defined, :data_set
|
4
|
+
|
5
|
+
def initialize(file_path)
|
6
|
+
@path = File.expand_path(file_path)
|
7
|
+
@file = File.open(path)
|
8
|
+
@headers = [] # will define in first iteration of loop
|
9
|
+
@headers_defined = false
|
10
|
+
@data_set = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse
|
14
|
+
CSV.parse(file) do |row|
|
15
|
+
unless @headers_defined
|
16
|
+
@headers = row.map {|header| header.gsub(' ', '_').downcase.to_sym }
|
17
|
+
end
|
18
|
+
@data_set << OpenStruct.new(Hash[headers.zip(row.map(&:to_s))]) if @headers_defined
|
19
|
+
@headers_defined = true
|
20
|
+
end
|
21
|
+
@data_set
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module CsvOrm
|
2
|
+
class Query
|
3
|
+
attr_reader :data
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@data = data
|
7
|
+
end
|
8
|
+
|
9
|
+
def size
|
10
|
+
@data.size
|
11
|
+
end
|
12
|
+
|
13
|
+
def aggregate(*fields)
|
14
|
+
agg = {}
|
15
|
+
@data.each do |row|
|
16
|
+
fields.each do |field|
|
17
|
+
agg[field] ||= {}
|
18
|
+
agg[field][row.send(field)] ||= 0
|
19
|
+
agg[field][row.send(field)] += 1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
agg
|
23
|
+
end
|
24
|
+
|
25
|
+
def where_any(attrs)
|
26
|
+
expression = build_expression('||', attrs)
|
27
|
+
self.class.new(@data.select {|row| eval(expression)});
|
28
|
+
end
|
29
|
+
|
30
|
+
def where_all(attrs)
|
31
|
+
expression = build_expression('&&', attrs)
|
32
|
+
self.class.new(@data.select {|row| eval(expression)});
|
33
|
+
end
|
34
|
+
|
35
|
+
def build_expression(conditional, attrs)
|
36
|
+
attrs.each_with_object([]) { |(k,v), array| array << "row.send(:#{k}) == '#{v}'" }.join(" #{conditional} ")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/csv_orm.rb
ADDED
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_orm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Lerner
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-24 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: csv
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ostruct
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Because I hate excel, I'd rather query my csv files like I would query
|
98
|
+
a db
|
99
|
+
email:
|
100
|
+
- cheeseandpepper@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".DS_Store"
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- ".travis.yml"
|
109
|
+
- Gemfile
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- csv_orm.gemspec
|
116
|
+
- lib/csv_orm.rb
|
117
|
+
- lib/csv_orm/ingestor.rb
|
118
|
+
- lib/csv_orm/query.rb
|
119
|
+
- lib/csv_orm/version.rb
|
120
|
+
homepage: http://www.google.com
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.5.1
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: A quick and dirty activerecord like orm for csv files.
|
144
|
+
test_files: []
|