import_csv 1.0.1 → 1.0.3
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/lib/import_csv.rb +53 -60
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7033ee24199ba6d119cb43b3a9d4caf96e2ea1ce
|
4
|
+
data.tar.gz: 36bc985c8727b83dfbf5e9123a03591c8786a042
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62bda7c19c0f333e447701927e3cd494db23c951daf790418490e0447720cb8c234a34901b1f428f464ec8a30d7b3cfbb8c036af75303175c3683c13a551b7d9
|
7
|
+
data.tar.gz: f8caaa712fef16918a128eaeb7cedfb3b21ee02bf0dd40459aa234feec6a6c7c84d1d809234c58156790b49c6200c8deb3e7c59b589d60caf9b16f65d43e7de3
|
data/lib/import_csv.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
1
3
|
# Kasyfil Aziz Tri Cahyana <tricahyana@windowslive.com> <kasyfil.aziz@wgs.co.id> 2016
|
2
4
|
#
|
3
5
|
# require all file below in yours Ruby on Rails Application
|
@@ -123,9 +125,6 @@
|
|
123
125
|
# ... your code ...
|
124
126
|
# end
|
125
127
|
#
|
126
|
-
|
127
|
-
require 'csv'
|
128
|
-
|
129
128
|
class ImportCSV
|
130
129
|
# preload data
|
131
130
|
attr_accessor :preload
|
@@ -344,9 +343,9 @@ class ImportCSV
|
|
344
343
|
# Example :
|
345
344
|
# csv = ImportCSV.new(Rails.root.join('db/seeds/development/tx_locations.csv'), header: true)
|
346
345
|
# csv.next
|
347
|
-
# p csv.location_id
|
346
|
+
# p csv.location_id ~> `return first line from file`
|
348
347
|
# csv.next
|
349
|
-
# p csv.location_id
|
348
|
+
# p csv.location_id ~> `return second line from file`
|
350
349
|
#
|
351
350
|
# Example using while :
|
352
351
|
# csv = ImportCSV.new(Rails.root.join('db/seeds/development/tx_locations.csv'), header: true)
|
@@ -480,87 +479,84 @@ class ImportCSV
|
|
480
479
|
#
|
481
480
|
# Example:
|
482
481
|
# CSV data:
|
483
|
-
#
|
482
|
+
# __________________________
|
484
483
|
# |id | name | birth |
|
485
484
|
# |1 | shania | 27-06-1998|
|
486
485
|
# |2 | jessica | 19-08-1993|
|
487
486
|
# |3 | michelle| 28-10-1999|
|
488
487
|
# |___|__________|___________|
|
489
488
|
#
|
490
|
-
#
|
491
|
-
#
|
492
|
-
#
|
493
|
-
#
|
494
|
-
#
|
489
|
+
# Equal.
|
490
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
491
|
+
# csv.where(name: 'michelle')
|
492
|
+
# ... use csv.each or while csv.next ...
|
493
|
+
# ~> will return [3, 'michelle', '28-10-1999']
|
495
494
|
#
|
496
|
-
#
|
497
|
-
#
|
498
|
-
#
|
499
|
-
#
|
495
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
496
|
+
# csv.where(name: ['shania', 'jessica'])
|
497
|
+
# ... use csv.each or while csv.next ...
|
498
|
+
# ~> will return [[1, 'shania', '27-06-1998'], [2, 'jessica', '19-08-1993']]
|
500
499
|
#
|
501
|
-
#
|
502
|
-
#
|
503
|
-
#
|
504
|
-
#
|
505
|
-
#
|
506
|
-
#
|
507
|
-
#
|
500
|
+
# Range. Only for Date, Integer and Float data type. Define datatype in
|
501
|
+
# first range. Use `integer` for Integer & Float, use `date` for Date.
|
502
|
+
# See Example below.
|
503
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
504
|
+
# csv.where(id: 1..2)
|
505
|
+
# ... use csv.each or while csv.next ...
|
506
|
+
# => will return [[1, 'shania', '27-06-1998'], [2, 'jessica', '19-08-1993']]
|
508
507
|
#
|
509
|
-
#
|
510
|
-
#
|
511
|
-
#
|
512
|
-
#
|
508
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
509
|
+
# csv.where(birth: '01-01-1993'.to_date..'01-01-1999'.to_date)
|
510
|
+
# ... use csv.each or while csv.next ...
|
511
|
+
# => will return [[1, 'shania', '27-06-1998'], [2, 'jessica', '19-08-1993']]
|
513
512
|
#
|
514
|
-
#
|
515
|
-
#
|
513
|
+
# Operator '>' & '<'. Only for column with data type Integer, Float or Date
|
514
|
+
# Like `id` or `birth` in example csv data above.
|
516
515
|
# - Data type must defined in filter, use `integer` for Integer or Float
|
517
516
|
# and use `date` for Date. Put operator & data type together without
|
518
517
|
# space. See example below.
|
518
|
+
#
|
519
519
|
# - For filter with Date data type (in csv or in filter), any value that
|
520
520
|
# can be parse using `Date.parse` are acceptable.
|
521
|
-
#
|
521
|
+
#
|
522
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
522
523
|
# csv.where(id: '>(integer)1')
|
523
524
|
# ... use csv.each or while csv.next ...
|
524
|
-
# => will return [[2, 'jessica', '19-08-1993'], [
|
525
|
+
# => will return [[2, 'jessica', '19-08-1993'], [3, 'michelle', '28-10-1999']]
|
525
526
|
#
|
526
|
-
#
|
527
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
527
528
|
# csv.where(birth: '<(date)01-01-1997')
|
528
529
|
# ... use csv.each or while csv.next ...
|
529
530
|
# => will return [2, 'jessica', '19-08-1993']
|
530
531
|
#
|
531
|
-
#
|
532
|
-
#
|
533
|
-
#
|
534
|
-
#
|
535
|
-
#
|
536
|
-
#
|
532
|
+
# Operator '!'. Put this operator in first character and folow with query
|
533
|
+
# without space.
|
534
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
535
|
+
# csv.where(name: '!michelle')
|
536
|
+
# ... use csv.each or while csv.next ...
|
537
|
+
# => will return [[1, 'shania', '27-06-1998'], [2, 'jessica', '19-08-1993']]
|
537
538
|
#
|
538
|
-
#
|
539
|
-
#
|
540
|
-
#
|
541
|
-
#
|
539
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
540
|
+
# csv.where(name: ['!shania', '!jessica'])
|
541
|
+
# ... use csv.each or while csv.next ...
|
542
|
+
# => will return [3, 'michelle', '28-10-1999']
|
542
543
|
#
|
543
|
-
#
|
544
|
-
#
|
545
|
-
#
|
546
|
-
#
|
547
|
-
#
|
548
|
-
#
|
544
|
+
# Operator '%'. `Like` Operator. Put this operator in first character and
|
545
|
+
# folow with query without space.
|
546
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
547
|
+
# csv.where(name: '%jes')
|
548
|
+
# ... use csv.each or while csv.next ...
|
549
|
+
# => will return [2, 'jessica', '19-08-1993']
|
549
550
|
#
|
550
|
-
#
|
551
|
-
#
|
552
|
-
#
|
553
|
-
#
|
551
|
+
# csv = ImportCSV.new('member.csv'), header: true)
|
552
|
+
# csv.where(name: ['%jes', '%shan'])
|
553
|
+
# ... use csv.each or while csv.next ...
|
554
|
+
# => will return [[1, 'shania', '27-06-1998'], [2, 'jessica', '19-08-1993']]
|
554
555
|
#
|
555
556
|
# Note :
|
556
|
-
# - Data type must define if you use `<` or
|
557
|
-
# data type in other operator.
|
557
|
+
# - Data type must define if you use `<` or `>`.
|
558
558
|
#
|
559
559
|
def where(query = Hash.new)
|
560
|
-
# if !self.has_header
|
561
|
-
# raise ArgumentError, 'Header not detected.'
|
562
|
-
# end
|
563
|
-
|
564
560
|
query.each do |key, values|
|
565
561
|
if values.kind_of?(Range)
|
566
562
|
if values.first.kind_of?(String) || values.last.kind_of?(String)
|
@@ -611,14 +607,12 @@ class ImportCSV
|
|
611
607
|
# if value is Range, (integer)1..2 or (date)01-01-2015..01-01-2016
|
612
608
|
if values.kind_of?(Range)
|
613
609
|
# scan for data type insert brackets.
|
614
|
-
# if values.first.scan(/\(([^\)]+)\)/)[0][0].downcase == "date"
|
615
610
|
if values.first.kind_of?(Date) || values.first.kind_of?(Time)
|
616
611
|
# tmp_range_first = Date.parse(values.first[6..values.first.size])
|
617
612
|
tmp_range_first = values.first
|
618
613
|
tmp_range_last = values.last
|
619
614
|
# remove quote \" and `new line` from string
|
620
615
|
tmp_value = Date.parse(_row_tmp[self.get_header_index(key)].gsub(/\A"|"\Z/, '').gsub(row_sep, ''))
|
621
|
-
# elsif values.first.scan(/\(([^\)]+)\)/)[0][0].downcase == "integer"
|
622
616
|
elsif values.first.kind_of?(Integer) || values.first.kind_of?(Float)
|
623
617
|
# value with type integer will convert to float
|
624
618
|
# tmp_range_first = (values.first[9..values.first.size]).to_f
|
@@ -690,7 +684,6 @@ class ImportCSV
|
|
690
684
|
else
|
691
685
|
is_insert = false
|
692
686
|
end
|
693
|
-
|
694
687
|
else
|
695
688
|
# raise an ArgumentError (Exception) if opertor is not one
|
696
689
|
# of which has been defined
|