honey_format 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd242cb77ccf8589eb2df059d887e13596f9c33a14fa587a886bcd376a6d728e
4
- data.tar.gz: 72842684af98946cd50f7756818a4e9ce94b6c35d6c23a1e14661f70a93d3bac
3
+ metadata.gz: e5d7c212e16b346e58a80617576ff6c39b1938885b8a66ad7a91c1b7654897cf
4
+ data.tar.gz: e70bee6d2a69eb8624a35d69300c0e0f02772e88f0237a94f64fa6a13d0c409f
5
5
  SHA512:
6
- metadata.gz: ed5f6fa7f275d7445ca2962aa1787f8c10e25125253c3c65e3a5732cb0ef40e91f7c7d6bf73fa2d49d8d684c43bfec48058369f38df2e9ed5a52522f36f59d8c
7
- data.tar.gz: 987843e3c4a987112bfbd46d00fa61a885ef12b71f19005e578e49d3f6f1bbe3af9022e3666af90437e015d522dbc7557a9f3065c45e4d0dd797d48670a9667f
6
+ metadata.gz: 7a0af309f8e7f31f725cd9b3d391ee27c4ebf858d31498f39dad99e78cf55ebcd8541a347fb2937fa51a52a0b43405b611943f52dda8040a9124ada30f3a2b54
7
+ data.tar.gz: 9c6a89db6da810c78dfe2c74b0fb1ea175b4e62392226bd84e92c53ce5529049190e5c99cdb518dc358f5a98d4385fc3554add7407d2f5c809e3966631ffc467
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- # v0.9.2
1
+ # v0.10.0
2
+
3
+ * Add support for filtering what rows are included in `#to_csv` [[#PR11](https://github.com/buren/honey_format/pull/11)]
4
+
5
+ # v0.9.0
2
6
 
3
7
  :warning: This release contains some backwards compatible changes.
4
8
 
data/README.md CHANGED
@@ -91,6 +91,13 @@ csv = HoneyFormat::CSV.new(csv_string)
91
91
  csv.to_csv(columns: [:id, :country]) # => "id,country\nburen,Sweden\n"
92
92
  ```
93
93
 
94
+ Output a subset of rows to CSV
95
+ ```ruby
96
+ csv_string = "Name, Country\nburen,Sweden\njacob,Denmark"
97
+ csv = HoneyFormat::CSV.new(csv_string)
98
+ csv.to_csv { |row| row.country == 'Sweden' } # => "name,country\nburen,Sweden\n"
99
+ ```
100
+
94
101
  You can of course set the delimiter
95
102
  ```ruby
96
103
  HoneyFormat::CSV.new(csv_string, delimiter: ';')
data/honey_format.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
20
20
 
21
+ spec.required_ruby_version = '>= 2.3.0'
22
+
21
23
  spec.add_development_dependency 'bundler', '~> 1.10'
22
24
  spec.add_development_dependency 'rake', '~> 10.0'
23
25
  spec.add_development_dependency 'benchmark-ips'
@@ -16,6 +16,7 @@ module HoneyFormat
16
16
  # Returns converted value and mutates the argument.
17
17
  # @return [Symbol] the cleaned header column.
18
18
  # @param [String] column the string to be cleaned.
19
+ # @param [Integer] column index.
19
20
  # @example Convert simple header
20
21
  # ConvertHeaderValue.call(" User name ") #=> "user_name"
21
22
  # @example Convert complex header
@@ -7,9 +7,11 @@ module HoneyFormat
7
7
  # Represents CSV.
8
8
  class CSV
9
9
  # @return [CSV] a new instance of CSV.
10
- # @param [String] csv string.
11
- # @param [Array<Symbol>] valid_columns valid array of symbols representing valid columns if empty all will be considered valid.
12
- # @param [Array<String>] header optional argument for CSV header.
10
+ # @param [String] csv the CSV string
11
+ # @param [String] delimiter the CSV delimiter
12
+ # @param [Array<String>] header optional argument that represents CSV header, required if the CSV file lacks a header row.
13
+ # @param [Array<Symbol>] valid_columns array of symbols representing valid columns, if empty all will be considered valid.
14
+ # @param [#call] header_converter converts header columns.
13
15
  # @param [#call] row_builder will be called for each parsed row.
14
16
  # @raise [HeaderError] super class of errors raised when there is a CSV header error.
15
17
  # @raise [MissingHeaderError] raised when header is missing (empty or nil).
@@ -38,13 +40,12 @@ module HoneyFormat
38
40
  end
39
41
 
40
42
  # @return [Array] of rows.
41
- # @raise [InvalidRowLengthError] raised when there are more row elements longer than columns
42
43
  def rows
43
44
  @rows
44
45
  end
45
46
 
46
47
  # @yield [row] The given block will be passed for every row.
47
- # @yieldparam [Row] a colmn in the CSV header.
48
+ # @yieldparam [Row] row in the CSV.
48
49
  # @return [Enumerator] If no block is given, an enumerator object will be returned.
49
50
  def each_row
50
51
  return rows.each unless block_given?
@@ -53,9 +54,18 @@ module HoneyFormat
53
54
  end
54
55
 
55
56
  # Convert CSV object as CSV-string.
57
+ # @param columns [Array<Symbol>, Set<Symbol>, NilClass] the columns to output, nil means all columns (default: nil)
58
+ # @yield [row] The given block will be passed for every row - return truthy if you want the row to be included in the output
59
+ # @yieldparam [Row] row
56
60
  # @return [String] CSV-string representation.
57
- def to_csv(columns: nil)
58
- @header.to_csv(columns: columns) + @rows.to_csv(columns: columns)
61
+ # @example with selected columns
62
+ # csv.to_csv(columns: [:id, :country])
63
+ # @example with selected rows
64
+ # csv.to_csv { |row| row.country == 'Sweden' }
65
+ # @example with both selected columns and rows
66
+ # csv.to_csv(columns: [:id, :country]) { |row| row.country == 'Sweden' }
67
+ def to_csv(columns: nil, &block)
68
+ @header.to_csv(columns: columns) + @rows.to_csv(columns: columns, &block)
59
69
  end
60
70
  end
61
71
  end
@@ -10,6 +10,7 @@ module HoneyFormat
10
10
  # @param [Array<String>] header array of strings.
11
11
  # @param [Array<Symbol, String>] valid array representing the valid columns, if empty all columns will be considered valid.
12
12
  # @param converter [#call] header converter that implements a #call method that takes one column (string) argument.
13
+ # @raise [HeaderError] super class of errors raised when there is a CSV header error.
13
14
  # @raise [MissingHeaderColumnError] raised when header is missing
14
15
  # @raise [UnknownHeaderColumnError] raised when column is not in valid list.
15
16
  # @example Instantiate a header with a customer converter
@@ -32,7 +33,7 @@ module HoneyFormat
32
33
  end
33
34
 
34
35
  # @yield [row] The given block will be passed for every column.
35
- # @yieldparam [Row] a colmn in the CSV header.
36
+ # @yieldparam [Row] a column in the CSV header.
36
37
  # @return [Enumerator]
37
38
  # If no block is given, an enumerator object will be returned.
38
39
  def each(&block)
@@ -7,7 +7,9 @@ module HoneyFormat
7
7
  # @return [Row] a new instance of Row.
8
8
  # @param [Array] columns an array of symbols.
9
9
  # @param builder [#call, #to_csv] optional row builder
10
+ # @raise [RowError] super class of errors raised when there is a row error.
10
11
  # @raise [EmptyRowColumnsError] raised when there are no columns.
12
+ # @raise [InvalidRowLengthError] raised when row has more columns than header columns.
11
13
  # @example Create new row
12
14
  # Row.new!([:id])
13
15
  def initialize(columns, builder: nil)
@@ -22,7 +24,7 @@ module HoneyFormat
22
24
  end
23
25
 
24
26
  # Returns a Struct.
25
- # @return [Struct] a new instance of Row.
27
+ # @return [Object] a new instance of built row.
26
28
  # @param row [Array] the row array.
27
29
  # @raise [InvalidRowLengthError] raised when there are more row elements longer than columns
28
30
  # @example Build new row
@@ -8,6 +8,7 @@ module HoneyFormat
8
8
  end
9
9
 
10
10
  # Represent row as CSV
11
+ # @param columns [Array<Symbol>, Set<Symbol>, NilClass] the columns to output, nil means all columns (default: nil)
11
12
  # @return [String] CSV-string representation.
12
13
  def to_csv(columns: nil)
13
14
  attributes = members
@@ -10,6 +10,9 @@ module HoneyFormat
10
10
  # @return [Rows] new instance of Rows.
11
11
  # @param [Array] rows the array of rows.
12
12
  # @param [Array] columns the array of column symbols.
13
+ # @raise [RowError] super class of errors raised when there is a row error.
14
+ # @raise [EmptyRowColumnsError] raised when there are no columns.
15
+ # @raise [InvalidRowLengthError] raised when row has more columns than header columns.
13
16
  def initialize(rows, columns, builder: nil)
14
17
  @rows = prepare_rows(Row.new(columns, builder: builder), rows)
15
18
  end
@@ -35,11 +38,26 @@ module HoneyFormat
35
38
  end
36
39
  alias_method :size, :length
37
40
 
41
+ # @param columns [Array<Symbol>, Set<Symbol>, NilClass] the columns to output, nil means all columns (default: nil)
42
+ # @yield [row] each row - return truthy if you want the row to be included in the output
43
+ # @yieldparam [Row] row
38
44
  # @return [String] CSV-string representation.
39
- def to_csv(columns: nil)
45
+ # @example with selected columns
46
+ # rows.to_csv(columns: [:id, :country])
47
+ # @example with selected rows
48
+ # rows.to_csv { |row| row.country == 'Sweden' }
49
+ # @example with both selected columns and rows
50
+ # csv.to_csv(columns: [:id, :country]) { |row| row.country == 'Sweden' }
51
+ def to_csv(columns: nil, &block)
40
52
  # Convert columns to Set for performance
41
53
  columns = Set.new(columns) if columns
42
- to_a.map { |row| row.to_csv(columns: columns) }.join
54
+ csv_rows = []
55
+ each do |row|
56
+ if !block || block.call(row)
57
+ csv_rows << row.to_csv(columns: columns)
58
+ end
59
+ end
60
+ csv_rows.join
43
61
  end
44
62
 
45
63
  private
@@ -1,4 +1,4 @@
1
1
  module HoneyFormat
2
2
  # Gem version
3
- VERSION = '0.9.0'
3
+ VERSION = '0.10.0'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honey_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Burenstam
@@ -137,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
- version: '0'
140
+ version: 2.3.0
141
141
  required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="