honey_format 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +3 -0
  3. data/.rubocop.yml +7 -0
  4. data/.ruby-style-guide.yml +264 -0
  5. data/CHANGELOG.md +15 -0
  6. data/Gemfile +2 -0
  7. data/README.md +63 -15
  8. data/Rakefile +2 -0
  9. data/bin/benchmark +2 -0
  10. data/bin/console +1 -0
  11. data/exe/honey_format +1 -0
  12. data/honey_format.gemspec +5 -4
  13. data/lib/honey_format/cli/benchmark_cli.rb +15 -13
  14. data/lib/honey_format/cli/cli.rb +17 -12
  15. data/lib/honey_format/cli/result_writer.rb +2 -0
  16. data/lib/honey_format/configuration.rb +114 -11
  17. data/lib/honey_format/converters/convert_boolean.rb +24 -0
  18. data/lib/honey_format/converters/convert_date_and_time.rb +30 -0
  19. data/lib/honey_format/converters/convert_number.rb +33 -0
  20. data/lib/honey_format/converters/convert_string.rb +42 -0
  21. data/lib/honey_format/converters/converters.rb +12 -0
  22. data/lib/honey_format/converters/header_column_converter.rb +57 -0
  23. data/lib/honey_format/csv.rb +22 -14
  24. data/lib/honey_format/errors.rb +8 -2
  25. data/lib/honey_format/helpers/helpers.rb +41 -0
  26. data/lib/honey_format/{header.rb → matrix/header.rb} +48 -12
  27. data/lib/honey_format/matrix/matrix.rb +104 -0
  28. data/lib/honey_format/{row.rb → matrix/row.rb} +6 -3
  29. data/lib/honey_format/{row_builder.rb → matrix/row_builder.rb} +5 -4
  30. data/lib/honey_format/{rows.rb → matrix/rows.rb} +7 -4
  31. data/lib/honey_format/matrix.rb +6 -89
  32. data/lib/honey_format/registry.rb +99 -0
  33. data/lib/honey_format/version.rb +4 -2
  34. data/lib/honey_format.rb +14 -6
  35. metadata +34 -24
  36. data/lib/honey_format/header_column_converter.rb +0 -40
  37. data/lib/honey_format/value_converter.rb +0 -117
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HoneyFormat
4
+ # Represents Matrix.
5
+ class Matrix
6
+ # Instantiate Matrix.
7
+ # @return [Matrix] a new instance of Matrix.
8
+ # @param [Array<Array<String, nil>>] matrix
9
+ # @param header [Array<String>]
10
+ # header optional argument that represents header, required if the matrix
11
+ # lacks a header row.
12
+ # @param header_converter [#call] converts header columns.
13
+ # @param header_deduplicator [#call] deduplicates header columns.
14
+ # @param row_builder [#call] will be called for each parsed row.
15
+ # @param type_map [Hash] map of column_name => type conversion to perform.
16
+ # @raise [HeaderError] super class of errors raised when there is a header error.
17
+ # @raise [MissingHeaderError] raised when header is missing (empty or nil).
18
+ # @raise [MissingHeaderColumnError] raised when header column is missing.
19
+ # @raise [RowError] super class of errors raised when there is a row error.
20
+ # @raise [EmptyRowColumnsError] raised when row columns are empty.
21
+ # @raise [InvalidRowLengthError] raised when row has more columns than header columns.
22
+ # @example
23
+ # matrix = HoneyFormat::Matrix.new([%w[name id], %w[jacob 1]])
24
+ # matrix.columns # => [:name, :id]
25
+ # matrix.rows.to_a # => [#<Row name="jacob", id="1">]
26
+ # @example With custom header converter
27
+ # converter = proc { |v| v == 'name' ? 'first_name' : v }
28
+ # matrix = HoneyFormat::Matrix.new([%w[name id]], header_converter: converter)
29
+ # matrix.columns # => [:first_name, :id]
30
+ # @example Handle errors
31
+ # begin
32
+ # matrix = HoneyFormat::Matrix.new([%w[name id]])
33
+ # rescue HoneyFormat::HeaderError => e
34
+ # puts "header error: #{e.class}, #{e.message}"
35
+ # rescue HoneyFormat::RowError => e
36
+ # puts "row error: #{e.class}, #{e.message}"
37
+ # end
38
+ # @see Header#initialize
39
+ # @see Rows#initialize
40
+ def initialize(
41
+ matrix,
42
+ header: nil,
43
+ header_converter: HoneyFormat.header_converter,
44
+ header_deduplicator: HoneyFormat.config.header_deduplicator,
45
+ row_builder: nil,
46
+ type_map: {}
47
+ )
48
+ header_row = header || matrix.shift
49
+ @header = Header.new(
50
+ header_row,
51
+ converter: header_converter,
52
+ deduplicator: header_deduplicator
53
+ )
54
+ @rows = Rows.new(matrix, columns, builder: row_builder, type_map: type_map)
55
+ end
56
+
57
+ # Original matrix header
58
+ # @return [Header] object representing the matrix header.
59
+ def header
60
+ @header
61
+ end
62
+
63
+ # Matrix columns converted from the original Matrix header
64
+ # @return [Array<Symbol>] of column identifiers.
65
+ def columns
66
+ @header.to_a
67
+ end
68
+
69
+ # Return rows
70
+ # @return [Rows] rows.
71
+ def rows
72
+ @rows
73
+ end
74
+
75
+ # Itereate over each row
76
+ # @yield [row] The given block will be passed for every row.
77
+ # @yieldparam [Row] row in the matrix.
78
+ # @return [Enumerator] If no block is given, an enumerator object will be returned.
79
+ def each_row
80
+ return rows.each unless block_given?
81
+
82
+ rows.each { |row| yield(row) }
83
+ end
84
+
85
+ # Convert matrix to CSV-string.
86
+ # @param columns [Array<Symbol>, Set<Symbol>, NilClass]
87
+ # the columns to output, nil means all columns (default: nil)
88
+ # @yield [row]
89
+ # The given block will be passed for every row - return truthy if you want the
90
+ # row to be included in the output
91
+ # @yieldparam [Row] row
92
+ # @return [String] CSV-string representation.
93
+ # @example with selected columns
94
+ # matrix.to_csv(columns: [:id, :country])
95
+ # @example with selected rows
96
+ # matrix.to_csv { |row| row.country == 'Sweden' }
97
+ # @example with both selected columns and rows
98
+ # matrix.to_csv(columns: [:id, :country]) { |row| row.country == 'Sweden' }
99
+ def to_csv(columns: nil, &block)
100
+ columns = columns&.map(&:to_sym)
101
+ @header.to_csv(columns: columns) + @rows.to_csv(columns: columns, &block)
102
+ end
103
+ end
104
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HoneyFormat
2
4
  # Default row builder
