csv_object 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b7eee66bcb3733f70e9a9dda093cf2a3dfc349ce80232d29abab1b1b8035e02e
4
+ data.tar.gz: a1d05b2e15b82388f6cccc65c83b370f71d30dc7fafa3a99df8a33a832c37383
5
+ SHA512:
6
+ metadata.gz: '0834a75c0fd1043c39561667e8bca5760ee8c608aced690e5eadb2b3a5ea881f8fb8a8372d5b09a8b00dfcd430442215e3e197a627a4dbb736da3d5ca26f3d22'
7
+ data.tar.gz: f6fb567589b520041ac30ee71b6141d7629ab7b017307371d49a37c0ad96b8e3ec5fd75220a3f7e93a42ca56a59eb098413e20256ea27033f795eafff1e508ef
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in csv_object.gemspec
4
+ gemspec
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ csv_object (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ method_source (0.9.2)
11
+ minitest (5.13.0)
12
+ minitest-power_assert (0.3.0)
13
+ minitest
14
+ power_assert (>= 1.1)
15
+ power_assert (1.1.5)
16
+ pry (0.12.2)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.9.0)
19
+ rake (10.5.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 2.0)
26
+ csv_object!
27
+ minitest (~> 5.0)
28
+ minitest-power_assert (~> 0.3)
29
+ pry (~> 0.12)
30
+ rake (~> 10.0)
31
+
32
+ BUNDLED WITH
33
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Ivan Nemytchenko
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.
@@ -0,0 +1,13 @@
1
+ c:
2
+ bin/console
3
+
4
+ test:
5
+ rake test
6
+
7
+ build:
8
+ gem build csv_object.gemspec
9
+
10
+ push:
11
+ gem push hola-0.0.0.gem
12
+
13
+ .PHONY: test
@@ -0,0 +1,71 @@
1
+ # CsvObject
2
+
3
+ Ever found yourself trying to find the right way to use Ruby's `CSV` library? Confused about all those `CSV.parse` and `CSV.open`, and when to use each of them?
4
+
5
+ The purpose of CsvObject gem is to create convenient interface to work with CSV files and data in Ruby. Whatever you have: string with filename, CSV data itself, `Pathname`, `CSV::Table`, array of hashes, or even `Paperclip::Attachment`, just throw it into `CsvObject.new(your_stuff)`
6
+
7
+ 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/csv_object`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'csv_object'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install csv_object
24
+
25
+ ## Usage
26
+
27
+
28
+
29
+ Let's say we have `people.csv` file:
30
+
31
+ ```
32
+ Id,Name,Age,Sex
33
+ 1011,Mike,13,Male
34
+ 1012,Annette,27,Female
35
+ 1013,Zoran,22,Male
36
+ ```
37
+
38
+ We can do the following:
39
+
40
+ ```
41
+ csv_object = CsvObject.new('test/fixtures/people.csv')
42
+ # => #<CsvObject:0x00007fc91dcd78a8 @csv=#<CSV::Table mode:col_or_row row_count:4>>
43
+
44
+ csv_object.headers
45
+ # => ["Id", "Name", "Age", "Sex"]
46
+
47
+ csv_object.sort { |a, b| a[2].to_i <=> b[2].to_i }.map{|a| [a["Name"], a["Age"]]}
48
+ # => [["Mike", "13"], ["Zoran", "22"], ["Annette", "27"]]
49
+
50
+ subset = csv_object.subset(["1013","1011"], "Id")
51
+ # => #<CsvObject:0x00007fc91f472e40 @csv=#<CSV::Table mode:col_or_row row_count:3>>
52
+
53
+ subset.size # => 2
54
+ subset.to_csv # => "Id,Name,Age,Sex\n1011,Mike,13,Male\n1013,Zoran,22,Male\n"
55
+ subset.to_file('/tmp/subset.csv') # saves subset to file
56
+
57
+ ```
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ 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).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/inem/csv_object. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "csv_object"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "csv_object/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "csv_object"
7
+ spec.version = CsvObject::VERSION
8
+ spec.authors = ["Ivan Nemytchenko"]
9
+ spec.email = ["nemytchenko@gmail.com"]
10
+
11
+ spec.summary = %q{Wrapper around Ruby's CSV for more convenience}
12
+ # spec.description = %q{Wrapper around Ruby's CSV for more convenience}
13
+ spec.homepage = "https://github.com/inem/csv_object"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/inem/csv_object"
18
+ spec.metadata["changelog_uri"] = "https://github.com/inem/csv_object/releases"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 2.0"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ spec.add_development_dependency "minitest-power_assert", "~> 0.3"
33
+ spec.add_development_dependency "pry", "~> 0.12"
34
+ end
@@ -0,0 +1,103 @@
1
+ require 'csv'
2
+ require "csv_object/version"
3
+
4
+ class CsvObject
5
+ # class Error < StandardError; end
6
+
7
+ def self.generate(array_of_hashes)
8
+ CSV.generate do |csv|
9
+ csv << array_of_hashes.first.keys
10
+ array_of_hashes.each do |hash|
11
+ csv << hash.values
12
+ end
13
+ end
14
+ end
15
+
16
+ def self.wrap(content)
17
+ case content
18
+ when CSV::Table
19
+ content
20
+ when String
21
+ if content =~ /\n/
22
+ content
23
+ elsif FileTest.exist?(content)
24
+ wrap(Pathname.new(content))
25
+ else
26
+ content
27
+ end
28
+ when Pathname
29
+ IO.read(content)
30
+ when Array # array of hashes
31
+ CsvObject.generate(content)
32
+ when Paperclip::Attachment
33
+ Paperclip.io_adapters.for(content).read # https://stackoverflow.com/questions/6555468
34
+ when CsvObject
35
+ content.to_csv
36
+ else
37
+ raise ArgumentError, "unexpected object class '#{content.class}'"
38
+ end
39
+ end
40
+
41
+ def initialize(content)
42
+ @csv = CSV.parse(CsvObject.wrap(content), headers: true)
43
+ end
44
+
45
+ def to_a
46
+ @csv
47
+ end
48
+
49
+ def sort(&block)
50
+ to_hash @csv.sort(&block)
51
+ end
52
+
53
+ def to_h
54
+ to_hash @csv
55
+ end
56
+
57
+ def headers
58
+ @headers ||= @csv.headers
59
+ end
60
+
61
+ def to_csv
62
+ @csv.to_csv
63
+ end
64
+
65
+ def to_s
66
+ to_csv
67
+ end
68
+
69
+ def subset(ids, id_column_name)
70
+ id_column_index = headers.index(id_column_name)
71
+
72
+ tmp = CSV.generate(headers: true) do |row|
73
+ row << headers
74
+
75
+ to_a.each do |hash|
76
+ id_value = hash[id_column_index]
77
+ row << hash if ids.include?(id_value)
78
+ end
79
+ end
80
+
81
+ CsvObject.new(tmp)
82
+ end
83
+
84
+ def to_file(path)
85
+ CSV.open(path, 'wb') do |file|
86
+ file << headers
87
+ to_h.each do |hash|
88
+ file << hash.values_at(*headers)
89
+ end
90
+ end
91
+ end
92
+
93
+ def size
94
+ to_h.size
95
+ end
96
+
97
+ private
98
+
99
+ def to_hash(collection)
100
+ hash = collection.map(&:to_hash)
101
+ defined?(ActiveSupport) ? hash.map(&:with_indifferent_access) : hash
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ class CsvObject
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Nemytchenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-power_assert
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.12'
83
+ description:
84
+ email:
85
+ - nemytchenko@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - LICENSE.txt
93
+ - Makefile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - csv_object.gemspec
99
+ - lib/csv_object.rb
100
+ - lib/csv_object/version.rb
101
+ homepage: https://github.com/inem/csv_object
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ homepage_uri: https://github.com/inem/csv_object
106
+ source_code_uri: https://github.com/inem/csv_object
107
+ changelog_uri: https://github.com/inem/csv_object/releases
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubygems_version: 3.0.3
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Wrapper around Ruby's CSV for more convenience
127
+ test_files: []