export_data 0.2.0 → 0.4.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 +42 -6
- data/export_data.gemspec +8 -2
- data/lib/export_data/version.rb +1 -1
- data/lib/export_data.rb +98 -4
- metadata +88 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ab81156058b3dfacec423e23f5e77b25f8433d4e1bbe0276e5e6a2ce93aa38e
|
4
|
+
data.tar.gz: 71ee5b2efc5e84bacfb1f553c0184ac958d0c9bc44cdd1f620a4bd1eb0844752
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3deda7a2fbaa674968ab65cdeb461f5fdcea89582120d3dbde83b18668879538729ab1ece8f85cd0d81f9540fc91b9bb10a0cefb130d639f2619d4d4873ca05b
|
7
|
+
data.tar.gz: f0423c7c7f728a287b78f34f304a65f5fc8f46334c4f7607b4009aa45d3a25fae6e1ccdc2eaa0f1c9aadfc5eb5c7b23384093922575e943e76aa3dab6a16a0f4
|
data/README.md
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
#
|
1
|
+
# ExportData
|
2
2
|
|
3
3
|
This gem exports model data into different formats, such as CSV. To achieve so, it uses an export model that handles how the data will be shown
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
gem 'export_data'
|
8
|
+
gem 'export_data'
|
9
9
|
```
|
10
10
|
|
11
11
|
Then, run `bundle install` to install the gem.
|
12
12
|
|
13
13
|
## Usage
|
14
14
|
|
15
|
-
|
15
|
+
### Exporting Data to CSV
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
require '
|
18
|
+
require 'data_export_gem'
|
19
19
|
|
20
20
|
# Prepare data
|
21
21
|
data = [
|
@@ -24,12 +24,48 @@ To use the DataExport::Exporter class, follow these steps:
|
|
24
24
|
]
|
25
25
|
|
26
26
|
# Create an instance of the exporter
|
27
|
-
exporter =
|
27
|
+
exporter = DataExportGem::Exporter.new
|
28
28
|
|
29
29
|
# Export data to a CSV file
|
30
30
|
exporter.export_to_csv(data, 'output.csv')
|
31
31
|
```
|
32
32
|
|
33
|
+
### Exporting Data to XLSX
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'data_export_gem'
|
37
|
+
|
38
|
+
# Prepare data
|
39
|
+
data = [
|
40
|
+
{ id: 1, name: 'John', age: 30 },
|
41
|
+
{ id: 2, name: 'Alice', age: 25 }
|
42
|
+
]
|
43
|
+
|
44
|
+
# Create an instance of the exporter
|
45
|
+
exporter = DataExportGem::Exporter.new
|
46
|
+
|
47
|
+
# Export data to a XLS file
|
48
|
+
exporter.export_to_xls(data, 'output.xls')
|
49
|
+
```
|
50
|
+
|
51
|
+
### Exporting Data to Text
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
require 'data_export_gem'
|
55
|
+
|
56
|
+
# Prepare data
|
57
|
+
data = [
|
58
|
+
{ id: 1, name: 'John', age: 30 },
|
59
|
+
{ id: 2, name: 'Alice', age: 25 }
|
60
|
+
]
|
61
|
+
|
62
|
+
# Create an instance of the exporter
|
63
|
+
exporter = DataExportGem::Exporter.new
|
64
|
+
|
65
|
+
# Export data to a TXT file
|
66
|
+
exporter.export_to_csv(data, 'output.txt')
|
67
|
+
```
|
68
|
+
|
33
69
|
## Development
|
34
70
|
|
35
71
|
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.
|
@@ -42,4 +78,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/ronaka
|
|
42
78
|
|
43
79
|
## License
|
44
80
|
|
45
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
81
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/export_data.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.metadata["homepage_uri"] = spec.homepage
|
20
20
|
spec.metadata["source_code_uri"] = "https://github.com/ronakabhattrz/export_data"
|
21
21
|
spec.metadata["changelog_uri"] = "https://github.com/ronakabhattrz/export_data/blob/main/CHANGELOG.md"
|
22
|
+
spec.metadata["documentation_uri"] = "https://www.rubydoc.info/gems/export_data/#{DataExport::VERSION}"
|
22
23
|
|
23
24
|
# Specify which files should be added to the gem when it is released.
|
24
25
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -28,8 +29,13 @@ Gem::Specification.new do |spec|
|
|
28
29
|
spec.require_paths = ["lib"]
|
29
30
|
|
30
31
|
# Uncomment to register a new dependency of your gem
|
31
|
-
|
32
|
-
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
spec.add_development_dependency "rubocop", "~> 1.0"
|
34
|
+
spec.add_development_dependency "yard", "~> 0.9"
|
35
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
36
|
+
spec.add_runtime_dependency 'axlsx', '~> 2.0'
|
37
|
+
spec.add_runtime_dependency 'roo', '~> 2.0'
|
38
|
+
|
33
39
|
# For more information and examples about making a new gem, check out our
|
34
40
|
# guide at: https://bundler.io/guides/creating_gem.html
|
35
41
|
end
|
data/lib/export_data/version.rb
CHANGED
data/lib/export_data.rb
CHANGED
@@ -12,17 +12,26 @@ module DataExport
|
|
12
12
|
# Exports data to a CSV file.
|
13
13
|
#
|
14
14
|
# This method takes an array of objects and exports them to a CSV file
|
15
|
-
# specified by the `file_path`. It expects that
|
16
|
-
#
|
15
|
+
# specified by the `file_path`. It expects that each object's `attributes`
|
16
|
+
# method returns a hash of attribute names and values. The method first
|
17
|
+
# writes the header row using the attribute names from the first object,
|
18
|
+
# and then writes the data rows using the attribute values of each object.
|
17
19
|
#
|
18
|
-
# @param objects [Array] The objects to export to CSV.
|
20
|
+
# @param objects [Array] The objects to export to a CSV file.
|
19
21
|
# @param file_path [String] The path to the CSV file to be created.
|
20
22
|
#
|
21
|
-
# @example Export user data to CSV:
|
23
|
+
# @example Export user data to a CSV file:
|
22
24
|
# objects = [user1, user2]
|
23
25
|
# DataExport::Exporter.export_to_csv(objects, 'users.csv')
|
24
26
|
#
|
25
27
|
# @return [void]
|
28
|
+
#
|
29
|
+
# @note If the `objects` array is empty, this method will output a message and
|
30
|
+
# not create a file.
|
31
|
+
# @raise [CSV::MalformedCSVError] If an error occurs while writing to the CSV file,
|
32
|
+
# an error message will be displayed.
|
33
|
+
# @raise [Errno::ENOENT] If an error occurs while creating or writing to the file,
|
34
|
+
# an error message will be displayed.
|
26
35
|
def self.export_to_csv(objects, file_path)
|
27
36
|
begin
|
28
37
|
CSV.open(file_path, 'w') do |csv|
|
@@ -45,5 +54,90 @@ module DataExport
|
|
45
54
|
puts "An error occurred while exporting to CSV: #{e.message}"
|
46
55
|
end
|
47
56
|
end
|
57
|
+
|
58
|
+
# Exports data to a text file.
|
59
|
+
#
|
60
|
+
# This method takes an array of objects and exports them to a text file
|
61
|
+
# specified by the `file_path`. It expects that each object's `attributes`
|
62
|
+
# method returns an array of attribute values. Each attribute value is joined
|
63
|
+
# using a tab character ('\t') as a delimiter to create tab-separated values
|
64
|
+
# in the text file.
|
65
|
+
#
|
66
|
+
# @param objects [Array] The objects to export to a text file.
|
67
|
+
# @param file_path [String] The path to the text file to be created.
|
68
|
+
#
|
69
|
+
# @example Export user data to a text file:
|
70
|
+
# objects = [user1, user2]
|
71
|
+
# DataExport::Exporter.export_to_text(objects, 'users.txt')
|
72
|
+
#
|
73
|
+
# @return [void]
|
74
|
+
#
|
75
|
+
# @note If the `objects` array is empty, this method will output a message and
|
76
|
+
# not create a file.
|
77
|
+
# @raise [Errno::ENOENT] If an error occurs while creating or writing to the file,
|
78
|
+
# an error message will be displayed.
|
79
|
+
def self.export_to_text(objects, file_path)
|
80
|
+
begin
|
81
|
+
File.open(file_path, 'w') do |file|
|
82
|
+
if objects.empty?
|
83
|
+
puts 'No objects to export.' # Or handle this case as needed
|
84
|
+
return
|
85
|
+
end
|
86
|
+
|
87
|
+
# Write the object's attribute values as text lines
|
88
|
+
objects.each do |obj|
|
89
|
+
file.puts(obj.attributes.values.join("\t")) # Use '\t' as a delimiter for tab-separated values
|
90
|
+
end
|
91
|
+
end
|
92
|
+
rescue Errno::ENOENT => e
|
93
|
+
puts "An error occurred while exporting to text: #{e.message}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# ...
|
98
|
+
|
99
|
+
# Exports data to an XLS (Excel) file.
|
100
|
+
#
|
101
|
+
# This method takes an array of objects and exports them to an XLS file
|
102
|
+
# specified by the `file_path`. It expects that each object's `attributes`
|
103
|
+
# method returns a hash of attribute names and values. The method creates an
|
104
|
+
# XLS workbook with a single worksheet and populates it with data from the
|
105
|
+
# objects.
|
106
|
+
#
|
107
|
+
# @param objects [Array] The objects to export to an XLS file.
|
108
|
+
# @param file_path [String] The path to the XLS file to be created.
|
109
|
+
#
|
110
|
+
# @example Export user data to an XLS file:
|
111
|
+
# objects = [user1, user2]
|
112
|
+
# DataExport::Exporter.export_to_xls(objects, 'users.xlsx')
|
113
|
+
#
|
114
|
+
# @return [void]
|
115
|
+
#
|
116
|
+
# @note If the `objects` array is empty, this method will create an empty XLS file.
|
117
|
+
# @raise [StandardError] If an error occurs while writing to the XLS file,
|
118
|
+
# an error message will be displayed.
|
119
|
+
def self.export_to_xls(objects, file_path)
|
120
|
+
begin
|
121
|
+
Axlsx::Package.new do |p|
|
122
|
+
p.workbook.add_worksheet(name: 'Data') do |sheet|
|
123
|
+
if objects.empty?
|
124
|
+
puts 'No objects to export.' # Or handle this case as needed
|
125
|
+
else
|
126
|
+
# Write the header row using the attribute names from the first object
|
127
|
+
sheet.add_row(objects.first.attributes.keys)
|
128
|
+
|
129
|
+
# Write the object's attribute values as data rows
|
130
|
+
objects.each do |obj|
|
131
|
+
sheet.add_row(obj.attributes.values)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
p.serialize(file_path)
|
136
|
+
end
|
137
|
+
rescue StandardError => e
|
138
|
+
puts "An error occurred while exporting to XLS: #{e.message}"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
48
142
|
end
|
49
143
|
end
|
metadata
CHANGED
@@ -1,15 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: export_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RONAK BHATT
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
12
|
-
dependencies:
|
11
|
+
date: 2023-09-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: axlsx
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: roo
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.0'
|
13
97
|
description: Simplify data import and export tasks in Rails applications.
|
14
98
|
email:
|
15
99
|
- ronakabhattrz@gmail.com
|
@@ -36,6 +120,7 @@ metadata:
|
|
36
120
|
homepage_uri: https://github.com/ronakabhattrz/export_data
|
37
121
|
source_code_uri: https://github.com/ronakabhattrz/export_data
|
38
122
|
changelog_uri: https://github.com/ronakabhattrz/export_data/blob/main/CHANGELOG.md
|
123
|
+
documentation_uri: https://www.rubydoc.info/gems/export_data/0.4.0
|
39
124
|
post_install_message:
|
40
125
|
rdoc_options: []
|
41
126
|
require_paths:
|