3
5
  class Row < Struct
@@ -12,7 +14,8 @@ module HoneyFormat
12
14
  end
13
15
 
14
16
  # Represent row as CSV
15
- # @param columns [Array<Symbol>, Set<Symbol>, NilClass] the columns to output, nil means all columns (default: nil)
17
+ # @param columns [Array<Symbol>, Set<Symbol>, NilClass]
18
+ # the columns to output, nil means all columns (default: nil)
16
19
  # @return [String] CSV-string representation.
17
20
  def to_csv(columns: nil)
18
21
  attributes = members
@@ -30,8 +33,8 @@ module HoneyFormat
30
33
  value = self[field]
31
34
  value = "\"#{value}\"" if value.is_a?(String)
32
35
 
33
- [field, value].join("=")
34
- end.join(", ")
36
+ [field, value].join('=')
37
+ end.join(', ')
35
38
 
36
39
  "#<Row #{attributes}>"
37
40
  end
@@ -1,4 +1,4 @@
1
- require 'honey_format/row'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module HoneyFormat
4
4
  # Holds data for a single row.
@@ -20,7 +20,7 @@ module HoneyFormat
20
20
  end
21
21
 
22
22
  @type_map = type_map
23
- @converter = HoneyFormat.value_converter
23
+ @converter = HoneyFormat.converter_registry
24
24
 
25
25
  @row_klass = Row.new(*columns)
26
26
  @builder = builder
@@ -30,7 +30,8 @@ module HoneyFormat
30
30
  # Returns an object representing the row.
31
31
  # @return [Row, Object] a new instance of built row.
32
32
  # @param row [Array] the row array.
33
- # @raise [InvalidRowLengthError] raised when there are more row elements longer than columns
33
+ # @raise [InvalidRowLengthError]
34
+ # raised when there are more row elements longer than columns
34
35
  # @example Build new row
35
36
  # r = RowBuilder.new([:id])
36
37
  # r.build(['1']).id #=> '1'
@@ -71,7 +72,7 @@ module HoneyFormat
71
72
  "Row length #{row.length}",
72
73
  "column length #{@columns.length}",
73
74
  "row: #{row.inspect}",
74
- "orignal message: '#{e.message}'"
75
+ "orignal message: '#{e.message}'",
75
76
  ].join(', ')
76
77
  raise(Errors::InvalidRowLengthError, err_msg)
77
78
  end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'set'
