export_data 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86b436786e1d9b8b361706c377c622301a2d08592dc078ffa3466c0636cdb35c
4
- data.tar.gz: 75be141bfb3c6bc41cedb5784a08e12169fd5e623a3edecfc0f61423237cad60
3
+ metadata.gz: 1ab81156058b3dfacec423e23f5e77b25f8433d4e1bbe0276e5e6a2ce93aa38e
4
+ data.tar.gz: 71ee5b2efc5e84bacfb1f553c0184ac958d0c9bc44cdd1f620a4bd1eb0844752
5
5
  SHA512:
6
- metadata.gz: aa13331c6cfeb321435b526cb52894bfc695cf2f58bb49025072b59cd33e6b7b96e4c6a1cfea0e8cea09de378df7a72cc146a7a22b060cabe4433a4ddd4442a0
7
- data.tar.gz: 200e95402b53ad395af2075cfcffdbd53cf41cc5b724eeb1e9ee57e63e2eb9c39109d30ecddbb0be6858c16d921e065c1e8197606c60a82344a7de5d4bc23ab0
6
+ metadata.gz: 3deda7a2fbaa674968ab65cdeb461f5fdcea89582120d3dbde83b18668879538729ab1ece8f85cd0d81f9540fc91b9bb10a0cefb130d639f2619d4d4873ca05b
7
+ data.tar.gz: f0423c7c7f728a287b78f34f304a65f5fc8f46334c4f7607b4009aa45d3a25fae6e1ccdc2eaa0f1c9aadfc5eb5c7b23384093922575e943e76aa3dab6a16a0f4
data/README.md CHANGED
@@ -12,35 +12,59 @@ 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
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:
15
+ ### Exporting Data to CSV
26
16
 
27
17
  ```ruby
28
- users = User.all
18
+ require 'data_export_gem'
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 = DataExportGem::Exporter.new
28
+
29
+ # Export data to a CSV file
30
+ exporter.export_to_csv(data, 'output.csv')
29
31
  ```
30
- - This will fetch all the user records from your User model and store them in the users variable.
31
32
 
32
- 3) Specify the file path where the CSV file will be saved:
33
+ ### Exporting Data to XLSX
33
34
 
34
35
  ```ruby
35
- file_path = 'users.csv'
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')
36
49
  ```
37
50
 
38
- 4) Call the `DataExport::Exporter.export_to_csv` method to export the user records to the CSV file:
51
+ ### Exporting Data to Text
39
52
 
40
53
  ```ruby
41
- DataExport::Exporter.export_to_csv(users, file_path)
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')
42
67
  ```
43
- - This line exports the user records in the `users` variable to the specified CSV file.
44
68
 
45
69
  ## Development
46
70
 
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
- # spec.add_dependency "example-gem", "~> 1.0"
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DataExport
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
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 the first object in the
16
- # array defines the column names for the CSV file.
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|
@@ -46,6 +55,27 @@ module DataExport
46
55
  end
47
56
  end
48
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.
49
79
  def self.export_to_text(objects, file_path)
50
80
  begin
51
81
  File.open(file_path, 'w') do |file|
@@ -64,5 +94,50 @@ module DataExport
64
94
  end
65
95
  end
66
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
+
67
142
  end
68
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.3.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-05 00:00:00.000000000 Z
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: