lp_csv_exportable 0.1.5
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 +7 -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 +55 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/lp_csv_exportable/can_export_as_csv.rb +78 -0
- data/lib/lp_csv_exportable/csv_column.rb +24 -0
- data/lib/lp_csv_exportable/version.rb +3 -0
- data/lib/lp_csv_exportable.rb +8 -0
- data/lp_csv_exportable-0.1.2.gem +0 -0
- data/lp_csv_exportable-0.1.3.gem +0 -0
- data/lp_csv_exportable-0.1.4.gem +0 -0
- data/lp_csv_exportable.gemspec +36 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3805b8cffef76f6642c1a073d98efa651b1a3d07
|
4
|
+
data.tar.gz: 07b0b866d5857e622d31045f180e06a97077dd9b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 592194d4195a038db32d718b997dde273ff62a171237abc1f73a24cb2db60dba14105877ef574a87f3735e0f6860bb42c40f4751353a5effa833c343e8c3c921
|
7
|
+
data.tar.gz: a6370821f0dffab8b52cb9f3276e1b3acc62efa455ca55a0cd1f1ba6c9490b56a132259ad47ef128916466f057c1a2a8423be498acb163ceee0acce8a40b9a09
|
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) 2017 Ryan Francis
|
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,55 @@
|
|
1
|
+
LpCSVExportable
|
2
|
+
===
|
3
|
+
|
4
|
+
What is LpCSVExportable?
|
5
|
+
---
|
6
|
+
|
7
|
+
LpCSVExportable makes it really easy to export your data into a CSV. It is easy to implement, but also highly extendable.
|
8
|
+
|
9
|
+
Installation
|
10
|
+
---
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'lp_csv_exportable'
|
14
|
+
```
|
15
|
+
|
16
|
+
```
|
17
|
+
bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
Basic Usage
|
21
|
+
---
|
22
|
+
|
23
|
+
Create a class that includes the module `LpCSVExportable::CanExportAsCSV`
|
24
|
+
|
25
|
+
```
|
26
|
+
class ExportUsers
|
27
|
+
include LpCSVExportable::CanExportAsCSV
|
28
|
+
|
29
|
+
column 'First Name', model_method: :first_name
|
30
|
+
column 'Last Name', model_method: :last_name
|
31
|
+
column 'Email', model_method: :email
|
32
|
+
column 'Role', model_methods: %i[membership name]
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
And then to export, simply instantiate your class and pass in your `collection`, then call `to_csv`. For example, in a Rails controller action, you would do:
|
37
|
+
|
38
|
+
```
|
39
|
+
def index
|
40
|
+
users = User.all
|
41
|
+
|
42
|
+
respond_to do |format|
|
43
|
+
format.csv do
|
44
|
+
export = ExportUsers.new(collection: attendees)
|
45
|
+
send_data export.to_csv
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
TODO
|
52
|
+
---
|
53
|
+
|
54
|
+
- Readme: More complex examples
|
55
|
+
- Readme: Ways to extend
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "lp_csv_exportable"
|
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
@@ -0,0 +1,78 @@
|
|
1
|
+
module LpCSVExportable
|
2
|
+
module CanExportAsCSV
|
3
|
+
attr_reader :collection
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(args = {})
|
10
|
+
@collection = args[:collection]
|
11
|
+
after_init(args)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_csv
|
15
|
+
CSV.generate do |csv|
|
16
|
+
csv << headers
|
17
|
+
data_matrix.each do |row|
|
18
|
+
csv << row
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def after_init(args = {})
|
26
|
+
# hook
|
27
|
+
end
|
28
|
+
|
29
|
+
def columns
|
30
|
+
self.class.columns_hashes.map do |hash|
|
31
|
+
CSVColumn.new(hash)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def headers
|
36
|
+
columns.map(&:header)
|
37
|
+
end
|
38
|
+
|
39
|
+
def data_matrix
|
40
|
+
# array of arrays
|
41
|
+
# i.e. rows 2+ of CSV
|
42
|
+
collection.map do |obj|
|
43
|
+
columns.map do |column|
|
44
|
+
column.format(retrieve_value(obj, column))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def retrieve_value(obj, column)
|
50
|
+
# only one method to retrieve value
|
51
|
+
return call_model_method(column.model_method, obj) if column.model_method.present?
|
52
|
+
# chain methods to retrieve value (jsonb store)
|
53
|
+
# i.e. employee.market_data.min
|
54
|
+
column.model_methods.inject(obj) do |memo, model_method|
|
55
|
+
call_model_method(model_method, memo)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def call_model_method(model_method, memo)
|
60
|
+
if respond_to?(model_method)
|
61
|
+
send(model_method, memo)
|
62
|
+
else
|
63
|
+
memo.try(:send, model_method)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Configuration...
|
68
|
+
module ClassMethods
|
69
|
+
def column(header, options = {})
|
70
|
+
columns_hashes << { header: header }.merge(options)
|
71
|
+
end
|
72
|
+
|
73
|
+
def columns_hashes
|
74
|
+
@columns_hashes ||= []
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module LpCSVExportable
|
2
|
+
class CSVColumn
|
3
|
+
attr_reader :header, :model_method, :model_methods, :type
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
@header = args[:header]
|
7
|
+
@model_method = args[:model_method]
|
8
|
+
@model_methods = args[:model_methods]
|
9
|
+
@type = args.fetch(:type, :string)
|
10
|
+
after_init(args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def format(result)
|
14
|
+
return send(type, result) if respond_to?(type)
|
15
|
+
result
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def after_init(args = {})
|
21
|
+
# hook for subclasses
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lp_csv_exportable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'lp_csv_exportable'
|
8
|
+
spec.version = LpCSVExportable::VERSION
|
9
|
+
spec.authors = ['Ryan Francis']
|
10
|
+
spec.email = ['ryan@launchpadlab.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Easily export data to CSV}
|
13
|
+
spec.description = %q{Export from your Rails app to CSV in no time.}
|
14
|
+
spec.homepage = 'http://github.com/launchpadlab/lp_csv_exportable'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
23
|
+
'public gem pushes.'
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = 'exe'
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ['lib']
|
32
|
+
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lp_csv_exportable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Francis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-22 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
|
+
description: Export from your Rails app to CSV in no time.
|
56
|
+
email:
|
57
|
+
- ryan@launchpadlab.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/lp_csv_exportable.rb
|
72
|
+
- lib/lp_csv_exportable/can_export_as_csv.rb
|
73
|
+
- lib/lp_csv_exportable/csv_column.rb
|
74
|
+
- lib/lp_csv_exportable/version.rb
|
75
|
+
- lp_csv_exportable-0.1.2.gem
|
76
|
+
- lp_csv_exportable-0.1.3.gem
|
77
|
+
- lp_csv_exportable-0.1.4.gem
|
78
|
+
- lp_csv_exportable.gemspec
|
79
|
+
homepage: http://github.com/launchpadlab/lp_csv_exportable
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata:
|
83
|
+
allowed_push_host: https://rubygems.org
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.1
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Easily export data to CSV
|
104
|
+
test_files: []
|