jquerycsv-rails 0.71

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae7e474ece32bba3534dbf8b5006c3f34865e919
4
+ data.tar.gz: 6292bd90b0d8e4aaef332700e1996108432cebfd
5
+ SHA512:
6
+ metadata.gz: 1d88a0aa3188318bac38a089c4770597e22b5c65f75145a43c3e87ea162a5a4da5b1b37b514ac7ad0f21485c0696d63968283b7013771b26003abc3d6f7555e0
7
+ data.tar.gz: 7a9919a5cb1e599128347368844cfdc8fc4b36f5adb283cedd8430d8d6e8b493c67ad4ff9146af1d9ca045d2afd61aa3c9480256c050596a414f7c0b95d14b18
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 James Koval
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Jquerycsv::Rails
2
+
3
+ Rails gem tracking jquery csv
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jquerycsv-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jquerycsv-rails
18
+
19
+ ## Usage
20
+
21
+ [Origin](https://code.google.com/p/jquery-csv/)
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require "jquerycsv/rails/version"
2
+
3
+ module Jquerycsv
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ require "jquerycsv/rails/version"
2
+
3
+ module Jquerycsv
4
+ module Rails
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Jquerycsv
2
+ module Rails
3
+ VERSION = "0.71"
4
+ end
5
+ end
@@ -0,0 +1,848 @@
1
+ /**
2
+ * jQuery-csv (jQuery Plugin)
3
+ * version: 0.71 (2012-11-19)
4
+ *
5
+ * This document is licensed as free software under the terms of the
6
+ * MIT License: http://www.opensource.org/licenses/mit-license.php
7
+ *
8
+ * Acknowledgements:
9
+ * The original design and influence to implement this library as a jquery
10
+ * plugin is influenced by jquery-json (http://code.google.com/p/jquery-json/).
11
+ * If you're looking to use native JSON.Stringify but want additional backwards
12
+ * compatibility for browsers that don't support it, I highly recommend you
13
+ * check it out.
14
+ *
15
+ * A special thanks goes out to rwk@acm.org for providing a lot of valuable
16
+ * feedback to the project including the core for the new FSM
17
+ * (Finite State Machine) parsers. If you're looking for a stable TSV parser
18
+ * be sure to take a look at jquery-tsv (http://code.google.com/p/jquery-tsv/).
19
+
20
+ * For legal purposes I'll include the "NO WARRANTY EXPRESSED OR IMPLIED.
21
+ * USE AT YOUR OWN RISK.". Which, in 'layman's terms' means, by using this
22
+ * library you are accepting responsibility if it breaks your code.
23
+ *
24
+ * Legal jargon aside, I will do my best to provide a useful and stable core
25
+ * that can effectively be built on.
26
+ *
27
+ * Copyrighted 2012 by Evan Plaice.
28
+ */
29
+
30
+ RegExp.escape= function(s) {
31
+ return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
32
+ };
33
+
34
+ (function( $ ) {
35
+ 'use strict'
36
+ /**
37
+ * jQuery.csv.defaults
38
+ * Encapsulates the method paramater defaults for the CSV plugin module.
39
+ */
40
+
41
+ $.csv = {
42
+ defaults: {
43
+ separator:',',
44
+ delimiter:'"',
45
+ headers:true
46
+ },
47
+
48
+ hooks: {
49
+ castToScalar: function(value, state) {
50
+ var hasDot = /\./;
51
+ if (isNaN(value)) {
52
+ return value;
53
+ } else {
54
+ if (hasDot.test(value)) {
55
+ return parseFloat(value);
56
+ } else {
57
+ var integer = parseInt(value);
58
+ if(isNaN(integer)) {
59
+ return null;
60
+ } else {
61
+ return integer;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ },
67
+
68
+ parsers: {
69
+ parse: function(csv, options) {
70
+ // cache settings
71
+ var separator = options.separator;
72
+ var delimiter = options.delimiter;
73
+
74
+ // set initial state if it's missing
75
+ if(!options.state.rowNum) {
76
+ options.state.rowNum = 1;
77
+ }
78
+ if(!options.state.colNum) {
79
+ options.state.colNum = 1;
80
+ }
81
+
82
+ // clear initial state
83
+ var data = [];
84
+ var entry = [];
85
+ var state = 0;
86
+ var value = ''
87
+ var exit = false;
88
+
89
+ function endOfEntry() {
90
+ // reset the state
91
+ state = 0;
92
+ value = '';
93
+
94
+ // if 'start' hasn't been met, don't output
95
+ if(options.start && options.state.rowNum < options.start) {
96
+ // update global state
97
+ entry = [];
98
+ options.state.rowNum++;
99
+ options.state.colNum = 1;
100
+ return;
101
+ }
102
+
103
+ if(options.onParseEntry === undefined) {
104
+ // onParseEntry hook not set
105
+ data.push(entry);
106
+ } else {
107
+ var hookVal = options.onParseEntry(entry, options.state); // onParseEntry Hook
108
+ // false skips the row, configurable through a hook
109
+ if(hookVal !== false) {
110
+ data.push(hookVal);
111
+ }
112
+ }
113
+ //console.log('entry:' + entry);
114
+
115
+ // cleanup
116
+ entry = [];
117
+
118
+ // if 'end' is met, stop parsing
119
+ if(options.end && options.state.rowNum >= options.end) {
120
+ exit = true;
121
+ }
122
+
123
+ // update global state
124
+ options.state.rowNum++;
125
+ options.state.colNum = 1;
126
+ }
127
+
128
+ function endOfValue() {
129
+ if(options.onParseValue === undefined) {
130
+ // onParseValue hook not set
131
+ entry.push(value);
132
+ } else {
133
+ var hook = options.onParseValue(value, options.state); // onParseValue Hook
134
+ // false skips the row, configurable through a hook
135
+ if(hook !== false) {
136
+ entry.push(hook);
137
+ }
138
+ }
139
+ //console.log('value:' + value);
140
+ // reset the state
141
+ value = '';
142
+ state = 0;
143
+ // update global state
144
+ options.state.colNum++;
145
+ }
146
+
147
+ // escape regex-specific control chars
148
+ var escSeparator = RegExp.escape(separator);
149
+ var escDelimiter = RegExp.escape(delimiter);
150
+
151
+ // compile the regEx str using the custom delimiter/separator
152
+ var match = /(D|S|\n|\r|[^DS\r\n]+)/;
153
+ var matchSrc = match.source;
154
+ matchSrc = matchSrc.replace(/S/g, escSeparator);
155
+ matchSrc = matchSrc.replace(/D/g, escDelimiter);
156
+ match = RegExp(matchSrc, 'gm');
157
+
158
+ // put on your fancy pants...
159
+ // process control chars individually, use look-ahead on non-control chars
160
+ csv.replace(match, function (m0) {
161
+ if(exit) {
162
+ return;
163
+ }
164
+ switch (state) {
165
+ // the start of a value
166
+ case 0:
167
+ // null last value
168
+ if (m0 === separator) {
169
+ value += '';
170
+ endOfValue();
171
+ break;
172
+ }
173
+ // opening delimiter
174
+ if (m0 === delimiter) {
175
+ state = 1;
176
+ break;
177
+ }
178
+ // null last value
179
+ if (m0 === '\n') {
180
+ endOfValue();
181
+ endOfEntry();
182
+ break;
183
+ }
184
+ // phantom carriage return
185
+ if (/^\r$/.test(m0)) {
186
+ break;
187
+ }
188
+ // un-delimited value
189
+ value += m0;
190
+ state = 3;
191
+ break;
192
+
193
+ // delimited input
194
+ case 1:
195
+ // second delimiter? check further
196
+ if (m0 === delimiter) {
197
+ state = 2;
198
+ break;
199
+ }
200
+ // delimited data
201
+ value += m0;
202
+ state = 1;
203
+ break;
204
+
205
+ // delimiter found in delimited input
206
+ case 2:
207
+ // escaped delimiter?
208
+ if (m0 === delimiter) {
209
+ value += m0;
210
+ state = 1;
211
+ break;
212
+ }
213
+ // null value
214
+ if (m0 === separator) {
215
+ endOfValue();
216
+ break;
217
+ }
218
+ // end of entry
219
+ if (m0 === '\n') {
220
+ endOfValue();
221
+ endOfEntry();
222
+ break;
223
+ }
224
+ // phantom carriage return
225
+ if (/^\r$/.test(m0)) {
226
+ break;
227
+ }
228
+ // broken paser?
229
+ throw new Error('CSVDataError: Illegal State [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
230
+
231
+ // un-delimited input
232
+ case 3:
233
+ // null last value
234
+ if (m0 === separator) {
235
+ endOfValue();
236
+ break;
237
+ }
238
+ // end of entry
239
+ if (m0 === '\n') {
240
+ endOfValue();
241
+ endOfEntry();
242
+ break;
243
+ }
244
+ // phantom carriage return
245
+ if (/^\r$/.test(m0)) {
246
+ break;
247
+ }
248
+ if (m0 === delimiter) {
249
+ // non-compliant data
250
+ throw new Error('CSVDataError: Illegal Quote [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
251
+ }
252
+ // broken parser?
253
+ throw new Error('CSVDataError: Illegal Data [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
254
+ default:
255
+ // shenanigans
256
+ throw new Error('CSVDataError: Unknown State [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
257
+ }
258
+ //console.log('val:' + m0 + ' state:' + state);
259
+ });
260
+
261
+ // submit the last entry
262
+ // ignore null last line
263
+ if(entry.length !== 0) {
264
+ endOfValue();
265
+ endOfEntry();
266
+ }
267
+
268
+ return data;
269
+ },
270
+
271
+ // a csv-specific line splitter
272
+ splitLines: function(csv, options) {
273
+ // cache settings
274
+ var separator = options.separator;
275
+ var delimiter = options.delimiter;
276
+
277
+ // set initial state if it's missing
278
+ if(!options.state.rowNum) {
279
+ options.state.rowNum = 1;
280
+ }
281
+
282
+ // clear initial state
283
+ var entries = [];
284
+ var state = 0;
285
+ var entry = '';
286
+ var exit = false;
287
+
288
+ function endOfLine() {
289
+ // reset the state
290
+ state = 0;
291
+
292
+ // if 'start' hasn't been met, don't output
293
+ if(options.start && options.state.rowNum < options.start) {
294
+ // update global state
295
+ entry = '';
296
+ options.state.rowNum++;
297
+ return;
298
+ }
299
+
300
+ if(options.onParseEntry === undefined) {
301
+ // onParseEntry hook not set
302
+ entries.push(entry);
303
+ } else {
304
+ var hookVal = options.onParseEntry(entry, options.state); // onParseEntry Hook
305
+ // false skips the row, configurable through a hook
306
+ if(hookVal !== false) {
307
+ entries.push(hookVal);
308
+ }
309
+ }
310
+
311
+ // cleanup
312
+ entry = '';
313
+
314
+ // if 'end' is met, stop parsing
315
+ if(options.end && options.state.rowNum >= options.end) {
316
+ exit = true;
317
+ }
318
+
319
+ // update global state
320
+ options.state.rowNum++;
321
+ }
322
+
323
+ // escape regex-specific control chars
324
+ var escSeparator = RegExp.escape(separator);
325
+ var escDelimiter = RegExp.escape(delimiter);
326
+
327
+ // compile the regEx str using the custom delimiter/separator
328
+ var match = /(D|S|\n|\r|[^DS\r\n]+)/;
329
+ var matchSrc = match.source;
330
+ matchSrc = matchSrc.replace(/S/g, escSeparator);
331
+ matchSrc = matchSrc.replace(/D/g, escDelimiter);
332
+ match = RegExp(matchSrc, 'gm');
333
+
334
+ // put on your fancy pants...
335
+ // process control chars individually, use look-ahead on non-control chars
336
+ csv.replace(match, function (m0) {
337
+ if(exit) {
338
+ return;
339
+ }
340
+ switch (state) {
341
+ // the start of a value/entry
342
+ case 0:
343
+ // null value
344
+ if (m0 === separator) {
345
+ entry += m0;
346
+ state = 0;
347
+ break;
348
+ }
349
+ // opening delimiter
350
+ if (m0 === delimiter) {
351
+ entry += m0;
352
+ state = 1;
353
+ break;
354
+ }
355
+ // end of line
356
+ if (m0 === '\n') {
357
+ endOfLine();
358
+ break;
359
+ }
360
+ // phantom carriage return
361
+ if (/^\r$/.test(m0)) {
362
+ break;
363
+ }
364
+ // un-delimit value
365
+ entry += m0;
366
+ state = 3;
367
+ break;
368
+
369
+ // delimited input
370
+ case 1:
371
+ // second delimiter? check further
372
+ if (m0 === delimiter) {
373
+ entry += m0;
374
+ state = 2;
375
+ break;
376
+ }
377
+ // delimited data
378
+ entry += m0;
379
+ state = 1;
380
+ break;
381
+
382
+ // delimiter found in delimited input
383
+ case 2:
384
+ // escaped delimiter?
385
+ var prevChar = entry.substr(entry.length - 1);
386
+ if (m0 === delimiter && prevChar === delimiter) {
387
+ entry += m0;
388
+ state = 1;
389
+ break;
390
+ }
391
+ // end of value
392
+ if (m0 === separator) {
393
+ entry += m0;
394
+ state = 0;
395
+ break;
396
+ }
397
+ // end of line
398
+ if (m0 === '\n') {
399
+ endOfLine();
400
+ break;
401
+ }
402
+ // phantom carriage return
403
+ if (m0 === '\r') {
404
+ break;
405
+ }
406
+ // broken paser?
407
+ throw new Error('CSVDataError: Illegal state [Row:' + options.state.rowNum + ']');
408
+
409
+ // un-delimited input
410
+ case 3:
411
+ // null value
412
+ if (m0 === separator) {
413
+ entry += m0;
414
+ state = 0;
415
+ break;
416
+ }
417
+ // end of line
418
+ if (m0 === '\n') {
419
+ endOfLine();
420
+ break;
421
+ }
422
+ // phantom carriage return
423
+ if (m0 === '\r') {
424
+ break;
425
+ }
426
+ // non-compliant data
427
+ if (m0 === delimiter) {
428
+ throw new Error('CSVDataError: Illegal quote [Row:' + options.state.rowNum + ']');
429
+ }
430
+ // broken parser?
431
+ throw new Error('CSVDataError: Illegal state [Row:' + options.state.rowNum + ']');
432
+ default:
433
+ // shenanigans
434
+ throw new Error('CSVDataError: Unknown state [Row:' + options.state.rowNum + ']');
435
+ }
436
+ //console.log('val:' + m0 + ' state:' + state);
437
+ });
438
+
439
+ // submit the last entry
440
+ // ignore null last line
441
+ if(entry !== '') {
442
+ endOfLine();
443
+ }
444
+
445
+ return entries;
446
+ },
447
+
448
+ // a csv entry parser
449
+ parseEntry: function(csv, options) {
450
+ // cache settings
451
+ var separator = options.separator;
452
+ var delimiter = options.delimiter;
453
+
454
+ // set initial state if it's missing
455
+ if(!options.state.rowNum) {
456
+ options.state.rowNum = 1;
457
+ }
458
+ if(!options.state.colNum) {
459
+ options.state.colNum = 1;
460
+ }
461
+
462
+ // clear initial state
463
+ var entry = [];
464
+ var state = 0;
465
+ var value = '';
466
+
467
+ function endOfValue() {
468
+ if(options.onParseValue === undefined) {
469
+ // onParseValue hook not set
470
+ entry.push(value);
471
+ } else {
472
+ var hook = options.onParseValue(value, options.state); // onParseValue Hook
473
+ // false skips the value, configurable through a hook
474
+ if(hook !== false) {
475
+ entry.push(hook);
476
+ }
477
+ }
478
+ // reset the state
479
+ value = '';
480
+ state = 0;
481
+ // update global state
482
+ options.state.colNum++;
483
+ }
484
+
485
+ // checked for a cached regEx first
486
+ if(!options.match) {
487
+ // escape regex-specific control chars
488
+ var escSeparator = RegExp.escape(separator);
489
+ var escDelimiter = RegExp.escape(delimiter);
490
+
491
+ // compile the regEx str using the custom delimiter/separator
492
+ var match = /(D|S|\n|\r|[^DS\r\n]+)/;
493
+ var matchSrc = match.source;
494
+ matchSrc = matchSrc.replace(/S/g, escSeparator);
495
+ matchSrc = matchSrc.replace(/D/g, escDelimiter);
496
+ options.match = RegExp(matchSrc, 'gm');
497
+ }
498
+
499
+ // put on your fancy pants...
500
+ // process control chars individually, use look-ahead on non-control chars
501
+ csv.replace(options.match, function (m0) {
502
+ switch (state) {
503
+ // the start of a value
504
+ case 0:
505
+ // null last value
506
+ if (m0 === separator) {
507
+ value += '';
508
+ endOfValue();
509
+ break;
510
+ }
511
+ // opening delimiter
512
+ if (m0 === delimiter) {
513
+ state = 1;
514
+ break;
515
+ }
516
+ // skip un-delimited new-lines
517
+ if (m0 === '\n' || m0 === '\r') {
518
+ break;
519
+ }
520
+ // un-delimited value
521
+ value += m0;
522
+ state = 3;
523
+ break;
524
+
525
+ // delimited input
526
+ case 1:
527
+ // second delimiter? check further
528
+ if (m0 === delimiter) {
529
+ state = 2;
530
+ break;
531
+ }
532
+ // delimited data
533
+ value += m0;
534
+ state = 1;
535
+ break;
536
+
537
+ // delimiter found in delimited input
538
+ case 2:
539
+ // escaped delimiter?
540
+ if (m0 === delimiter) {
541
+ value += m0;
542
+ state = 1;
543
+ break;
544
+ }
545
+ // null value
546
+ if (m0 === separator) {
547
+ endOfValue();
548
+ break;
549
+ }
550
+ // skip un-delimited new-lines
551
+ if (m0 === '\n' || m0 === '\r') {
552
+ break;
553
+ }
554
+ // broken paser?
555
+ throw new Error('CSVDataError: Illegal State [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
556
+
557
+ // un-delimited input
558
+ case 3:
559
+ // null last value
560
+ if (m0 === separator) {
561
+ endOfValue();
562
+ break;
563
+ }
564
+ // skip un-delimited new-lines
565
+ if (m0 === '\n' || m0 === '\r') {
566
+ break;
567
+ }
568
+ // non-compliant data
569
+ if (m0 === delimiter) {
570
+ throw new Error('CSVDataError: Illegal Quote [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
571
+ }
572
+ // broken parser?
573
+ throw new Error('CSVDataError: Illegal Data [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
574
+ default:
575
+ // shenanigans
576
+ throw new Error('CSVDataError: Unknown State [Row:' + options.state.rowNum + '][Col:' + options.state.colNum + ']');
577
+ }
578
+ //console.log('val:' + m0 + ' state:' + state);
579
+ });
580
+
581
+ // submit the last value
582
+ endOfValue();
583
+
584
+ return entry;
585
+ }
586
+ },
587
+
588
+ /**
589
+ * $.csv.toArray(csv)
590
+ * Converts a CSV entry string to a javascript array.
591
+ *
592
+ * @param {Array} csv The string containing the CSV data.
593
+ * @param {Object} [options] An object containing user-defined options.
594
+ * @param {Character} [separator] An override for the separator character. Defaults to a comma(,).
595
+ * @param {Character} [delimiter] An override for the delimiter character. Defaults to a double-quote(").
596
+ *
597
+ * This method deals with simple CSV strings only. It's useful if you only
598
+ * need to parse a single entry. If you need to parse more than one line,
599
+ * use $.csv2Array instead.
600
+ */
601
+ toArray: function(csv, options, callback) {
602
+ var options = (options !== undefined ? options : {});
603
+ var config = {};
604
+ config.callback = ((callback !== undefined && typeof(callback) === 'function') ? callback : false);
605
+ config.separator = 'separator' in options ? options.separator : $.csv.defaults.separator;
606
+ config.delimiter = 'delimiter' in options ? options.delimiter : $.csv.defaults.delimiter;
607
+ var state = (options.state !== undefined ? options.state : {});
608
+
609
+ // setup
610
+ var options = {
611
+ delimiter: config.delimiter,
612
+ separator: config.separator,
613
+ onParseEntry: options.onParseEntry,
614
+ onParseValue: options.onParseValue,
615
+ state: state
616
+ }
617
+
618
+ var entry = $.csv.parsers.parseEntry(csv, options);
619
+
620
+ // push the value to a callback if one is defined
621
+ if(!config.callback) {
622
+ return entry;
623
+ } else {
624
+ config.callback('', entry);
625
+ }
626
+ },
627
+
628
+ /**
629
+ * $.csv.toArrays(csv)
630
+ * Converts a CSV string to a javascript array.
631
+ *
632
+ * @param {String} csv The string containing the raw CSV data.
633
+ * @param {Object} [options] An object containing user-defined options.
634
+ * @param {Character} [separator] An override for the separator character. Defaults to a comma(,).
635
+ * @param {Character} [delimiter] An override for the delimiter character. Defaults to a double-quote(").
636
+ *
637
+ * This method deals with multi-line CSV. The breakdown is simple. The first
638
+ * dimension of the array represents the line (or entry/row) while the second
639
+ * dimension contains the values (or values/columns).
640
+ */
641
+ toArrays: function(csv, options, callback) {
642
+ var options = (options !== undefined ? options : {});
643
+ var config = {};
644
+ config.callback = ((callback !== undefined && typeof(callback) === 'function') ? callback : false);
645
+ config.separator = 'separator' in options ? options.separator : $.csv.defaults.separator;
646
+ config.delimiter = 'delimiter' in options ? options.delimiter : $.csv.defaults.delimiter;
647
+
648
+ // setup
649
+ var data = [];
650
+ var options = {
651
+ delimiter: config.delimiter,
652
+ separator: config.separator,
653
+ onParseEntry: options.onParseEntry,
654
+ onParseValue: options.onParseValue,
655
+ start: options.start,
656
+ end: options.end,
657
+ state: {
658
+ rowNum: 1,
659
+ colNum: 1
660
+ }
661
+ };
662
+
663
+ // break the data down to lines
664
+ data = $.csv.parsers.parse(csv, options);
665
+
666
+ // push the value to a callback if one is defined
667
+ if(!config.callback) {
668
+ return data;
669
+ } else {
670
+ config.callback('', data);
671
+ }
672
+ },
673
+
674
+ /**
675
+ * $.csv.toObjects(csv)
676
+ * Converts a CSV string to a javascript object.
677
+ * @param {String} csv The string containing the raw CSV data.
678
+ * @param {Object} [options] An object containing user-defined options.
679
+ * @param {Character} [separator] An override for the separator character. Defaults to a comma(,).
680
+ * @param {Character} [delimiter] An override for the delimiter character. Defaults to a double-quote(").
681
+ * @param {Boolean} [headers] Indicates whether the data contains a header line. Defaults to true.
682
+ *
683
+ * This method deals with multi-line CSV strings. Where the headers line is
684
+ * used as the key for each value per entry.
685
+ */
686
+ toObjects: function(csv, options, callback) {
687
+ var options = (options !== undefined ? options : {});
688
+ var config = {};
689
+ config.callback = ((callback !== undefined && typeof(callback) === 'function') ? callback : false);
690
+ config.separator = 'separator' in options ? options.separator : $.csv.defaults.separator;
691
+ config.delimiter = 'delimiter' in options ? options.delimiter : $.csv.defaults.delimiter;
692
+ config.headers = 'headers' in options ? options.headers : $.csv.defaults.headers;
693
+ options.start = 'start' in options ? options.start : 1;
694
+
695
+ // account for headers
696
+ if(config.headers) {
697
+ options.start++;
698
+ }
699
+ if(options.end && config.headers) {
700
+ options.end++;
701
+ }
702
+
703
+ // setup
704
+ var lines = [];
705
+ var data = [];
706
+
707
+ var options = {
708
+ delimiter: config.delimiter,
709
+ separator: config.separator,
710
+ onParseEntry: options.onParseEntry,
711
+ onParseValue: options.onParseValue,
712
+ start: options.start,
713
+ end: options.end,
714
+ state: {
715
+ rowNum: 1,
716
+ colNum: 1
717
+ },
718
+ match: false
719
+ };
720
+
721
+ // fetch the headers
722
+ var headerOptions = {
723
+ delimiter: config.delimiter,
724
+ separator: config.separator,
725
+ start: 1,
726
+ end: 1,
727
+ state: {
728
+ rowNum:1,
729
+ colNum:1
730
+ }
731
+ }
732
+ var headerLine = $.csv.parsers.splitLines(csv, headerOptions);
733
+ var headers = $.csv.toArray(headerLine[0], options);
734
+
735
+ // fetch the data
736
+ var lines = $.csv.parsers.splitLines(csv, options);
737
+
738
+ // reset the state for re-use
739
+ options.state.colNum = 1;
740
+ if(headers){
741
+ options.state.rowNum = 2;
742
+ } else {
743
+ options.state.rowNum = 1;
744
+ }
745
+
746
+ // convert data to objects
747
+ for(var i=0, len=lines.length; i<len; i++) {
748
+ var entry = $.csv.toArray(lines[i], options);
749
+ var object = {};
750
+ for(var j in headers) {
751
+ object[headers[j]] = entry[j];
752
+ }
753
+ data.push(object);
754
+
755
+ // update row state
756
+ options.state.rowNum++;
757
+ }
758
+
759
+ // push the value to a callback if one is defined
760
+ if(!config.callback) {
761
+ return data;
762
+ } else {
763
+ config.callback('', data);
764
+ }
765
+ },
766
+
767
+ /**
768
+ * $.csv.fromArrays(arrays)
769
+ * Converts a javascript array to a CSV String.
770
+ *
771
+ * @param {Array} array An array containing an array of CSV entries.
772
+ * @param {Object} [options] An object containing user-defined options.
773
+ * @param {Character} [separator] An override for the separator character. Defaults to a comma(,).
774
+ * @param {Character} [delimiter] An override for the delimiter character. Defaults to a double-quote(").
775
+ *
776
+ * This method generates a CSV file from an array of arrays (representing entries).
777
+ */
778
+ fromArrays: function(arrays, options, callback) {
779
+ var options = (options !== undefined ? options : {});
780
+ var config = {};
781
+ config.callback = ((callback !== undefined && typeof(callback) === 'function') ? callback : false);
782
+ config.separator = 'separator' in options ? options.separator : $.csv.defaults.separator;
783
+ config.delimiter = 'delimiter' in options ? options.delimiter : $.csv.defaults.delimiter;
784
+ config.escaper = 'escaper' in options ? options.escaper : $.csv.defaults.escaper;
785
+ config.experimental = 'experimental' in options ? options.experimental : false;
786
+
787
+ if(!config.experimental) {
788
+ throw new Error('not implemented');
789
+ }
790
+
791
+ var output = [];
792
+ for(i in arrays) {
793
+ output.push(arrays[i]);
794
+ }
795
+
796
+ // push the value to a callback if one is defined
797
+ if(!config.callback) {
798
+ return output;
799
+ } else {
800
+ config.callback('', output);
801
+ }
802
+ },
803
+
804
+ /**
805
+ * $.csv.fromObjects(objects)
806
+ * Converts a javascript dictionary to a CSV string.
807
+ * @param {Object} objects An array of objects containing the data.
808
+ * @param {Object} [options] An object containing user-defined options.
809
+ * @param {Character} [separator] An override for the separator character. Defaults to a comma(,).
810
+ * @param {Character} [delimiter] An override for the delimiter character. Defaults to a double-quote(").
811
+ *
812
+ * This method generates a CSV file from an array of objects (name:value pairs).
813
+ * It starts by detecting the headers and adding them as the first line of
814
+ * the CSV file, followed by a structured dump of the data.
815
+ */
816
+ fromObjects2CSV: function(objects, options, callback) {
817
+ var options = (options !== undefined ? options : {});
818
+ var config = {};
819
+ config.callback = ((callback !== undefined && typeof(callback) === 'function') ? callback : false);
820
+ config.separator = 'separator' in options ? options.separator : $.csv.defaults.separator;
821
+ config.delimiter = 'delimiter' in options ? options.delimiter : $.csv.defaults.delimiter;
822
+ config.experimental = 'experimental' in options ? options.experimental : false;
823
+
824
+ if(!config.experimental) {
825
+ throw new Error('not implemented');
826
+ }
827
+
828
+ var output = [];
829
+ for(i in objects) {
830
+ output.push(arrays[i]);
831
+ }
832
+
833
+ // push the value to a callback if one is defined
834
+ if(!config.callback) {
835
+ return output;
836
+ } else {
837
+ config.callback('', output);
838
+ }
839
+ }
840
+ };
841
+
842
+ // Maintenance code to maintain backward-compatibility
843
+ // Will be removed in release 1.0
844
+ $.csvEntry2Array = $.csv.toArray;
845
+ $.csv2Array = $.csv.toArrays;
846
+ $.csv2Dictionary = $.csv.toObjects;
847
+
848
+ })( jQuery );
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jquerycsv-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.71'
5
+ platform: ruby
6
+ authors:
7
+ - James Koval
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: Rails gem tracking jquery csv
56
+ email:
57
+ - james.ross.koval@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/jquerycsv/rails/version.rb
63
+ - lib/jquerycsv/rails.rb
64
+ - lib/jquerycsv-rails.rb
65
+ - vendor/assets/javascripts/jquery.csv-0.71.js
66
+ - LICENSE.txt
67
+ - README.md
68
+ homepage: https://github.com/jakl/jquerycsv-rails
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Rails gem tracking jquery csv
92
+ test_files: []