honey_format 0.2.1 → 0.3.0

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
- SHA1:
3
- metadata.gz: 976b0f358d1577605a7cf6774c0bfa0d8cc417dc
4
- data.tar.gz: 692ee6e558f64ff7cded7e1e3d2b0b3fe33e3d73
2
+ SHA256:
3
+ metadata.gz: d0c2df26f96e590dfd9c65b15c99b9cb17227c02b434e09acd5afc0477b4e5ba
4
+ data.tar.gz: 59acd72d13e5474e22b47f35beb6961de6a38c7104b1a6f73bfb85f99d301a75
5
5
  SHA512:
6
- metadata.gz: e7da999d1c8b1b938111752b87c6174bc8d5119c0d8acd46100fd6d6add24ef48a03153a4df8f6d5153ade514bc40dfbcd6e121b9a9fb4da102b60d6eef9eb81
7
- data.tar.gz: cdf05e13e33c42d7942becd58f4db7a9ffac59e7b05a86a793ae97861f18a9f72064605fc2de331aa46384b80f1c28878534452ca6d6cc7e5e89456cd979de42
6
+ metadata.gz: 549b7fd83a6d787d0d5aef97fbec94a33fb6fbca040e2523d7a30724c2e56b81669f71d8464eac6ab428ef4552e4ca4bb614b4d2046aa0bd6d2d527b3a1a17b1
7
+ data.tar.gz: 6889ec68257b31887b4e804766c281b0547f93d8feb8720214670d79cf2636f40920633181ce7597d66e7ae381bac34dc32e1f449bccac19be73eab24c29b539
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  benchmark.csv
11
+ .byebug_history
data/.travis.yml CHANGED
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.2
4
- before_install: gem install bundler -v 1.10.6
3
+ - 2.3.0
4
+ - 2.4.0
5
+ - 2.5.0
6
+ before_install: gem install bundler -v 1.16.1
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 object with one command.
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 object with one command. Automagically makes your column order independent by defining real, named, methods for each column.'
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
 
@@ -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 = :all)
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
- Sanitize.string!(column)
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
@@ -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.
@@ -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
- @column_names = build_header(header)
14
- @columns = Columns.new(@column_names, valid)
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 array of cleaned elements.
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
- column.strip! unless column.nil?
23
- column
42
+ return if column.nil?
43
+ column.tap(&:strip!)
24
44
  end
25
45
  end
26
46
  end
@@ -1,4 +1,4 @@
1
1
  module HoneyFormat
2
2
  # Gem version
3
- VERSION = '0.2.1'
3
+ VERSION = '0.3.0'
4
4
  end
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.2.1
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: 2015-10-04 00:00:00.000000000 Z
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 object with one command. Automagically makes your column
84
- order independent by defining real, named, methods for each column.
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.4.7
135
+ rubygems_version: 2.7.6
134
136
  signing_key:
135
137
  specification_version: 4
136
138
  summary: Convert CSV to objects.