2
- require 'honey_format/row_builder'
3
4
 
4
5
  module HoneyFormat
5
6
  # Represents rows.
@@ -46,8 +47,10 @@ module HoneyFormat
46
47
  end
47
48
  alias_method :size, :length
48
49
 
49
- # @param columns [Array<Symbol>, Set<Symbol>, NilClass] the columns to output, nil means all columns (default: nil)
50
- # @yield [row] each row - return truthy if you want the row to be included in the output
50
+ # @param columns [Array<Symbol>, Set<Symbol>, NilClass]
51
+ # the columns to output, nil means all columns (default: nil)
52
+ # @yield [row]
53
+ # each row - return truthy if you want the row to be included in the output
51
54
  # @yieldparam [Row] row
52
55
  # @return [String] CSV-string representation.
53
56
  # @example with selected columns
@@ -61,7 +64,7 @@ module HoneyFormat
61
64
  columns = Set.new(columns.map(&:to_sym)) if columns
62
65
  csv_rows = []
63
66
  each do |row|
64
- if !block || block.call(row)
67
+ if !block || yield(row)
65
68
  csv_rows << row.to_csv(columns: columns)
66
69
  end
67
70
  end
@@ -1,90 +1,7 @@
1
- require 'csv'
1
+ # frozen_string_literal: true
2
2
 
3
- require 'honey_format/rows'
4
- require 'honey_format/header'
5
-
6
- module HoneyFormat
7
- # Represents CSV.
8
- class Matrix
9
- # Instantiate CSV.
10
- # @return [CSV] a new instance of Matrix.
11
- # @param [Array<Array<String, nil>>] matrix
12
- # @param [Array<String>] header optional argument that represents header, required if the matrix lacks a header row.
13
- # @param [#call] header_converter converts header columns.
14
- # @param [#call] row_builder will be called for each parsed row.
15
- # @param type_map [Hash] map of column_name => type conversion to perform.
16
- # @raise [HeaderError] super class of errors raised when there is a header error.
17
- # @raise [MissingHeaderError] raised when header is missing (empty or nil).
18
- # @raise [MissingHeaderColumnError] raised when header column is missing.
19
- # @raise [RowError] super class of errors raised when there is a row error.
20
- # @raise [EmptyRowColumnsError] raised when row columns are empty.
21
- # @raise [InvalidRowLengthError] raised when row has more columns than header columns.
22
- # @example
23
- # matrix = HoneyFormat::Matrix.new([%w[name id]])
24
- # @example With custom header converter
25
- # converter = proc { |v| v == 'name' ? 'first_name' : v }
26
- # matrix = HoneyFormat::Matrix.new([%w[name id]], header_converter: converter)
27
- # matrix.columns # => [:first_name, :id]
28
- # @example Handle errors
29
- # begin
30
- # matrix = HoneyFormat::Matrix.new([%w[name id]])
31
- # rescue HoneyFormat::HeaderError => e
32
- # puts "header error: #{e.class}, #{e.message}"
33
- # rescue HoneyFormat::RowError => e
34
- # puts "row error: #{e.class}, #{e.message}"
35
- # end
36
- def initialize(
37
- matrix,
38
- header: nil,
39
- header_converter: HoneyFormat.header_converter,
40
- row_builder: nil,
41
- type_map: {}
42
- )
43
- header_row = header || matrix.shift
44
- @header = Header.new(header_row, converter: header_converter)
45
- @rows = Rows.new(matrix, columns, builder: row_builder, type_map: type_map)
46
- end
47
-
48
- # Original CSV header
49
- # @return [Header] object representing the CSV header.
50
- def header
51
- @header
52
- end
53
-
54
- # CSV columns converted from the original CSV header
55
- # @return [Array<Symbol>] of column identifiers.
56
- def columns
57
- @header.to_a
58
- end
59
-
60
- # @return [Rows] of rows.
61
- def rows
62
- @rows
63
- end
64
-
65
- # @yield [row] The given block will be passed for every row.
66
- # @yieldparam [Row] row in the CSV.
67
- # @return [Enumerator] If no block is given, an enumerator object will be returned.
68
- def each_row
69
- return rows.each unless block_given?
70
-
71
- rows.each { |row| yield(row) }
72
- end
73
-
74
- # Convert matrix to CSV-string.
75
- # @param columns [Array<Symbol>, Set<Symbol>, NilClass] the columns to output, nil means all columns (default: nil)
76
- # @yield [row] The given block will be passed for every row - return truthy if you want the row to be included in the output
77
- # @yieldparam [Row] row
78
- # @return [String] CSV-string representation.
79
- # @example with selected columns
80
- # matrix.to_csv(columns: [:id, :country])
81
- # @example with selected rows
82
- # matrix.to_csv { |row| row.country == 'Sweden' }
83
- # @example with both selected columns and rows
84
- # matrix.to_csv(columns: [:id, :country]) { |row| row.country == 'Sweden' }
85
- def to_csv(columns: nil, &block)
86
- columns = columns&.map(&:to_sym)
87
- @header.to_csv(columns: columns) + @rows.to_csv(columns: columns, &block)
88
- end
89
- end
90
- end
3
+ require 'honey_format/matrix/row_builder'
4
+ require 'honey_format/matrix/rows'
5
+ require 'honey_format/matrix/row'
6
+ require 'honey_format/matrix/header'
7
+ require 'honey_format/matrix/matrix'
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HoneyFormat
4
+ # Registry that holds value callers
5
+ class Registry
6
+ # Instantiate a caller registry
7
+ # @param [Hash] default hash of defaults
8
+ def initialize(default = {})
9
+ @callers = nil
10
+ @default = default.dup
11
+ reset!
12
+ end
13
+
14
+ # Returns list of registered types
15
+ # @return [Array<Symbol>] list of registered types
16
+ def types
17
+ @callers.keys
18
+ end
19
+
20
+ # Register a caller
21
+ # @param [Symbol, String] type the name of the type
22
+ # @param [#call] caller that responds to #call
23
+ # @return [Registry] returns self
24
+ # @raise [TypeExistsError] if type is already registered
25
+ def register(type, caller)
26
+ self[type] = caller
27
+ self
28
+ end
29
+
30
+ # Unregister a caller
31
+ # @param [Symbol, String] type the name of the type
32
+ # @return [Registry] returns self
33
+ # @raise [UnknownTypeError] if type is already registered
34
+ def unregister(type)
35
+ unknown_type_error!(type) unless type?(type)
36
+ @callers.delete(to_key(type))
37
+ self
38
+ end
39
+
40
+ # Call value type
41
+ # @param [Symbol, String] type the name of the type
42
+ # @param [Object] value to be converted
43
+ def call(value, type)
44
+ self[type].call(value)
45
+ end
46
+
47
+ # Register a caller
48
+ # @param [Symbol, String] type the name of the type
49
+ # @param [#call] caller that responds to #call
50
+ # @return [Object] returns the caller
51
+ # @raise [TypeExistsError] if type is already registered
52
+ def []=(type, caller)
53
+ type = to_key(type)
54
+
55
+ if type?(type)
56
+ raise(Errors::TypeExistsError, "type '#{type}' already exists")
57
+ end
58
+
59
+ @callers[type] = caller
60
+ end
61
+
62
+ # Returns the given type or raises error if type doesn't exist
63
+ # @param [Symbol, String] type the name of the type
64
+ # @return [Object] returns the caller
65
+ # @raise [UnknownTypeError] if type does not exist
66
+ def [](type)
67
+ @callers.fetch(to_key(type)) { unknown_type_error!(type) }
68
+ end
69
+
70
+ # Returns true if the type exists, false otherwise
71
+ # @param [Symbol, String] type the name of the type
72
+ # @return [true, false] true if type exists, false otherwise
73
+ def type?(type)
74
+ return false unless keyable?(type)
75
+ @callers.key?(to_key(type))
76
+ end
77
+
78
+ # Resets the caller registry to its default configuration
79
+ # @return [Registry] returns the caller registry
80
+ def reset!
81
+ @callers = @default.dup
82
+ self
83
+ end
84
+
85
+ private
86
+
87
+ def keyable?(key)
88
+ key.respond_to?(:to_sym)
89
+ end
90
+
91
+ def to_key(key)
92
+ key.to_sym
93
+ end
94
+
95
+ def unknown_type_error!(type)
96
+ raise(Errors::UnknownTypeError, "unknown type '#{type}'")
97
+ end
98
+ end
99
+ end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HoneyFormat
2
4
  # Gem version
3
5
  VERSION = [
4
6
  MAJOR_VERSION = 0,
5
- MINOR_VERSION = 16,
6
- PATCH_VERSION = 0
7
+ MINOR_VERSION = 17,
8
+ PATCH_VERSION = 0,
7
9
  ].join('.')
8
10
  end
data/lib/honey_format.rb CHANGED
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'honey_format/version'
2
4
  require 'honey_format/configuration'
3
5
  require 'honey_format/errors'
4
- require 'honey_format/value_converter'
6
+ require 'honey_format/registry'
7
+ require 'honey_format/converters/converters'
5
8
  require 'honey_format/csv'
6
9
 
7
-
8
10
  # Main module for HoneyFormat
9
11
  module HoneyFormat
10
12
  # CSV alias
@@ -32,9 +34,15 @@ module HoneyFormat
32
34
  config.header_converter
33
35
  end
34
36
 
35
- # Returns the configured value converter
36
- # @return [#call] the current value converter
37
- def self.value_converter
38
- config.converter
37
+ # Returns the configured converter registry
38
+ # @return [Registry] the current converter registry
39
+ def self.converter_registry
40
+ config.converter_registry
41
+ end
42
+
43
+ # Returns the configured deduplicator registry
44
+ # @return [Registry] the current deduplicator registry
45
+ def self.header_deduplicator_registry
46
+ config.header_deduplicator_registry
39
47
  end
40
48
  end
metadata CHANGED
@@ -1,45 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honey_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Burenstam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-30 00:00:00.000000000 Z
11
+ date: 2018-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: benchmark-ips
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '1.10'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '1.10'
41
41
  - !ruby/object:Gem::Dependency
42
- name: benchmark-ips
42
+ name: byebug
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,21 +53,21 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '10.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '10.0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: simplecov
70
+ name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: byebug
84
+ name: simplecov
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -105,7 +105,10 @@ extensions: []
105
105
  extra_rdoc_files: []
106
106
  files:
107
107
  - ".gitignore"
108
+ - ".hound.yml"
108
109
  - ".rspec"
110
+ - ".rubocop.yml"
111
+ - ".ruby-style-guide.yml"
109
112
  - ".travis.yml"
110
113
  - CHANGELOG.md
111
114
  - CODE_OF_CONDUCT.md
@@ -123,15 +126,22 @@ files:
123
126
  - lib/honey_format/cli/cli.rb
124
127
  - lib/honey_format/cli/result_writer.rb
125
128
  - lib/honey_format/configuration.rb
129
+ - lib/honey_format/converters/convert_boolean.rb
130
+ - lib/honey_format/converters/convert_date_and_time.rb
131
+ - lib/honey_format/converters/convert_number.rb
132
+ - lib/honey_format/converters/convert_string.rb
133
+ - lib/honey_format/converters/converters.rb
134
+ - lib/honey_format/converters/header_column_converter.rb
126
135
  - lib/honey_format/csv.rb
127
136
  - lib/honey_format/errors.rb
128
- - lib/honey_format/header.rb
129
- - lib/honey_format/header_column_converter.rb
137
+ - lib/honey_format/helpers/helpers.rb
130
138
  - lib/honey_format/matrix.rb
131
- - lib/honey_format/row.rb
132
- - lib/honey_format/row_builder.rb
133
- - lib/honey_format/rows.rb
134
- - lib/honey_format/value_converter.rb
139
+ - lib/honey_format/matrix/header.rb
140
+ - lib/honey_format/matrix/matrix.rb
141
+ - lib/honey_format/matrix/row.rb
142
+ - lib/honey_format/matrix/row_builder.rb
143
+ - lib/honey_format/matrix/rows.rb
144
+ - lib/honey_format/registry.rb
135
145
  - lib/honey_format/version.rb
136
146
  homepage: https://github.com/buren/honey_format
137
147
  licenses:
@@ -1,40 +0,0 @@
1
- module HoneyFormat
2
- # Header column converter
3
- module HeaderColumnConverter
4
- # Replace map
5
- REPLACE_MAP = [
6
- [/ \(/, '('],
7
- [/ \[/, '['],
8
- [/ \{/, '{'],
9
- [/\) /, ')'],
10
- [/\] /, ']'],
11
- [/\} /, '}'],
12
- [/ /, '_'],
13
- [/-/, '_']
14
- ].map { |array| array.freeze }.freeze
15
-
16
- # Returns converted value and mutates the argument.
17
- # @return [Symbol] the cleaned header column.
18
- # @param [String] column the string to be cleaned.
19
- # @param [Integer] index the column index.
20
- # @example Convert simple header
21
- # HeaderColumnConverter.call(" User name ") #=> "user_name"
22
- # @example Convert complex header
23
- # HeaderColumnConverter.call(" First name (user)") #=> :'first_name(user)'
24
- def self.call(column, index = nil)
25
- if column.nil? || column.empty?
26
- raise(ArgumentError, "column and column index can't be blank/nil") unless index
27
- return :"column#{index}"
28
- end
29
-
30
- column = column.dup
31
- column.strip!
32
- column.downcase!
33
- REPLACE_MAP.each do |data|
34
- from, to = data
35
- column.gsub!(from, to)
36
- end
37
- column.to_sym
38
- end
39
- end
40
- end