minimalist_ods 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/minimalist_ods.rb +72 -48
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 463a57f017a1b0a8761d32fc7a52d9a191a03750725489eb8e12b9e0a0d4cdca
4
- data.tar.gz: f3afeb53140f7e7ded4e96b786ec889adc55cbbd8644e20dbd00653d763cac62
3
+ metadata.gz: b24d9ff3c01e1adeac8c948f79e999e6af9a89c5b6976362e22011ad89d0c57a
4
+ data.tar.gz: cd9d65128da3b457baab953a3a845b6cc2f32c2cb9be5053790dbb94556d72a3
5
5
  SHA512:
6
- metadata.gz: 6954e7a37852a4c3b72b3422667755a557818dcc1aa80f07b448e60fe82d0e6e2ae42c418ff28f5b0c1f59e29275928c6ef47660734fc5955557d930c4351d56
7
- data.tar.gz: 45be33c3a46aa0b9f2ce7bfbba3dc237a2bb14fe8ae4cc3cdd125f68a5819c61c15f9817fd97ab352174d6937035db8c4bbbbff3be1975f4fec235400b7ab66e
6
+ metadata.gz: cb1fa28505949a88f5a9d5b91c38083af58ef89b9120469f1e1c987e710db037f5a1687d9022d116609aceee38409d9551da9ca39fd368fe1e91c87b1652775f
7
+ data.tar.gz: 4a7287bb2f07909192d4f2914cb1bc787aaf39c9446dca3a44ca8af3fec8a13a517b6401b76b672f59ec468928c3b7b6b454607565bfc24ace70b2b99b57ea8b
@@ -2,6 +2,7 @@
2
2
 
3
3
  # A Ruby Minimalist ODS
4
4
  require 'rubygems'
5
+ require 'stringio'
5
6
  require 'zip'
6
7
  require 'date'
7
8
 
@@ -94,43 +95,9 @@ class MinimalistODS
94
95
  </table:table-cell>
95
96
  XML
96
97
 
97
- TABLE_OPEN = 1
98
- TABLE_CLOSED = 0
99
-
100
- attr_reader :zip, :save_as, :creator, :buffer
101
-
102
- def initialize(save_as, creator = 'minimalist-ods')
103
- @save_as = save_as
104
- @creator = creator
105
- init_zip!
106
- init_mimetype!
107
- init_meta!
108
- init_manifest!
109
- init_content!
110
- end
111
-
112
- def init_zip!
113
- @zip = Zip::File.open(save_as, Zip::File::CREATE)
114
- end
115
-
116
- def init_mimetype!
117
- write_to_zip('mimetype', MIMETYPE)
118
- end
119
-
120
- def init_meta!
121
- meta = META_TEMPLATE.gsub(':CREATOR', creator).gsub(':TIME', Time.now.strftime('%Y-%m-%dT%H:%M:%S'))
122
- write_to_zip('meta.xml', meta)
123
- end
124
-
125
- def init_manifest!
126
- write_to_zip('META-INF/manifest.xml', MANIFEST_TEMPLATE)
127
- end
128
-
129
- def init_content!
130
- @buffer = @zip.get_output_stream('content.xml')
131
- @status = TABLE_CLOSED
132
- buffer.write(CONTENT_HEADER)
133
- end
98
+ TABLE_OPEN = 0
99
+ TABLE_CLOSED = 1
100
+ FILE_CLOSED = 2
134
101
 
135
102
  class MinimalistOODSError < StandardError
136
103
  end
@@ -153,16 +120,40 @@ class MinimalistODS
153
120
  end
154
121
  end
155
122
 
123
+ class FileClosed < MinimalistOODSError
124
+ def initialize
125
+ super('The file is now closed')
126
+ end
127
+ end
128
+
129
+ class FileNotClosed < MinimalistOODSError
130
+ def initialize
131
+ super('The file is still opened')
132
+ end
133
+ end
134
+
156
135
  class InvalidParameter < MinimalistOODSError
157
136
  end
158
137
 
138
+ attr_reader :zip_buffer, :content_buffer, :save_as, :save_to_disk, :creator
139
+
140
+ def initialize(save_as = nil, creator = 'minimalist-ods')
141
+ @save_as = save_as
142
+ @creator = creator
143
+ @save_to_disk = !(save_as.instance_of? StringIO)
144
+
145
+ init_content
146
+ end
147
+
159
148
  def open_table(table_name, cols_number)
149
+ raise FileClosed if @status == FILE_CLOSED
160
150
  raise TableAlreadyOpened if @status == TABLE_OPEN
161
151
  raise InvalidParameter, "Got invalid value `#{cols_number}' for table size" if cols_number.nil? || !cols_number.is_a?(Integer) || !cols_number.positive?
152
+
162
153
  @cols_number = cols_number
163
154
 
164
155
  table_header = TABLE_TEMPLATE.gsub(':NAME', table_name).gsub(':COL_NUMBER', cols_number.to_s)
165
- buffer.write(table_header)
156
+ content_buffer.write(table_header)
166
157
  @status = TABLE_OPEN
167
158
  end
168
159
 
@@ -170,29 +161,62 @@ class MinimalistODS
170
161
  raise InvalidRowLength.new(@cols_number, row.size) if row.size != @cols_number
171
162
 
172
163
  cells = row.map { |cell| cell_to_xml(cell) }.join
173
- buffer.write(ROW_TEMPLATE.gsub(':CELLS', cells))
164
+ content_buffer.write(ROW_TEMPLATE.gsub(':CELLS', cells))
174
165
  end
175
166
 
176
-
177
167
  def close_table
178
168
  raise TableNotOpened if @status == TABLE_CLOSED
179
169
 
180
- buffer.write('</table:table>')
170
+ content_buffer.write('</table:table>')
181
171
  @status = TABLE_CLOSED
182
172
  end
183
173
 
184
174
  def close_file
185
- buffer.write(CONTENT_FOOTER)
186
- buffer.close
187
- zip.close
175
+ raise TableAlreadyOpened if @status == TABLE_OPEN
176
+
177
+ content_buffer.write(CONTENT_FOOTER)
178
+ @status = FILE_CLOSED
179
+ create_zip
180
+ write_file!
188
181
  end
189
182
 
190
183
  private
191
184
 
192
- def write_to_zip(file_name, content)
193
- stream = zip.get_output_stream(file_name)
194
- stream.write(content)
195
- stream.close
185
+ def init_content
186
+ @content_buffer = StringIO.new
187
+ content_buffer.write(CONTENT_HEADER)
188
+ @status = TABLE_CLOSED
189
+ end
190
+
191
+ def format_meta
192
+ META_TEMPLATE.gsub(':CREATOR', creator).gsub(':TIME', Time.now.strftime('%Y-%m-%dT%H:%M:%S'))
193
+ end
194
+
195
+ def create_zip
196
+ @zip_buffer = Zip::OutputStream.write_buffer do |zip_handler|
197
+ zip_handler.put_next_entry('mimetype')
198
+ zip_handler.write(MIMETYPE)
199
+
200
+ zip_handler.put_next_entry('meta.xml')
201
+ zip_handler.write(format_meta)
202
+
203
+ zip_handler.put_next_entry('META-INF/manifest.xml')
204
+ zip_handler.write(MANIFEST_TEMPLATE)
205
+
206
+ zip_handler.put_next_entry('content.xml')
207
+ zip_handler.write(content_buffer.string)
208
+ end
209
+ end
210
+
211
+ def write_file!
212
+ if save_to_disk
213
+ File.open(save_as, 'wb') do |file|
214
+ file.write(zip_buffer.string)
215
+ end
216
+ else
217
+ save_as.write(zip_buffer.string)
218
+ save_as.rewind
219
+ end
196
220
  end
197
221
 
198
222
  def cell_to_xml(cell)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimalist_ods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilberto Vargas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-01 00:00:00.000000000 Z
11
+ date: 2024-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip