csv_manager 1.0.1.pre.rc.1 → 1.0.2.pre.rc.1

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
  SHA1:
3
- metadata.gz: ced119dddc799c9c44f05de132e04f2bee096843
4
- data.tar.gz: a8bb365c2826af9f3bb13ac11f3213fd8d56206a
3
+ metadata.gz: c13cb6a7dee6b5d523e69bde463aed98eeb43c18
4
+ data.tar.gz: d7bb6b7a0b965317b3736045601bb68aa4085af5
5
5
  SHA512:
6
- metadata.gz: '00826715c6ed5a3d2cdd943c6a9454507fe9540e133e88f8298e3e4ac688982ba6ebf520e48a784791acd19f0fab7fdfb1e9519978165db8285f1e1ca3ee2892'
7
- data.tar.gz: d8c0a2fa7f8089a92cf88eb3394b008d2f1f1e67f26214468595b8a2fa8a9dab34c77469bda6e9b04fdaf07d441226d36dc906f716ecbd7a8aa71f26a9221daf
6
+ metadata.gz: 985cd035536b6c666718c5df1c8b59238f1cac93e3787e26afdf14f0bbc2c1a6cbf99f87b6780e6230af7d36cde08f3e528f5d6a4784fc039577938dd6ea15ac
7
+ data.tar.gz: 6302d1b9739f57aea3f50b655d47a427b644cd570dcf388d15828ba010c22df46374db3a2ebc1e40096d35c37d6bc1b562e97c03cb60ea50358e7224ee057c2f
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # CSVManager
2
2
 
3
+ [![Build Status](https://travis-ci.org/ghbooth12/csv_manager.svg?branch=master)](https://travis-ci.org/ghbooth12/csv_manager)
4
+ [![Gem Version](https://badge.fury.io/rb/csv_manager.svg)](https://badge.fury.io/rb/csv_manager)
5
+
3
6
  This gem helps import/export a CSV file in a very easy and simple way.
4
7
 
5
8
  ```ruby
@@ -80,6 +83,7 @@ In the view, make sure to send `format: "csv"` to the path.
80
83
 
81
84
  * [Example 5) Download database in a CSV file](doc/export_download.md)
82
85
 
86
+ -----
83
87
 
84
88
  ## Demo App
85
89
  To see the `CSVManager` in action you can take a look at this app - [Inventory](https://inventory-ghbooth12.herokuapp.com).
data/csv_manager.gemspec CHANGED
@@ -28,5 +28,6 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "rake", "~> 10.0"
29
29
  spec.add_development_dependency "rspec", "~> 3.0"
30
30
  spec.add_development_dependency "pry", "~> 0.10.4"
31
- # spec.add_development_dependency "logging"
31
+
32
+ spec.add_runtime_dependency "logging"
32
33
  end
data/lib/csv_manager.rb CHANGED
@@ -1,8 +1,10 @@
1
- require "csv_manager/version"
2
1
  require "csv"
3
2
  require "pry"
3
+ require 'logging'
4
+ require "csv_manager/version"
4
5
  require "csv_manager/import"
5
6
  require "csv_manager/export"
7
+ require 'csv_manager/utils'
6
8
 
7
9
  module CSVManager
8
10
  end
@@ -1,18 +1,25 @@
1
- # require 'logging'
2
-
3
1
  module CSVManager
4
2
  class Export
5
- # def initialize
6
- # @logger = Logging.logger['csv_manager_export_log.txt']
7
- # @logger.level = :debug
8
- # end
3
+ def initialize
4
+ @logger = Logging.logger(STDOUT)
5
+ @logger.level = :debug
6
+ end
9
7
 
8
+ # Calls `send_data` with text from `to_csv(obj)` and option hash.
9
+ # Then returns a CSV formatted string in an array.
10
+ # [params]
11
+ # - controller: controller that takes care of "download CSV" request. eg) products_controller
12
+ # - objs: model instances. eg) Product.all, Product.where(category: "office")
13
+ # - filename: a filename string. eg) "inventory_2016.csv"
10
14
  def download(controller, objs, filename=nil)
11
- # @logger.debug "DOWNLOAD METHOD CALLED"
12
15
  return if controller.nil? || objs.nil?
16
+ @logger.info "#{LINER}CSVManager::Export#download {controller: #{controller}, objs: #{objs}, filename: #{filename}}#{LINER}"
13
17
  controller.send_data to_csv(objs), filename: filename
14
18
  end
15
19
 
20
+ # Generates a CSV formatted string with headers and given objects(objs).
21
+ # [params]
22
+ # - objs: model instances. eg) [product1, product2], Product.all
16
23
  def to_csv(objs)
17
24
  return unless validate(objs)
18
25
 
@@ -34,7 +41,9 @@ module CSVManager
34
41
  return csv_file
35
42
  end
36
43
 
37
- # This method checks to see if an instance of ActiveRecord::Relation or an array of instance of ActiveRecord::Base is passed in.
44
+ # Checks to see if an instance of ActiveRecord::Relation or an array of instance of ActiveRecord::Base is passed in.
45
+ # [params]
46
+ # - objs: model instances
38
47
  def validate(objs)
39
48
  valid = false
40
49
 
@@ -2,13 +2,26 @@ module CSVManager
2
2
  class Import
3
3
  attr_reader :cols, :rows, :entire, :error
4
4
 
5
+ # [Instance variables]
6
+ # - @cols: an array of CSV headers. eg) ["category", "name", "price"]
7
+ # - @rows: an array of arrays which consist of values in each row of CSV file. eg) [["fruit", "apple", 1], ["fruit", "banana", 0.5]]
8
+ # - @entire: an array of hashes which consist of keys and values of CSV file. Keys are headers and values are data under the header columns. eg) [{"category"=>"fruit", "name"=>"apple", "price"=>1}, {"category"=>"fruit", "name"=>"banana", "price"=>0.5}]
9
+ # - @error: an error message string
5
10
  def initialize
11
+ @logger = Logging.logger(STDOUT)
12
+ @logger.level = :debug
13
+
6
14
  @cols = []
7
15
  @rows = []
8
16
  @entire = []
9
17
  @error = nil
10
18
  end
11
19
 
20
+ # Reads the given CSV file, parses and sets @cols, @rows and @entire.
21
+ # If CSV file is not readable, the error message assigns to @error without throwing an exception.
22
+ # [params]
23
+ # - file: CSV file path string
24
+ # - headers: true(CSV file includes headers.) OR string type("category,name,price") OR array type(["category","name","price"])
12
25
  def parse(file, headers=true)
13
26
  return if file.nil?
14
27
  file = file.path if file.respond_to?(:path)
@@ -17,6 +30,7 @@ module CSVManager
17
30
  raw_rows = CSV.read(file, headers: headers, converters: :numeric).to_a
18
31
  rescue
19
32
  @error = "ERROR! #{$!.message}"
33
+ @logger.warn "#{LINER}CSVManager::Import#parse {file: #{file}, headers: #{headers}}\n#{@error}#{LINER}"
20
34
  return
21
35
  end
22
36
 
@@ -29,20 +43,47 @@ module CSVManager
29
43
  end
30
44
  end
31
45
 
46
+ # Counts the number of rows in CSV file which was parsed in `parse` method.
47
+ # Note: Before calling this method, `parse` has to be called first.
32
48
  def count
33
49
  @rows.count
34
50
  end
35
51
 
52
+ # Returns an array of values related to `col`
53
+ # [params]
54
+ # - col: a header string. eg) "category", "name"...
36
55
  def get_contents(col)
37
56
  @entire.map {|hash| hash[col]}
38
57
  end
39
58
 
40
- # Before calling this method, `parse` has to be called first.
59
+ # Populates `model` with `hash_arr` array
60
+ # Note: Before calling this method, `parse` has to be called first.
61
+ # [params]
62
+ # - model: model object eg) Call create(Product), NOT create("Product")
63
+ # - hash_arr: an array of hashes. eg) [{"name"=>"doll", "category"=>"Toys", "quantity"=>8}, {"name"=>"ribbon", "category"=>"craft", "quantity"=>10}]
41
64
  def create(model, hash_arr=@entire)
42
- return if model.nil?
65
+ return if model.nil? || hash_arr.nil? || !validate(model, hash_arr)
66
+
43
67
  for hash in hash_arr
44
68
  model.create(hash)
45
69
  end
46
70
  end
71
+
72
+ # Checks if `hash_arr` is an array and
73
+ # attributes of `model` match with keys of the hash from `hash_arr`.
74
+ # [params]
75
+ # - model: model object eg) Call create(Product), NOT create("Product")
76
+ # - hash_arr: an array of hashes. eg) [{"name"=>"doll", "category"=>"Toys", "quantity"=>8}, {"name"=>"ribbon", "category"=>"craft", "quantity"=>10}]
77
+ def validate(model, hash_arr)
78
+ return false unless hash_arr.is_a?(Array)
79
+ keys = hash_arr.first.keys
80
+ cols = model.column_names[1..-3]
81
+
82
+ if keys != cols
83
+ @logger.warn "#{LINER}CSVManager::Import#validate | MatchError: #{keys} != #{cols} #{LINER}"
84
+ return false
85
+ end
86
+ true
87
+ end
47
88
  end
48
89
  end
@@ -0,0 +1,3 @@
1
+ module CSVManager
2
+ LINER = "\n#{"=" * 20}\n"
3
+ end
@@ -1,3 +1,3 @@
1
1
  module CSVManager
2
- VERSION = "1.0.1-rc.1"
2
+ VERSION = "1.0.2-rc.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1.pre.rc.1
4
+ version: 1.0.2.pre.rc.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gahee Heo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-09 00:00:00.000000000 Z
11
+ date: 2017-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -120,6 +120,20 @@ dependencies:
120
120
  - - "~>"
121
121
  - !ruby/object:Gem::Version
122
122
  version: 0.10.4
123
+ - !ruby/object:Gem::Dependency
124
+ name: logging
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ type: :runtime
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
123
137
  description: Import and export a CSV file in a very easy and simple way
124
138
  email:
125
139
  - ghbooth12@gmail.com
@@ -146,6 +160,7 @@ files:
146
160
  - lib/csv_manager.rb
147
161
  - lib/csv_manager/export.rb
148
162
  - lib/csv_manager/import.rb
163
+ - lib/csv_manager/utils.rb
149
164
  - lib/csv_manager/version.rb
150
165
  homepage: https://github.com/ghbooth12/csv_manager
151
166
  licenses: