table_structure 0.1.1 → 0.1.2

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: 01f8d4a85402265ce225a8e137e2f16df3c4294fbe21037c74d799606b547fa7
4
- data.tar.gz: 91db3584049011857bc704ed1be0b4d4e07e10effc1286af35ea9d3e0e90309d
3
+ metadata.gz: f308e4cc6c48b759e040421bb73d27582bf63555bb3a7e8cffbe9d881409873e
4
+ data.tar.gz: e7661a42884ac86bd894e9fd9e542433b31627355d71812d525b9e0a66bdc6d0
5
5
  SHA512:
6
- metadata.gz: 57b2bb8f02703930fca0b26976e0d84469d69127b8203af4cf86976a2bb5f2f83e5f5fd282288bc9bd22e8f79be4c5a3f8559f07d4a418b2e16e9a585589dae2
7
- data.tar.gz: a9bc2bf2e7cf4bb4a19c1f8a60f4ad396c8197c3c7e06f0d8e7db9102807ddfca397df3cc02642989aba4047d542e4c3e2e1184ea2cfabd2f599e94d225b9007
6
+ metadata.gz: 4f042133eac79798923ac099a8108d29a9bfd43e36fe68d4811f2074ea9da9e638174e0381ac8fefcaeeea71d8e29a1504a439799e9ac5638d1b45fc040239a6
7
+ data.tar.gz: 9578b9cd2478a440fd3b8a0b6e84ade031cd0b298ae11e7766c39529e3b795340005bd20fade907cd961879da318166db958818c0b3a38284fd0b450f13a98f0
data/.travis.yml CHANGED
@@ -6,4 +6,5 @@ rvm:
6
6
  - 2.6
7
7
  - 2.5
8
8
  - 2.4
9
+ - 2.3
9
10
  before_install: gem install bundler -v 1.17.3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.1.1)
