smuggle 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +82 -13
- data/lib/generators/smuggle/exporter/USAGE +14 -0
- data/lib/generators/smuggle/exporter/exporter_generator.rb +13 -0
- data/lib/generators/{templates/exporters → smuggle/exporter/templates}/exporter.rb +1 -1
- data/lib/generators/smuggle/install/USAGE +3 -0
- data/lib/generators/smuggle/install/install_generator.rb +11 -0
- data/lib/generators/{templates/exporters → smuggle/install/templates}/application_exporter.rb +0 -0
- data/lib/smuggle/version.rb +1 -1
- metadata +9 -6
- data/lib/generators/smuggle/smuggle_generator.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b542958f709628b6a1d1fce332e11074c8e00a4c
|
4
|
+
data.tar.gz: 8df40c91ed11cd51f8e5b526a31f630cff4a7d9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 187563335fa1100e557bdbbddc7c155f82f755746f546e435d49ac55053ae5e60ce1d8b97cd8e6112eefad5daed8ea6fa584f4791b76e99706a5d7625d310c9e
|
7
|
+
data.tar.gz: 11ffc896106586065d7673f3f31dc6c9c4b4c5f60735104d507cc8f70b433d2c3806af62592f4446143ee574e5e8da7f6e6f17df9bb1c4d1f74d6ed17bc6919a
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Smuggle
|
2
2
|
|
3
|
-
|
3
|
+
Is a gem to manage exports with ease, separating the logic from the models, resulting in a much cleaner codebase. Easy to use, with familiar structure.
|
4
4
|
|
5
|
-
|
5
|
+
**Smuggle is not dependent on Rails**, you can use it on ActiveRecord models, as well as plain ruby objects and hashes.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -14,30 +14,99 @@ gem 'smuggle'
|
|
14
14
|
|
15
15
|
And then execute:
|
16
16
|
|
17
|
-
|
17
|
+
```
|
18
|
+
$ bundle
|
19
|
+
```
|
18
20
|
|
19
21
|
Or install it yourself as:
|
20
22
|
|
21
|
-
|
23
|
+
```
|
24
|
+
$ gem install smuggle
|
25
|
+
```
|
26
|
+
|
27
|
+
To generate the base exporter run:
|
28
|
+
|
29
|
+
```
|
30
|
+
$ rails g smuggle:install
|
31
|
+
create app/exporters/application_exporter.rb
|
32
|
+
```
|
33
|
+
|
34
|
+
To geneate an exporter, you can run the following command:
|
35
|
+
|
36
|
+
```
|
37
|
+
$ rails g smuggle:exporter user
|
38
|
+
create app/exporters/user_exporter.rb
|
39
|
+
```
|
40
|
+
|
41
|
+
You can also include the attributes you wish to export by running:
|
42
|
+
|
43
|
+
```
|
44
|
+
$ rails g smuggle:exporter user email username created_at
|
45
|
+
create app/exporters/user_exporter.rb
|
46
|
+
```
|
47
|
+
|
48
|
+
## Example
|
49
|
+
|
50
|
+
Inside the `user_exporter.rb` file:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class UserExporter < ApplicationExporter
|
54
|
+
attributes :email, :username, :created_at
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
Extra logic can be establish inside the exporter file, using the same name as the attribute:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class UserExporter < ApplicationExporter
|
62
|
+
attributes :email, :username, :created_at
|
63
|
+
|
64
|
+
def created_at
|
65
|
+
super.created_at.strftime("%m/%d/%Y")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
If there are no attributes defined in the exporter, all the attributes of the ActiveModel record will be included.
|
71
|
+
If it is a hash, then all values will be included.
|
22
72
|
|
23
73
|
## Usage
|
24
74
|
|
25
|
-
|
75
|
+
Generate the csv in the desired export controller simply call:
|
26
76
|
|
27
|
-
|
77
|
+
```ruby
|
78
|
+
class User
|
79
|
+
attr_accessor :name
|
28
80
|
|
29
|
-
|
81
|
+
def initialize(name)
|
82
|
+
@name = name
|
83
|
+
end
|
84
|
+
end
|
30
85
|
|
31
|
-
|
86
|
+
class UserExporter < ApplicationExporter
|
87
|
+
attributes :name
|
88
|
+
end
|
89
|
+
|
90
|
+
users = [User.new('Rick Sanchez'), User.new('Morty Smith')]
|
91
|
+
|
92
|
+
Smuggle::Services::Export.call(scope: users, exporter: UserExporter)
|
93
|
+
# => "name\n" + "Rick Sanchez\n" + "Morty Smith\n"
|
94
|
+
```
|
95
|
+
|
96
|
+
Or if you are using active record, the exporter class will be automatically resolved from the scope:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
Smuggle::Services::Export.call(scope: User.all)
|
100
|
+
```
|
32
101
|
|
33
102
|
## Contributing
|
34
103
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pablocrivella/smuggle.
|
36
105
|
|
37
|
-
##
|
106
|
+
## Todo
|
38
107
|
|
39
|
-
|
108
|
+
- Implement `importer` functionality
|
40
109
|
|
41
|
-
##
|
110
|
+
## License
|
42
111
|
|
43
|
-
|
112
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Generates a exporter for a scope with the given name.
|
3
|
+
Additionally, you can pass the attributes you want to include in the export.
|
4
|
+
|
5
|
+
Example:
|
6
|
+
rails generate smuggle:exporter user first_name last_name
|
7
|
+
|
8
|
+
This will create:
|
9
|
+
|
10
|
+
# app/exporter/user_exporter.rb
|
11
|
+
|
12
|
+
class UserExporter < ApplicationExporter
|
13
|
+
attributes :first_name, :last_name
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Smuggle
|
2
|
+
module Generators
|
3
|
+
class ExporterGenerator < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
5
|
+
|
6
|
+
argument :attributes, type: :array, required: false
|
7
|
+
|
8
|
+
def create_exporter
|
9
|
+
template 'exporter.rb', File.join('app/exporters', class_path, "#{file_name}_exporter.rb")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Smuggle
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
5
|
+
|
6
|
+
def copy_application_exporter
|
7
|
+
template 'application_exporter.rb', 'app/exporters/application_exporter.rb'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/generators/{templates/exporters → smuggle/install/templates}/application_exporter.rb
RENAMED
File without changes
|
data/lib/smuggle/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smuggle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pablo Crivella
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -127,9 +127,12 @@ files:
|
|
127
127
|
- Rakefile
|
128
128
|
- bin/console
|
129
129
|
- bin/setup
|
130
|
-
- lib/generators/smuggle/
|
131
|
-
- lib/generators/
|
132
|
-
- lib/generators/templates/
|
130
|
+
- lib/generators/smuggle/exporter/USAGE
|
131
|
+
- lib/generators/smuggle/exporter/exporter_generator.rb
|
132
|
+
- lib/generators/smuggle/exporter/templates/exporter.rb
|
133
|
+
- lib/generators/smuggle/install/USAGE
|
134
|
+
- lib/generators/smuggle/install/install_generator.rb
|
135
|
+
- lib/generators/smuggle/install/templates/application_exporter.rb
|
133
136
|
- lib/smuggle.rb
|
134
137
|
- lib/smuggle/engine.rb
|
135
138
|
- lib/smuggle/exporter/base.rb
|
@@ -157,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
160
|
version: '0'
|
158
161
|
requirements: []
|
159
162
|
rubyforge_project:
|
160
|
-
rubygems_version: 2.6.
|
163
|
+
rubygems_version: 2.6.13
|
161
164
|
signing_key:
|
162
165
|
specification_version: 4
|
163
166
|
summary: It Exports stuff to CSV
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Smuggle
|
2
|
-
module Generators
|
3
|
-
class SmuggleGenerator < Rails::Generators::Base
|
4
|
-
namespace 'smuggle'
|
5
|
-
|
6
|
-
source_root File.expand_path('../../templates/exporters', __FILE__)
|
7
|
-
|
8
|
-
desc 'Generates an Exporter with the given NAME.' \
|
9
|
-
'Additionally, you can pass the attributes you want to include in the export'
|
10
|
-
|
11
|
-
argument :scope, required: true,
|
12
|
-
desc: 'The scope to create exporter for, e.g. users, comments'
|
13
|
-
|
14
|
-
argument :attributes, type: :array, required: false,
|
15
|
-
desc: 'Specify the attributes you want to export, these will also be the headers.'
|
16
|
-
|
17
|
-
def create_initializer_file
|
18
|
-
return if File.exist?('app/exporters/application_exporter.rb')
|
19
|
-
copy_file 'application_exporter.rb', 'app/exporters/application_exporter.rb'
|
20
|
-
end
|
21
|
-
|
22
|
-
def create_exporter
|
23
|
-
template 'exporter.rb', "app/exporters/#{scope.underscore}_exporter.rb"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|