honey_format 0.4.0 → 0.5.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: a39f797b080a1d5ff641f7f477cdfe2d181cf599e0ab3e05a3e1d69e21ebb94b
4
- data.tar.gz: 2701ce31bf31e19229d11d6ff6f817de24c9d24165d27904c30cd8bc7f6b9752
3
+ metadata.gz: 3f11a451917cde22eb1749f01e42e12d4bfc3314f0dd42692f27100c452557ab
4
+ data.tar.gz: 4ad3a5e3be4a82e26fb3715c50011b5f7cc26069ac74058763cc4d097d1f7f0d
5
5
  SHA512:
6
- metadata.gz: cedc41b03b37daa7199cd11d6f2b4fd72599ae31adac8c7379c3dd396f364215cf5eff2376d09c975482ae44491aa74d7042e3c90b4bfb36ddc156a51b32c372
7
- data.tar.gz: 45ec386f47253fd4a1e5c6e86cd99f7e381abe93be65fb4fc766fb2b3fdc29f84c4037209af68cf75ad1fbc10621d539b04976bc8e579eac9398fc8251755b2e
6
+ metadata.gz: 63090f5e515480669c3dc2a74882e4fc4aba303eb7f2f0e71fbeb67a7f9ccf9f5b0cf592a07af8890e79da2f682ce9f6622be672a141219e1471d559f1ac9223
7
+ data.tar.gz: 9439ebcfc40892fb57ac3cdca5f74f6c2ac58e0972b5200a6678194376c5063b4cc1083fde94f68c42c302fd720124c6ff3878cbd6b5d5546e92d38c0d4fcc87
@@ -1,6 +1,12 @@
1
1
  module HoneyFormat
2
2
  # Holds data for a single row.
3
3
  class Row
4
+ class RowBuilder < Struct
5
+ def self.call(row)
6
+ new(*row)
7
+ end
8
+ end
9
+
4
10
  # Returns a new instance of Row.
5
11
  # @return [Row] a new instance of Row.
6
12
  # @param [Array] columns an array of symbols.
@@ -9,9 +15,9 @@ module HoneyFormat
9
15
  # Row.new!([:id])
10
16
  def initialize(columns, builder: nil)
11
17
  validate_columns!(columns)
12
- @klass = Struct.new(*columns)
18
+ @row_builder = RowBuilder.new(*columns)
19
+ @builder = builder || ->(row) { row }
13
20
  @columns = columns
14
- @builder = builder
15
21
  end
16
22
 
17
23
  # Returns a Struct.
@@ -19,10 +25,11 @@ module HoneyFormat
19
25
  # @param row [Array] the row array.
20
26
  # @raise [InvalidRowLengthError] raised when there are more row elements longer than columns
21
27
  # @example Build new row
22
- # r = Row.new!([:id])
28
+ # r = Row.new([:id])
23
29
  # r.build(['1']).id #=> '1'
24
30
  def build(row)
25
- @klass.new(*row).tap { |o| @builder && @builder.call(o) }
31
+ built_row = @row_builder.call(row)
32
+ @builder.call(built_row)
26
33
  rescue ArgumentError, 'struct size differs'
27
34
  fail_for_struct_size_diff!(row)
28
35
  end
@@ -30,10 +37,10 @@ module HoneyFormat
30
37
  private
31
38
 
32
39
  def validate_columns!(columns)
33
- if columns.empty?
34
- err_msg = 'Expected array with at least one element, but was empty.'
35
- fail(EmptyColumnsError, err_msg)
36
- end
40
+ return unless columns.empty?
41
+
42
+ err_msg = 'Expected array with at least one element, but was empty.'
43
+ fail(EmptyColumnsError, err_msg)
37
44
  end
38
45
 
39
46
  def fail_for_struct_size_diff!(row)
@@ -1,4 +1,4 @@
1
1
  module HoneyFormat
2
2
  # Gem version
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.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.4.0
4
+ version: 0.5.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: 2018-05-03 00:00:00.000000000 Z
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler