to_deep_csv 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 +8 -0
- data/.rubocop.yml +10 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +41 -0
- data/README.md +50 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/to_deep_csv.rb +3 -0
- data/lib/to_deep_csv/active_record/relation.rb +5 -0
- data/lib/to_deep_csv/array.rb +33 -0
- data/lib/to_deep_csv/version.rb +3 -0
- data/to_deep_csv.gemspec +26 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aab9faac7ad0c7bec0dc0de01777c80746d4a13b3076562d9fe0e4fef409b92c
|
4
|
+
data.tar.gz: 99816f4bfccaf2400a6047658e5381716114e4565e9d2ea895d98bf970359d1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d7b80cc835f276aa51e3903c18b74786f76160d7f12a6d44ed19d23d25831d85bb46a39d717b4f3b05f88c2ec2e6fc85b31865c18f479f7ef2f624b09e7534c
|
7
|
+
data.tar.gz: 3163e48d617234e192a59c5e68d420f2be36b045980e39fa22f816e48feb558a8994a62daa70b4e6797f5c0ce9ef5bf118fa20195e9c7bd9b6dd97afacf1e698
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
to_deep_csv (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
onkcop (0.53.0.0)
|
11
|
+
rubocop (~> 0.53.0)
|
12
|
+
rubocop-rspec (>= 1.24.0)
|
13
|
+
parallel (1.12.1)
|
14
|
+
parser (2.5.1.0)
|
15
|
+
ast (~> 2.4.0)
|
16
|
+
powerpack (0.1.2)
|
17
|
+
rainbow (3.0.0)
|
18
|
+
rake (10.5.0)
|
19
|
+
rubocop (0.53.0)
|
20
|
+
parallel (~> 1.10)
|
21
|
+
parser (>= 2.5)
|
22
|
+
powerpack (~> 0.1)
|
23
|
+
rainbow (>= 2.2.2, < 4.0)
|
24
|
+
ruby-progressbar (~> 1.7)
|
25
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
26
|
+
rubocop-rspec (1.26.0)
|
27
|
+
rubocop (>= 0.53.0)
|
28
|
+
ruby-progressbar (1.9.0)
|
29
|
+
unicode-display_width (1.4.0)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
bundler (~> 1.16)
|
36
|
+
onkcop
|
37
|
+
rake (~> 10.0)
|
38
|
+
to_deep_csv!
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
1.16.1
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# to_deep_csv
|
2
|
+
|
3
|
+
to_csv for `Array<Array>`, `Array<Hash>`, `Array<ActiveRecord::Base>`, `ActiveRecord::Relation` etc.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'to_deep_csv'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install to_deep_csv
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require "to_deep_csv"
|
25
|
+
|
26
|
+
[{a: 1, b: 2}].to_deep_csv(headers: true)
|
27
|
+
[[1, 2]].to_deep_csv
|
28
|
+
[[1, 2]].to_deep_csv(headers: true, columns: [:a, :b])
|
29
|
+
{:a => 1}.to_a.to_deep_csv
|
30
|
+
|
31
|
+
# for ActiveRecord
|
32
|
+
Foo.all.to_deep_csv(headers: true)
|
33
|
+
Foo.all.to_deep_csv(columns: [:id, :name])
|
34
|
+
Foo.all.select(:id, :name).to_deep_csv
|
35
|
+
Foo.all.group(:type).count.to_a.to_deep_csv(headers: true, columns: [:type, :count])
|
36
|
+
```
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
41
|
+
|
42
|
+
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).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Narazaka/to_deep_csv.
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
This is released under The [Zlib License](https://narazaka.net/license/Zlib?2018)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "to_deep_csv"
|
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/lib/to_deep_csv.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
class Array
|
2
|
+
# to_csv for Array<Array>, Array<Hash>, Array<ActiveRecord::Base>, ActiveRecord::Relation etc.
|
3
|
+
#
|
4
|
+
# @param [Hash] options CSV options + alpha
|
5
|
+
# @option options [Array<Symbol>] column names
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# [{a: 1, b: 2}].to_deep_csv(headers: true)
|
9
|
+
# [[1, 2]].to_deep_csv
|
10
|
+
# [[1, 2]].to_deep_csv(headers: true, columns: [:a, :b])
|
11
|
+
# Foo.all.to_deep_csv(headers: true)
|
12
|
+
# Foo.all.to_deep_csv(columns: [:id, :name])
|
13
|
+
# Foo.all.select(:id, :name).to_deep_csv
|
14
|
+
# {:a => 1}.to_a.to_deep_csv
|
15
|
+
# Foo.all.group(:type).count.to_a.to_deep_csv(headers: true, columns: [:type, :count])
|
16
|
+
def to_deep_csv(options = {})
|
17
|
+
# true if Hash like
|
18
|
+
columns_by_first = self.first.try(:attributes).try(:keys) || self.first.try(:keys)
|
19
|
+
columns = options.delete(:columns) || columns_by_first
|
20
|
+
CSV.generate("", options) do |csv|
|
21
|
+
csv << columns if columns && options[:headers]
|
22
|
+
self.each do |record|
|
23
|
+
csv << if columns_by_first # Hash like
|
24
|
+
columns.map do |column|
|
25
|
+
Array(column).reduce(record) {|target, key| target.try(key) || target[key] }
|
26
|
+
end
|
27
|
+
else # Array like
|
28
|
+
record
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/to_deep_csv.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("lib", __dir__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "to_deep_csv/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "to_deep_csv"
|
8
|
+
spec.version = ToDeepCsv::VERSION
|
9
|
+
spec.licenses = ["Zlib"]
|
10
|
+
spec.authors = ["Narazaka"]
|
11
|
+
spec.email = ["info@narazaka.net"]
|
12
|
+
|
13
|
+
spec.summary = "to_csv for Array<Array>, Array<Hash>, Array<ActiveRecord::Base>, ActiveRecord::Relation etc."
|
14
|
+
spec.homepage = "https://github.com/Narazaka/to_deep_csv"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "onkcop"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_deep_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Narazaka
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-13 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: onkcop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- info@narazaka.net
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/to_deep_csv.rb
|
71
|
+
- lib/to_deep_csv/active_record/relation.rb
|
72
|
+
- lib/to_deep_csv/array.rb
|
73
|
+
- lib/to_deep_csv/version.rb
|
74
|
+
- to_deep_csv.gemspec
|
75
|
+
homepage: https://github.com/Narazaka/to_deep_csv
|
76
|
+
licenses:
|
77
|
+
- Zlib
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.7.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: to_csv for Array<Array>, Array<Hash>, Array<ActiveRecord::Base>, ActiveRecord::Relation
|
99
|
+
etc.
|
100
|
+
test_files: []
|