4
+ table_structure (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -5,9 +5,6 @@
5
5
  `TableStructure` has two major functions.
6
6
  The functions are `TableStructure::Schema` that defines the schema of a table using DSL and ` TableStructure::Writer` that converts and outputs data with that schema.
7
7
 
8
- `TableStructure::Writer` outputs the converted data line by line.
9
- This keeps the memory usage constant and is suitable for large-scale CSV output.
10
-
11
8
  ## Installation
12
9
 
13
10
  Add this line to your application's Gemfile:
@@ -68,6 +65,8 @@ schema = SampleTableSchema.new(context: context)
68
65
  #### TableStructure::Writer
69
66
  ```ruby
70
67
  writer = TableStructure::Writer.new(schema)
68
+ ## When omitting header line
69
+ # writer = TableStructure::Writer.new(schema, header_omitted: true)
71
70
 
72
71
  items = [
73
72
  {
@@ -117,11 +116,11 @@ class SampleTableSchema
117
116
 
118
117
  TableContext = Struct.new(:questions, keyword_init: true)
119
118
 
120
- RowContext = Struct.new(:id, :name, :pets, :answers, keyword_init: true) {
119
+ RowContext = Struct.new(:id, :name, :pets, :answers, keyword_init: true) do
121
120
  def more_pets
122
121
  pets + pets
123
122
  end
124
- }
123
+ end
125
124
 
126
125
  context_builder :table, ->(context) { TableContext.new(**context) }
127
126
  context_builder :row, ->(context) { RowContext.new(**context) }
@@ -135,14 +134,14 @@ class SampleTableSchema
135
134
  columns name: ['Pet 1', 'Pet 2', 'Pet 3'],
136
135
  value: ->(row, *) { row.more_pets }
137
136
 
138
- columns ->(table) {
137
+ columns ->(table) do
139
138
  table.questions.map do |question|
140
139
  {
141
140
  name: question[:id],
142
141
  value: ->(row, *) { row.answers[question[:id]] }
143
142
  }
144
143
  end
145
- }
144
+ end
146
145
 
147
146
  column_converter :to_s, ->(val, *) { val.to_s }
148
147
  end
@@ -2,13 +2,34 @@ module TableStructure
2
2
  module Schema
3
3
  class Column
4
4
 
5
+ class Error < ::TableStructure::Error
6
+ attr_reader :group_index
7
+
8
+ def initialize(error_message, group_index)
9
+ @group_index = group_index
10
+ super("#{error_message} [defined position: #{group_index + 1}]")
11
+ end
12
+ end
13
+
14
+ DEFAULT_DEFINITION = {
15
+ name: nil,
16
+ key: nil,
17
+ value: nil,
18
+ size: nil
19
+ }
20
+
5
21
  DEFAULT_SIZE = 1
6
22
 
7
- def initialize(name: nil, key: nil, value: nil, size: nil)
8
- @name = name
9
- @key = key
10
- @value = value
11
- @size = determine_size(specified_size: size)
23
+ attr_reader :size, :group_index
24
+
25
+ def initialize(definition, group_index)
26
+ @group_index = group_index
27
+ definition = DEFAULT_DEFINITION.merge(definition)
28
+ validate(definition)
29
+ @name = definition[:name]
30
+ @key = definition[:key]
31
+ @value = definition[:value]
32
+ @size = determine_size(definition)
12
33
  end
13
34
 
14
35
  def name(header_context, table_context)
@@ -17,7 +38,7 @@ module TableStructure
17
38
  end
18
39
 
19
40
  def key
20
- @key
41
+ optimize_size(@key)
21
42
  end
22
43
 
23
44
  def value(row_context, table_context)
@@ -27,18 +48,23 @@ module TableStructure
27
48
 
28
49
  private
29
50
 
30
- def determine_size(specified_size:)
31
- if @name.respond_to?(:call) && !specified_size
32
- raise ::TableStructure::Error.new('"size" must be specified, because column size cannot be determined.')
51
+ def validate(name:, key:, size:, **)
52
+ if !key && name.respond_to?(:call) && !size
53
+ raise Error.new('"size" must be specified, because column size cannot be determined.', @group_index)
33
54
  end
34
- if specified_size
35
- if specified_size < DEFAULT_SIZE
36
- raise ::TableStructure::Error.new('"size" must be positive.')
37
- end
38
- return specified_size
55
+ if size && size < DEFAULT_SIZE
56
+ raise Error.new('"size" must be positive.', @group_index)
39
57
  end
40
- if @name.kind_of?(Array)
41
- return @name.empty? ? DEFAULT_SIZE : @name.size
58
+ end
59
+
60
+ def determine_size(name:, key:, size:, **)
61
+ return size if size
62
+ [calculate_size(name), calculate_size(key)].max
63
+ end
64
+
65
+ def calculate_size(val)
66
+ if val.kind_of?(Array)
67
+ return val.empty? ? DEFAULT_SIZE : val.size
42
68
  end
43
69
  DEFAULT_SIZE
44
70
  end
@@ -4,7 +4,7 @@ module TableStructure
4
4
 
5
5
  DEFAULT_OPTIONS = { result_type: :array }
6
6
 
7
- attr_reader :column_converters, :result_builders
7
+ attr_reader :columns, :column_converters, :result_builders
8
8
 
9
9
  def initialize(column_definitions, column_converters, result_builders, context, options)
10
10
  @context = context
@@ -24,11 +24,15 @@ module TableStructure
24
24
 
25
25
  private
26
26
 
27
- def build_columns(column_definitions)
28
- column_definitions
29
- .map { |column| Utils.evaluate_callable(column, @context) }
30
- .flatten
31
- .map { |column| Column.new(**column) }
27
+ def build_columns(definitions)
28
+ definitions
29
+ .map { |definition| Utils.evaluate_callable(definition, @context) }
30
+ .map.with_index do |definition, i|
31
+ [definition]
32
+ .flatten
33
+ .map { |definition| Column.new(definition, i) }
34
+ end
35
+ .flatten
32
36
  end
33
37
 
34
38
  def default_column_converters
@@ -1,3 +1,3 @@
1
1
  module TableStructure
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,4 +1,6 @@
1
1
  module TableStructure
2
+ class Error < StandardError; end
3
+
2
4
  require 'table_structure/version'
3
5
 
4
6
  require 'table_structure/schema'
@@ -11,5 +13,4 @@ module TableStructure
11
13
  require 'table_structure/schema/utils'
12
14
  require 'table_structure/writer'
13
15
 
14
- class Error < StandardError; end
15
16
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["jsmmr@icloud.com"]
11
11
 
12
12
  spec.summary = %q{Create and output table structure data.}
13
- spec.description = %q{This gem creates and outputs table structure data. Useful for outputting large CSV file.}
13
+ spec.description = %q{This gem creates and outputs table structure data. Useful for creating CSV.}
14
14
  spec.homepage = "https://github.com/jsmmr/ruby_table_structure"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-15 00:00:00.000000000 Z
11
+ date: 2019-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,8 +52,8 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: This gem creates and outputs table structure data. Useful for outputting
56
- large CSV file.
55
+ description: This gem creates and outputs table structure data. Useful for creating
56
+ CSV.
57
57
  email:
58
58
  - jsmmr@icloud.com
59
59
  executables: []
@@ -101,7 +101,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
103
  requirements: []
104
- rubygems_version: 3.0.4
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.6.2
105
106
  signing_key:
106
107
  specification_version: 4
107
108
  summary: Create and output table structure data.