honey_format 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.travis.yml +4 -2
- data/README.md +3 -1
- data/honey_format.gemspec +1 -1
- data/lib/honey_format/columns.rb +5 -11
- data/lib/honey_format/convert_header_value.rb +31 -0
- data/lib/honey_format/csv.rb +2 -2
- data/lib/honey_format/header.rb +7 -12
- data/lib/honey_format/sanitize.rb +26 -6
- data/lib/honey_format/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d0c2df26f96e590dfd9c65b15c99b9cb17227c02b434e09acd5afc0477b4e5ba
|
4
|
+
data.tar.gz: 59acd72d13e5474e22b47f35beb6961de6a38c7104b1a6f73bfb85f99d301a75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 549b7fd83a6d787d0d5aef97fbec94a33fb6fbca040e2523d7a30724c2e56b81669f71d8464eac6ab428ef4552e4ca4bb614b4d2046aa0bd6d2d527b3a1a17b1
|
7
|
+
data.tar.gz: 6889ec68257b31887b4e804766c281b0547f93d8feb8720214670d79cf2636f40920633181ce7597d66e7ae381bac34dc32e1f449bccac19be73eab24c29b539
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# HoneyFormat [![Build Status](https://travis-ci.org/buren/honey_format.svg)](https://travis-ci.org/buren/honey_format) [![Code Climate](https://codeclimate.com/github/buren/honey_format/badges/gpa.svg)](https://codeclimate.com/github/buren/honey_format) ![Docs badge](https://inch-ci.org/github/buren/honey_format.svg?branch=master)
|
2
2
|
|
3
|
-
Convert CSV to
|
3
|
+
Convert CSV to an array of objects with with ease.
|
4
4
|
|
5
5
|
Perfect for small files of test data or small import scripts.
|
6
6
|
|
@@ -13,6 +13,8 @@ user.id # => "1"
|
|
13
13
|
user.username # => "buren"
|
14
14
|
```
|
15
15
|
|
16
|
+
:information_source: Supports Ruby >= 2.3, has no dependencies other than Ruby stdlib.
|
17
|
+
|
16
18
|
## Installation
|
17
19
|
|
18
20
|
Add this line to your application's Gemfile:
|
data/honey_format.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ['burenstam@gmail.com']
|
11
11
|
|
12
12
|
spec.summary = 'Convert CSV to objects.'
|
13
|
-
spec.description = 'Convert CSV to
|
13
|
+
spec.description = 'Convert CSV to an array of objects with with ease. Create objects for each row with methods matching the column names. No dependencies other than Ruby stdlib.'
|
14
14
|
spec.homepage = 'https://github.com/buren/honey_format'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
data/lib/honey_format/columns.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'honey_format/convert_header_value'
|
2
|
+
|
1
3
|
module HoneyFormat
|
2
4
|
# Represents columns.
|
3
5
|
class Columns
|
@@ -6,7 +8,8 @@ module HoneyFormat
|
|
6
8
|
# @param [Array] valid array of symbols representing valid columns.
|
7
9
|
# @raise [MissingCSVHeaderColumnError] raised when header is missing
|
8
10
|
# @raise [UnknownCSVHeaderColumnError] raised when column is not in valid list.
|
9
|
-
def initialize(header, valid
|
11
|
+
def initialize(header, valid: :all, converter: ConvertHeaderValue)
|
12
|
+
@converter = converter
|
10
13
|
@columns = build_columns(header, valid)
|
11
14
|
end
|
12
15
|
|
@@ -20,23 +23,14 @@ module HoneyFormat
|
|
20
23
|
|
21
24
|
def build_columns(header, valid)
|
22
25
|
header.map do |column|
|
23
|
-
|
26
|
+
column = @converter.call(column.dup)
|
24
27
|
validate_column_presence!(column)
|
25
28
|
|
26
|
-
column = symnolize_string!(column)
|
27
|
-
|
28
29
|
validate_column_name!(column, valid)
|
29
30
|
column
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
|
-
def symnolize_string!(column)
|
34
|
-
column.downcase!
|
35
|
-
column.gsub!(/ /, '')
|
36
|
-
column.gsub!(/-/, '_')
|
37
|
-
column.to_sym
|
38
|
-
end
|
39
|
-
|
40
34
|
def validate_column_presence!(col)
|
41
35
|
if col.nil? || col.empty?
|
42
36
|
fail(MissingCSVHeaderColumnError, "CSV header column can't be empty.")
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module HoneyFormat
|
2
|
+
module ConvertHeaderValue
|
3
|
+
REPLACE_MAP = [
|
4
|
+
[/ \(/, '('],
|
5
|
+
[/ \[/, '['],
|
6
|
+
[/ \{/, '{'],
|
7
|
+
[/\) /, ')'],
|
8
|
+
[/\] /, ']'],
|
9
|
+
[/\} /, '}'],
|
10
|
+
[/ /, '_'],
|
11
|
+
[/-/, '_']
|
12
|
+
].map { |array| array.freeze }.freeze
|
13
|
+
|
14
|
+
# Returns converted value and mutates the argument.
|
15
|
+
# @return [Symbol] the cleaned header column.
|
16
|
+
# @param [String] column the string to be cleaned.
|
17
|
+
# @example Convert simple header
|
18
|
+
# ConvertHeaderValue.call(" User name ") #=> "user_name"
|
19
|
+
# @example Convert complex header
|
20
|
+
# ConvertHeaderValue.call(" First name (user)") #=> :'first_name(user)'
|
21
|
+
def self.call(column)
|
22
|
+
column.strip!
|
23
|
+
column.downcase!
|
24
|
+
REPLACE_MAP.each do |data|
|
25
|
+
from, to = data
|
26
|
+
column.gsub!(from, to)
|
27
|
+
end
|
28
|
+
column.to_sym
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/honey_format/csv.rb
CHANGED
@@ -14,10 +14,10 @@ module HoneyFormat
|
|
14
14
|
# @raise [MissingCSVHeaderError] raised when header is missing (empty or nil).
|
15
15
|
# @raise [MissingCSVHeaderColumnError] raised when header column is missing.
|
16
16
|
# @raise [UnknownCSVHeaderColumnError] raised when column is not in valid list.
|
17
|
-
def initialize(csv, delimiter: ',', header: nil, valid_columns: :all)
|
17
|
+
def initialize(csv, delimiter: ',', header: nil, valid_columns: :all, header_converter: ConvertHeaderValue)
|
18
18
|
csv = ::CSV.parse(csv, col_sep: delimiter)
|
19
19
|
@csv_body = csv
|
20
|
-
@header = Header.new(header || csv.shift, valid: valid_columns)
|
20
|
+
@header = Header.new(header || csv.shift, valid: valid_columns, converter: header_converter)
|
21
21
|
end
|
22
22
|
|
23
23
|
# @return [Array] of strings for sanitized header.
|
data/lib/honey_format/header.rb
CHANGED
@@ -9,9 +9,13 @@ module HoneyFormat
|
|
9
9
|
# @param [Array] header array of strings.
|
10
10
|
# @param [Array] valid array of symbols representing valid columns.
|
11
11
|
# @raise [MissingCSVHeaderError] raised when header is missing (empty or nil).
|
12
|
-
def initialize(header, valid: :all)
|
13
|
-
|
14
|
-
|
12
|
+
def initialize(header, valid: :all, converter: ConvertHeaderValue)
|
13
|
+
if header.nil? || header.empty?
|
14
|
+
fail(MissingCSVHeaderError, "CSV header can't be empty.")
|
15
|
+
end
|
16
|
+
|
17
|
+
@column_names = Sanitize.array(header)
|
18
|
+
@columns = Columns.new(@column_names, valid: valid, converter: converter)
|
15
19
|
end
|
16
20
|
|
17
21
|
# Returns columns as array.
|
@@ -19,14 +23,5 @@ module HoneyFormat
|
|
19
23
|
def columns
|
20
24
|
@columns.to_a
|
21
25
|
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def build_header(header)
|
26
|
-
if header.nil? || header.empty?
|
27
|
-
fail(MissingCSVHeaderError, "CSV header can't be empty.")
|
28
|
-
end
|
29
|
-
Sanitize.array!(header)
|
30
|
-
end
|
31
26
|
end
|
32
27
|
end
|
@@ -2,16 +2,36 @@ module HoneyFormat
|
|
2
2
|
# Utility class for sanitizing various simple data types.
|
3
3
|
class Sanitize
|
4
4
|
# Returns array of cleaned strings.
|
5
|
-
# @return [Array] the cleaned array of strings.
|
6
|
-
# @param [Array] row the array of strings to be cleaned.
|
5
|
+
# @return [Array<String>] the cleaned array of strings.
|
6
|
+
# @param [Array<String>] row the array of strings to be cleaned.
|
7
|
+
# @example Sanitize array
|
8
|
+
# Sanitize.array([" a "]) #=> ["a"]
|
9
|
+
def self.array(row)
|
10
|
+
row.map { |column| string(column) }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns array of cleaned elements.
|
14
|
+
# @return [String] the cleaned array.
|
15
|
+
# @param [String] column the string to be cleaned.
|
16
|
+
# @example Sanitize string
|
17
|
+
# Sanitize.string(" a ") #=> "a"
|
18
|
+
# @example Sanitize nil
|
19
|
+
# Sanitize.string(nil) #=> nil
|
20
|
+
def self.string(column)
|
21
|
+
return column if column.nil?
|
22
|
+
column.strip
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns mutated array of cleaned strings.
|
26
|
+
# @return [Array<String>] the cleaned array of strings.
|
27
|
+
# @param [Array<String>] row the array of strings to be cleaned.
|
7
28
|
# @example Sanitize array
|
8
29
|
# Sanitize.array!([" a "]) #=> ["a"]
|
9
30
|
def self.array!(row)
|
10
31
|
row.map! { |column| string!(column) }
|
11
|
-
row
|
12
32
|
end
|
13
33
|
|
14
|
-
# Returns
|
34
|
+
# Returns mutated and cleaned string.
|
15
35
|
# @return [String] the cleaned array.
|
16
36
|
# @param [String] column the string to be cleaned.
|
17
37
|
# @example Sanitize string
|
@@ -19,8 +39,8 @@ module HoneyFormat
|
|
19
39
|
# @example Sanitize nil
|
20
40
|
# Sanitize.string!(nil) #=> nil
|
21
41
|
def self.string!(column)
|
22
|
-
|
23
|
-
column
|
42
|
+
return if column.nil?
|
43
|
+
column.tap(&:strip!)
|
24
44
|
end
|
25
45
|
end
|
26
46
|
end
|
data/lib/honey_format/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: honey_format
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Burenstam
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,8 +80,9 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description: Convert CSV to
|
84
|
-
|
83
|
+
description: Convert CSV to an array of objects with with ease. Create objects for
|
84
|
+
each row with methods matching the column names. No dependencies other than Ruby
|
85
|
+
stdlib.
|
85
86
|
email:
|
86
87
|
- burenstam@gmail.com
|
87
88
|
executables: []
|
@@ -103,6 +104,7 @@ files:
|
|
103
104
|
- honey_format.gemspec
|
104
105
|
- lib/honey_format.rb
|
105
106
|
- lib/honey_format/columns.rb
|
107
|
+
- lib/honey_format/convert_header_value.rb
|
106
108
|
- lib/honey_format/csv.rb
|
107
109
|
- lib/honey_format/exceptions.rb
|
108
110
|
- lib/honey_format/header.rb
|
@@ -130,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
132
|
version: '0'
|
131
133
|
requirements: []
|
132
134
|
rubyforge_project:
|
133
|
-
rubygems_version: 2.
|
135
|
+
rubygems_version: 2.7.6
|
134
136
|
signing_key:
|
135
137
|
specification_version: 4
|
136
138
|
summary: Convert CSV to objects.
|