export_data 0.2.0 → 0.3.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 +4 -4
- data/README.md +29 -17
- data/lib/export_data/version.rb +1 -1
- data/lib/export_data.rb +19 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86b436786e1d9b8b361706c377c622301a2d08592dc078ffa3466c0636cdb35c
|
4
|
+
data.tar.gz: 75be141bfb3c6bc41cedb5784a08e12169fd5e623a3edecfc0f61423237cad60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa13331c6cfeb321435b526cb52894bfc695cf2f58bb49025072b59cd33e6b7b96e4c6a1cfea0e8cea09de378df7a72cc146a7a22b060cabe4433a4ddd4442a0
|
7
|
+
data.tar.gz: 200e95402b53ad395af2075cfcffdbd53cf41cc5b724eeb1e9ee57e63e2eb9c39109d30ecddbb0be6858c16d921e065c1e8197606c60a82344a7de5d4bc23ab0
|
data/README.md
CHANGED
@@ -1,34 +1,46 @@
|
|
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
|
-
To use the DataExport::Exporter class, follow these steps
|
15
|
+
To use the DataExport::Exporter class, follow these steps
|
16
|
+
|
17
|
+
Assuming you have 50 `User` records in your database and your User model represents these records, you can follow these steps:
|
18
|
+
|
19
|
+
1) Open your Rails console:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
rails console
|
23
|
+
```
|
24
|
+
|
25
|
+
2) Retrieve the user records from your database:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
users = User.all
|
29
|
+
```
|
30
|
+
- This will fetch all the user records from your User model and store them in the users variable.
|
31
|
+
|
32
|
+
3) Specify the file path where the CSV file will be saved:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
file_path = 'users.csv'
|
36
|
+
```
|
37
|
+
|
38
|
+
4) Call the `DataExport::Exporter.export_to_csv` method to export the user records to the CSV file:
|
16
39
|
|
17
40
|
```ruby
|
18
|
-
|
19
|
-
|
20
|
-
# Prepare data
|
21
|
-
data = [
|
22
|
-
{ id: 1, name: 'John', age: 30 },
|
23
|
-
{ id: 2, name: 'Alice', age: 25 }
|
24
|
-
]
|
25
|
-
|
26
|
-
# Create an instance of the exporter
|
27
|
-
exporter = DataExport::Exporter.new
|
28
|
-
|
29
|
-
# Export data to a CSV file
|
30
|
-
exporter.export_to_csv(data, 'output.csv')
|
41
|
+
DataExport::Exporter.export_to_csv(users, file_path)
|
31
42
|
```
|
43
|
+
- This line exports the user records in the `users` variable to the specified CSV file.
|
32
44
|
|
33
45
|
## Development
|
34
46
|
|
@@ -42,4 +54,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/ronaka
|
|
42
54
|
|
43
55
|
## License
|
44
56
|
|
45
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/export_data/version.rb
CHANGED
data/lib/export_data.rb
CHANGED
@@ -45,5 +45,24 @@ module DataExport
|
|
45
45
|
puts "An error occurred while exporting to CSV: #{e.message}"
|
46
46
|
end
|
47
47
|
end
|
48
|
+
|
49
|
+
def self.export_to_text(objects, file_path)
|
50
|
+
begin
|
51
|
+
File.open(file_path, 'w') do |file|
|
52
|
+
if objects.empty?
|
53
|
+
puts 'No objects to export.' # Or handle this case as needed
|
54
|
+
return
|
55
|
+
end
|
56
|
+
|
57
|
+
# Write the object's attribute values as text lines
|
58
|
+
objects.each do |obj|
|
59
|
+
file.puts(obj.attributes.values.join("\t")) # Use '\t' as a delimiter for tab-separated values
|
60
|
+
end
|
61
|
+
end
|
62
|
+
rescue Errno::ENOENT => e
|
63
|
+
puts "An error occurred while exporting to text: #{e.message}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
48
67
|
end
|
49
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: export_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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-
|
11
|
+
date: 2023-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Simplify data import and export tasks in Rails applications.
|
14
14
|
email:
|