xporter 0.1.0
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 +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/xporter.rb +27 -0
- data/lib/xporter/column.rb +22 -0
- data/lib/xporter/exporter.rb +22 -0
- data/lib/xporter/exporter/decorator.rb +64 -0
- data/lib/xporter/exporter/dsl.rb +23 -0
- data/lib/xporter/exporter/generator.rb +50 -0
- data/lib/xporter/exporter/settings.rb +21 -0
- data/lib/xporter/exporter/streaming.rb +23 -0
- data/lib/xporter/exporter/transform.rb +33 -0
- data/lib/xporter/file_streamer.rb +20 -0
- data/lib/xporter/version.rb +3 -0
- data/xporter.gemspec +45 -0
- metadata +143 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 207bcdfa2e4a758749cff89636cdcde8092a219150d9729444fb873b21459262
|
|
4
|
+
data.tar.gz: c68cc45192dddc076c6573a1770ad09b289525ac85a7243f19a50f4fff3a87df
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9daa4c62ccf54e35929fa43c0fe670ce395ce8d9c1fd75488d7f426124ec3d825bc97a5178ee3f61e28c19c0908917f6a513777e108a4103e377e4fff0527e1c
|
|
7
|
+
data.tar.gz: 03cb9d3a9ff8679784326f36279d75e461c6c14623c084ee911491b3e280b3d458a80576b14a95ca326d960aa31ed4b1cd4f14b78006d0b2a7dbe82de140003b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.5.3
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Changelog
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at matt@madebylotus.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
xporter (0.1.0)
|
|
5
|
+
activesupport (< 6)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
activesupport (5.2.2)
|
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
12
|
+
i18n (>= 0.7, < 2)
|
|
13
|
+
minitest (~> 5.1)
|
|
14
|
+
tzinfo (~> 1.1)
|
|
15
|
+
byebug (10.0.2)
|
|
16
|
+
concurrent-ruby (1.1.3)
|
|
17
|
+
diff-lcs (1.3)
|
|
18
|
+
i18n (1.2.0)
|
|
19
|
+
concurrent-ruby (~> 1.0)
|
|
20
|
+
minitest (5.11.3)
|
|
21
|
+
rake (10.5.0)
|
|
22
|
+
rspec (3.8.0)
|
|
23
|
+
rspec-core (~> 3.8.0)
|
|
24
|
+
rspec-expectations (~> 3.8.0)
|
|
25
|
+
rspec-mocks (~> 3.8.0)
|
|
26
|
+
rspec-core (3.8.0)
|
|
27
|
+
rspec-support (~> 3.8.0)
|
|
28
|
+
rspec-expectations (3.8.2)
|
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
30
|
+
rspec-support (~> 3.8.0)
|
|
31
|
+
rspec-mocks (3.8.0)
|
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
33
|
+
rspec-support (~> 3.8.0)
|
|
34
|
+
rspec-support (3.8.0)
|
|
35
|
+
thread_safe (0.3.6)
|
|
36
|
+
tzinfo (1.2.5)
|
|
37
|
+
thread_safe (~> 0.1)
|
|
38
|
+
|
|
39
|
+
PLATFORMS
|
|
40
|
+
ruby
|
|
41
|
+
|
|
42
|
+
DEPENDENCIES
|
|
43
|
+
bundler (~> 1.17)
|
|
44
|
+
byebug (< 11)
|
|
45
|
+
rake (~> 10.0)
|
|
46
|
+
rspec (~> 3.0)
|
|
47
|
+
xporter!
|
|
48
|
+
|
|
49
|
+
RUBY VERSION
|
|
50
|
+
ruby 2.5.3p105
|
|
51
|
+
|
|
52
|
+
BUNDLED WITH
|
|
53
|
+
1.17.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Matt Millsaps-Brewer
|
|
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,88 @@
|
|
|
1
|
+
# Xporter
|
|
2
|
+
ruby gem for DSL creating streaming CSV exports.
|
|
3
|
+
|
|
4
|
+
[ ](https://app.codeship.com/projects/319101)
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add this line to your application's Gemfile:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'xporter'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
And then execute:
|
|
15
|
+
|
|
16
|
+
$ bundle
|
|
17
|
+
|
|
18
|
+
Or install it yourself as:
|
|
19
|
+
|
|
20
|
+
$ gem install xporter
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
In most cases, you'll use this library in a rails app, to organize different exporters and optionally use Live Streaming from the controller to browser to begin streaming results straight from the database to the client.
|
|
24
|
+
|
|
25
|
+
### Define Exporter
|
|
26
|
+
To define an exporter, simply place a file in the `app/exporters` directory in your rails app (restarting app server if creating directory for the first time).
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
# app/exporters/admin_exporter.rb
|
|
30
|
+
class AdminExporter < Xporter::Exporter
|
|
31
|
+
model User
|
|
32
|
+
decorates AdminDecorator
|
|
33
|
+
|
|
34
|
+
column(:name)
|
|
35
|
+
column(:email)
|
|
36
|
+
column(:dynamic) { |record| record.object_id }
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Generate CSV String
|
|
41
|
+
With an exporter defined, you'll want to convert a collection of objects into a CSV string.
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
exporter = AdminExporter.new
|
|
45
|
+
exporter.generate(User.all)
|
|
46
|
+
# => "Name,Email,Dynamic\nJustin,justin@madebylotus.com,12345511\n"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
If provided an `ActiveRecord::Relation`, we'll fetch records lazily in batches from ActiveRecord.
|
|
50
|
+
|
|
51
|
+
### Stream CSV to Browser
|
|
52
|
+
With an exporter defined, you'll want to convert a collection of objects and stream to the browser immediately.
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class AdministratorsController < ApplicationController
|
|
56
|
+
include Xporter::FileStreamer
|
|
57
|
+
|
|
58
|
+
def index
|
|
59
|
+
@users = User.all
|
|
60
|
+
|
|
61
|
+
respond_to do |format|
|
|
62
|
+
format.html
|
|
63
|
+
format.csv do
|
|
64
|
+
stream_file "administrators-export-#{ SecureRandom.uuid }".parameterize, 'csv' do |stream|
|
|
65
|
+
AdminExporter.stream(@users, stream)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Development
|
|
73
|
+
|
|
74
|
+
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.
|
|
75
|
+
|
|
76
|
+
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).
|
|
77
|
+
|
|
78
|
+
## Contributing
|
|
79
|
+
|
|
80
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xporter. 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.
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
85
|
+
|
|
86
|
+
## Code of Conduct
|
|
87
|
+
|
|
88
|
+
Everyone interacting in the Xporter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/madebylotus/xporter/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "xporter"
|
|
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/xporter.rb
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require "csv"
|
|
2
|
+
require "active_support/core_ext/class/attribute"
|
|
3
|
+
require "active_support/core_ext/object/blank"
|
|
4
|
+
require "active_support/core_ext/string/inflections"
|
|
5
|
+
require "active_support/concern"
|
|
6
|
+
|
|
7
|
+
begin
|
|
8
|
+
require 'byebug'
|
|
9
|
+
|
|
10
|
+
# optional, will be used in decorators if available
|
|
11
|
+
require 'action_view'
|
|
12
|
+
require 'action_controller'
|
|
13
|
+
rescue LoadError
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
require "xporter/version"
|
|
17
|
+
require "xporter/column"
|
|
18
|
+
require "xporter/exporter"
|
|
19
|
+
|
|
20
|
+
if defined?(ActionController)
|
|
21
|
+
require "xporter/file_streamer"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
module Xporter
|
|
25
|
+
class Error < StandardError; end
|
|
26
|
+
# Your code goes here...
|
|
27
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
class Column
|
|
3
|
+
attr_reader :attribute_name, :title, :block
|
|
4
|
+
|
|
5
|
+
def initialize(attribute_name, title = nil, &block)
|
|
6
|
+
@attribute_name = attribute_name
|
|
7
|
+
@title = title
|
|
8
|
+
@block = block || ->(model){ model.public_send(attribute_name) }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def data(model)
|
|
12
|
+
block.call(model)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def title_from(resource_class)
|
|
16
|
+
return title if title.present?
|
|
17
|
+
return attribute_name.to_s.titleize unless resource_class.respond_to?(:human_attribute_name)
|
|
18
|
+
|
|
19
|
+
resource_class.human_attribute_name(attribute_name)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require "xporter/exporter/decorator"
|
|
2
|
+
require "xporter/exporter/dsl"
|
|
3
|
+
require "xporter/exporter/generator"
|
|
4
|
+
require "xporter/exporter/settings"
|
|
5
|
+
require "xporter/exporter/streaming"
|
|
6
|
+
require "xporter/exporter/transform"
|
|
7
|
+
|
|
8
|
+
module Xporter
|
|
9
|
+
class Exporter
|
|
10
|
+
include Decorator
|
|
11
|
+
include Settings
|
|
12
|
+
include Transform
|
|
13
|
+
include DSL
|
|
14
|
+
include Generator
|
|
15
|
+
include Streaming
|
|
16
|
+
|
|
17
|
+
# reset so that each subclass has it's own collection
|
|
18
|
+
def self.inherited(other)
|
|
19
|
+
other._columns = []
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
class Exporter
|
|
3
|
+
module Decorator
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
class_attribute :_decorates, instance_accessor: false
|
|
8
|
+
class_attribute :_decorator_class, instance_accessor: false
|
|
9
|
+
|
|
10
|
+
self._decorates = false
|
|
11
|
+
self._decorator_class = nil
|
|
12
|
+
|
|
13
|
+
private_class_method :decorates
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class_methods do
|
|
17
|
+
def decorates(boolean_or_class)
|
|
18
|
+
if boolean_or_class.nil? || boolean_or_class == false
|
|
19
|
+
self._decorates = false
|
|
20
|
+
self._decorator_class = nil
|
|
21
|
+
return
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
self._decorates = true
|
|
25
|
+
|
|
26
|
+
if boolean_or_class.is_a?(Class)
|
|
27
|
+
self._decorator_class = boolean_or_class
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def view_context
|
|
35
|
+
return unless defined?(ActionView)
|
|
36
|
+
|
|
37
|
+
@view_context ||= begin
|
|
38
|
+
ActionView::Base.new(ActionController::Base.view_paths).tap do |view|
|
|
39
|
+
view.class_eval do
|
|
40
|
+
include Rails.application.routes.url_helpers
|
|
41
|
+
include ApplicationHelper
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def decorator?
|
|
48
|
+
self.class._decorates == true
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def decorate(record)
|
|
52
|
+
if self.class._decorator_class.present?
|
|
53
|
+
instantiate_decorated_object(self.class._decorator_class, record)
|
|
54
|
+
else
|
|
55
|
+
record.decorate(view_context)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def instantiate_decorated_object(klass, record)
|
|
60
|
+
klass.new(record, view_context)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
class Exporter
|
|
3
|
+
module DSL
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
private_class_method :column,
|
|
8
|
+
:model,
|
|
9
|
+
:transform
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class_methods do
|
|
13
|
+
def column(attribute_name, title = nil, &block)
|
|
14
|
+
self._columns << Column.new(attribute_name, title, &block)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def model(resource_class)
|
|
18
|
+
self._resource_class = resource_class
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
class Exporter
|
|
3
|
+
module Generator
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
class_methods do
|
|
7
|
+
def generate(*args)
|
|
8
|
+
new.generate(*args)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def generate(collection)
|
|
13
|
+
@collection = collection
|
|
14
|
+
|
|
15
|
+
CSV.generate do |csv|
|
|
16
|
+
csv << headers
|
|
17
|
+
|
|
18
|
+
content.each do |row|
|
|
19
|
+
csv << row
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def headers
|
|
27
|
+
columns.map do |column|
|
|
28
|
+
column.title_from(self.class._resource_class)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def content
|
|
33
|
+
each_item.map do |record|
|
|
34
|
+
record = transform(record) if transform?
|
|
35
|
+
record = decorate(record) if decorator?
|
|
36
|
+
|
|
37
|
+
columns.map do |column|
|
|
38
|
+
column.data(record)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def each_item(&block)
|
|
44
|
+
return @collection.each.lazy unless @collection.respond_to?(:find_each)
|
|
45
|
+
|
|
46
|
+
@collection.find_each(batch_size: self.class._batch_size).lazy
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
class Exporter
|
|
3
|
+
module Settings
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
class_attribute :_columns, instance_accessor: false
|
|
8
|
+
class_attribute :_resource_class, instance_accessor: false
|
|
9
|
+
class_attribute :_record_transform, instance_accessor: false
|
|
10
|
+
class_attribute :_batch_size, instance_accessor: false
|
|
11
|
+
|
|
12
|
+
self._columns = []
|
|
13
|
+
self._batch_size = 500.freeze
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def columns
|
|
17
|
+
self.class._columns
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
class Exporter
|
|
3
|
+
module Streaming
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
class_methods do
|
|
7
|
+
def stream(*args)
|
|
8
|
+
new.stream(*args)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def stream(collection, stream)
|
|
13
|
+
stream.write CSV.generate_line(headers)
|
|
14
|
+
|
|
15
|
+
@collection = collection
|
|
16
|
+
|
|
17
|
+
content.each do |row|
|
|
18
|
+
stream.write CSV.generate_line(row)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
class Exporter
|
|
3
|
+
module Transform
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
class_attribute :_record_transform, instance_accessor: false
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class_methods do
|
|
11
|
+
def transform(&block)
|
|
12
|
+
raise 'Block must accept two arguments' unless block.arity == 2
|
|
13
|
+
|
|
14
|
+
self._record_transform = block
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def context=(options={})
|
|
19
|
+
@_record_context = options
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def transform(record)
|
|
25
|
+
self.class._record_transform.call(record, @_record_context)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def transform?
|
|
29
|
+
self.class._record_transform.present?
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Xporter
|
|
2
|
+
module FileStreamer
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
include ActionController::Live
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def stream_file(filename, extension, &block)
|
|
10
|
+
response.headers["Content-Type"] = "application/octet-stream"
|
|
11
|
+
response.headers["Content-Disposition"] = "attachment; filename=#{filename}.#{extension}"
|
|
12
|
+
|
|
13
|
+
begin
|
|
14
|
+
yield response.stream
|
|
15
|
+
ensure
|
|
16
|
+
response.stream.close
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
data/xporter.gemspec
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "xporter/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "xporter"
|
|
8
|
+
spec.version = Xporter::VERSION
|
|
9
|
+
spec.authors = ["Matt Millsaps-Brewer"]
|
|
10
|
+
spec.email = ["matt@madebylotus.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{ruby gem for DSL creating streaming CSV exports}
|
|
13
|
+
spec.description = %q{ruby gem for DSL creating streaming CSV exports}
|
|
14
|
+
spec.homepage = "https://github.com/madebylotus/xporter"
|
|
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
|
+
|
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
23
|
+
spec.metadata["source_code_uri"] = "https://github.com/madebylotus/xporter"
|
|
24
|
+
spec.metadata["changelog_uri"] = "https://github.com/madebylotus/xporter/blob/master/CHANGELOG.md"
|
|
25
|
+
else
|
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
27
|
+
"public gem pushes."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
34
|
+
end
|
|
35
|
+
spec.bindir = "exe"
|
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
37
|
+
spec.require_paths = ["lib"]
|
|
38
|
+
|
|
39
|
+
spec.add_dependency "activesupport", "< 6"
|
|
40
|
+
|
|
41
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
|
42
|
+
spec.add_development_dependency "byebug", "< 11"
|
|
43
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
44
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
45
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: xporter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Matt Millsaps-Brewer
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-12-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "<"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "<"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '6'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.17'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.17'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: byebug
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "<"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '11'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "<"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '11'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.0'
|
|
83
|
+
description: ruby gem for DSL creating streaming CSV exports
|
|
84
|
+
email:
|
|
85
|
+
- matt@madebylotus.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- ".gitignore"
|
|
91
|
+
- ".rspec"
|
|
92
|
+
- ".ruby-version"
|
|
93
|
+
- ".travis.yml"
|
|
94
|
+
- CHANGELOG.md
|
|
95
|
+
- CODE_OF_CONDUCT.md
|
|
96
|
+
- Gemfile
|
|
97
|
+
- Gemfile.lock
|
|
98
|
+
- LICENSE.txt
|
|
99
|
+
- README.md
|
|
100
|
+
- Rakefile
|
|
101
|
+
- bin/console
|
|
102
|
+
- bin/setup
|
|
103
|
+
- lib/xporter.rb
|
|
104
|
+
- lib/xporter/column.rb
|
|
105
|
+
- lib/xporter/exporter.rb
|
|
106
|
+
- lib/xporter/exporter/decorator.rb
|
|
107
|
+
- lib/xporter/exporter/dsl.rb
|
|
108
|
+
- lib/xporter/exporter/generator.rb
|
|
109
|
+
- lib/xporter/exporter/settings.rb
|
|
110
|
+
- lib/xporter/exporter/streaming.rb
|
|
111
|
+
- lib/xporter/exporter/transform.rb
|
|
112
|
+
- lib/xporter/file_streamer.rb
|
|
113
|
+
- lib/xporter/version.rb
|
|
114
|
+
- xporter.gemspec
|
|
115
|
+
homepage: https://github.com/madebylotus/xporter
|
|
116
|
+
licenses:
|
|
117
|
+
- MIT
|
|
118
|
+
metadata:
|
|
119
|
+
allowed_push_host: https://rubygems.org
|
|
120
|
+
homepage_uri: https://github.com/madebylotus/xporter
|
|
121
|
+
source_code_uri: https://github.com/madebylotus/xporter
|
|
122
|
+
changelog_uri: https://github.com/madebylotus/xporter/blob/master/CHANGELOG.md
|
|
123
|
+
post_install_message:
|
|
124
|
+
rdoc_options: []
|
|
125
|
+
require_paths:
|
|
126
|
+
- lib
|
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
|
+
requirements:
|
|
134
|
+
- - ">="
|
|
135
|
+
- !ruby/object:Gem::Version
|
|
136
|
+
version: '0'
|
|
137
|
+
requirements: []
|
|
138
|
+
rubyforge_project:
|
|
139
|
+
rubygems_version: 2.7.6
|
|
140
|
+
signing_key:
|
|
141
|
+
specification_version: 4
|
|
142
|
+
summary: ruby gem for DSL creating streaming CSV exports
|
|
143
|
+
test_files: []
|